• vue3 + elementPlus实现select下拉框插入确定和取消按钮。


    实现思路

    Select 选择器 | Element Plus

    1、select方法@visible-change这个方法是下拉框出现/隐藏时触发,当显示的时候将两个按钮插入到下拉框里面,是基于原生插入DOM的这种方式;

    2、通过vue3 ref获取selectDOM,在获取select的popperPaneRef.$el (select下拉框的DOM),在基于js方法创建div `document.createElement('div')` 而div里面放入两个按钮使用div.appendChild插入按钮;

    3、最后将div按钮插入到 popperPaneRef.$el(select下拉框的DOM)里就可以了,在将element-button-mini的css按钮样式复制,添加到原生按钮身上;

    4、这里是无法使用el-button这种组件因为不解析,只能使用原生button,为其添加ele的样式;

    具体实现看代码 

    1. <script setup>
    2. import {reactive, ref} from "vue";
    3. const value3 = ref([])
    4. const filterSelect = ref()
    5. let isShowSelect = ref(false)
    6. const options = reactive([
    7. {
    8. value: 'key-1',
    9. label: '今天',
    10. },
    11. {
    12. value: 'key-2',
    13. label: '明天',
    14. },
    15. {
    16. value: 'key-3',
    17. label: '后天',
    18. },
    19. {
    20. value: 'key-4',
    21. label: '昨天',
    22. },
    23. {
    24. value: 'key-5',
    25. label: '前天',
    26. },
    27. ])
    28. let tableData = ref([
    29. {
    30. date: '今天',
    31. name: 'Tom',
    32. address: 'No. 189, Grove St, Los Angeles',
    33. },
    34. {
    35. date: '明天',
    36. name: 'Tom',
    37. address: 'No. 189, Grove St, Los Angeles',
    38. },
    39. {
    40. date: '后天',
    41. name: 'Tom',
    42. address: 'No. 189, Grove St, Los Angeles',
    43. },
    44. {
    45. date: '昨天',
    46. name: 'Tom',
    47. address: 'No. 189, Grove St, Los Angeles',
    48. },
    49. {
    50. date: '前天',
    51. name: 'Tom222',
    52. address: 'No. 189, Grove St, Los Angeles',
    53. },
    54. ])
    55. const visibleChange = (visible) => {
    56. console.log(visible,filterSelect)
    57. // 下拉框显示隐藏
    58. if (visible ) {
    59. const ref = filterSelect.value
    60. // 拿到下拉选项的对象
    61. let popper = ref.popperPaneRef
    62. // 在拿到它的DOM元素
    63. if (popper.$el) popper = popper.$el
    64. // 判断是否有添加按钮
    65. if (!Array.from(popper.children).some(v => v.className === 'select-btn-box')) {
    66. // 插入按钮
    67. let el = document.createElement('div')
    68. let cancelBtn = document.createElement('button')
    69. let confirmBtn = document.createElement('button')
    70. el.className = 'select-btn-box'
    71. cancelBtn.className = 'select-cancel-mini-btn'
    72. confirmBtn.className = 'select-confirm-mini-btn'
    73. cancelBtn.innerText = '取消'
    74. confirmBtn.innerText = '确定'
    75. el.appendChild(cancelBtn)
    76. el.appendChild(confirmBtn)
    77. // 调用确认和取消函数
    78. cancelBtn.onclick = () => cancelHandle()
    79. confirmBtn.onclick = () => confirmHandle()
    80. popper.appendChild(el)
    81. }
    82. }
    83. }
    84. const cancelHandle = () => {
    85. console.log('取消',value3.value)
    86. isShowSelect.value = false
    87. }
    88. const confirmHandle = () => {
    89. console.log('确认',value3.value)
    90. isShowSelect.value = false
    91. }
    92. script>
    93. <style>
    94. .headers-slot {
    95. display: flex;
    96. align-items: center;
    97. justify-content: space-around;
    98. }
    99. /*
    100. 这些是按钮css,复制底层ele-btn-mini的
    101. */
    102. .custom-button {
    103. display: inline-block;
    104. line-height: 1;
    105. white-space: nowrap;
    106. cursor: pointer;
    107. background: #fff;
    108. color: #fff;
    109. -webkit-appearance: none;
    110. text-align: center;
    111. box-sizing: border-box;
    112. outline: 0;
    113. margin: 0;
    114. padding: 10px 15px;
    115. font-size: 14px;
    116. border-radius: 4px;
    117. }
    118. .select-btn-box{
    119. display: flex;
    120. border-top: 1px solid #dfe6ec;
    121. width: 100%;
    122. height: 44px;
    123. align-items: center;
    124. justify-content:flex-end ;
    125. }
    126. .select-cancel-mini-btn{
    127. display: inline-block;
    128. display: inline-block;
    129. line-height: 1;
    130. white-space: nowrap;
    131. cursor: pointer;
    132. background: #FFFFFF;
    133. border: 1px solid #DCDFE6;
    134. border-color: #DCDFE6;
    135. color: #606266;
    136. -webkit-appearance: none;
    137. text-align: center;
    138. -webkit-box-sizing: border-box;
    139. box-sizing: border-box;
    140. outline: none;
    141. margin: 0;
    142. -webkit-transition: 0.1s;
    143. transition: 0.1s;
    144. font-weight: 400;
    145. -moz-user-select: none;
    146. -webkit-user-select: none;
    147. -ms-user-select: none;
    148. padding: 7px 15px;
    149. font-size: 12px;
    150. border-radius: 3px;
    151. margin-right: 10px;
    152. }
    153. .select-cancel-mini-btn:hover{
    154. background-color:#e4f2fe ;
    155. color: #349cfb;
    156. border-color: #afd9fd;
    157. }
    158. .select-confirm-mini-btn:hover{
    159. background-color: #339bfa;
    160. }
    161. .select-confirm-mini-btn{
    162. display: inline-block;
    163. line-height: 1;
    164. white-space: nowrap;
    165. cursor: pointer;
    166. background: #fff;
    167. border: 1px solid #dcdfe6;
    168. -webkit-appearance: none;
    169. text-align: center;
    170. box-sizing: border-box;
    171. outline: none;
    172. margin: 0;
    173. transition: 0.1s;
    174. font-weight: 500;
    175. -moz-user-select: none;
    176. -webkit-user-select: none;
    177. -ms-user-select: none;
    178. padding: 12px 20px;
    179. font-size: 14px;
    180. border-radius: 4px;
    181. color: #fff;
    182. background-color: #0080f5;
    183. border-color: #409eff;
    184. padding: 7px 15px;
    185. font-size: 12px;
    186. border-radius: 3px;
    187. margin-right: 10px;
    188. }
    189. span{
    190. cursor: pointer;
    191. }
    192. style>

  • 相关阅读:
    第9章 读写文件
    我在绑定微信账号时出现了问题,提示该微信已绑定其他账号
    第十站:Java白——测试与调试的艺术
    jmeter——接口压测和性能监测实践
    Python读取postgresql数据库
    【JavaEE】网络编程(网络编程基础、Socket套接字)
    CDN的基本概念
    4.3 IAT Hook 挂钩技术
    miniob源码 架构概览
    【kubernetes篇】使用Harbor仓库管理kubernetes镜像
  • 原文地址:https://blog.csdn.net/qq_54753561/article/details/132982541