当前位置:   article > 正文

Element Ui 下拉选择框内追加添加按钮,点击可以追加新的选项内容_elementui下拉按钮

elementui下拉按钮

功能需求,在下拉选项框内,追加一个添加的按钮,点击可以添加新的被选项内容;

效果展示如下:

代码实现封装组件:

  1. <template>
  2. <span>
  3. <el-cascader
  4. ref="cascader"
  5. v-model="selectedOptions"
  6. size="medium"
  7. style="width:100%"
  8. :props="defaultParams"
  9. :options="brands"
  10. :clearable="true"
  11. @change="handleChange"
  12. @visible-change="v => visibleChange(v, 'cascader', categoriesClick, '添加品牌')"
  13. />
  14. <!-- 添加品牌 -->
  15. <add-brand :open-off="openOff" @changeAddclose="closeAdd" />
  16. </span>
  17. </template>
  18. <script>
  19. import { brandAllList } from '@/api/item/brand.js'
  20. import AddBrand from './components/addbrand.vue'
  21. export default {
  22. name: 'AboutAddBrand',
  23. components: {
  24. AddBrand
  25. },
  26. data() {
  27. return {
  28. brands: [], // 品牌选项
  29. selectedOptions: [], // 品牌被选
  30. defaultParams: { // 转品牌选项
  31. label: 'name',
  32. value: 'brand_id',
  33. children: 'children'
  34. },
  35. openOff: false // 添加品牌信息弹框
  36. }
  37. },
  38. async mounted() {
  39. await this.getBrand() // 品牌
  40. },
  41. methods: {
  42. /**
  43. * 获取品牌选项
  44. */
  45. getBrand() {
  46. this.brands = []
  47. brandAllList().then((res) => {
  48. if (res && res.code === 0 && res.data) {
  49. this.brands = res.data.list
  50. const selectId = this.findTargetId(this.brands[0])
  51. this.selectedOptions = selectId
  52. } else {
  53. this.$message({
  54. message: res.msg,
  55. type: 'danger'
  56. })
  57. }
  58. })
  59. },
  60. /**
  61. * 递归设置默认值
  62. */
  63. findTargetId(dataobj) {
  64. if (!dataobj.children) {
  65. return dataobj.brand_id
  66. } else {
  67. if (dataobj.children) {
  68. for (var i = 0; i < dataobj.children.length; i++) {
  69. var flagId = this.findTargetId(dataobj.children[i])
  70. if (flagId) {
  71. return flagId
  72. }
  73. }
  74. }
  75. }
  76. return false
  77. },
  78. // 商品品牌选中改变设置
  79. handleChange(value) {
  80. // console.log(value)
  81. this.$refs.cascader.dropDownVisible = false
  82. this.$emit('handleChange', value)
  83. },
  84. /**
  85. * 为element-ui的Select和Cascader添加弹层底部操作按钮
  86. * @param visible
  87. * @param refName 设定的ref名称
  88. * @param onClick 底部操作按钮点击监听
  89. * @param addname 底部按钮名称
  90. */
  91. visibleChange(visible, refName, onClick, addname) {
  92. if (visible) {
  93. const ref = this.$refs[refName]
  94. let popper = ref.$refs.popper
  95. if (popper.$el) popper = popper.$el
  96. if (!Array.from(popper.children).some(v => v.className === 'el-cascader-menu__list')) {
  97. const el = document.createElement('ul')
  98. el.className = 'el-cascader-menu__list'
  99. el.style = 'border-top: solid 1px #E4E7ED; padding:0; color: #606266;'
  100. el.innerHTML = `<li class="el-cascader-node text-center" style="height:50px;line-height: 50px">
  101. <span class="el-cascader-node__label"><i class="font-blue el-icon-plus"></i>` + addname + `</span>
  102. </li>`
  103. popper.appendChild(el)
  104. el.onclick = () => {
  105. // 底部按钮的点击事件 点击后想触发的逻辑也可以直接写在这
  106. onClick && onClick()
  107. // 以下代码实现点击后弹层隐藏 不需要可以删掉
  108. if (ref.toggleDropDownVisible) {
  109. ref.toggleDropDownVisible(false)
  110. } else {
  111. ref.visible = false
  112. }
  113. }
  114. }
  115. }
  116. },
  117. /**
  118. * 点击添加品牌
  119. */
  120. categoriesClick() {
  121. this.openOff = true
  122. },
  123. /**
  124. * 添加品牌
  125. */
  126. handleAddClick() {
  127. this.closeAdd(true)
  128. },
  129. /** 关闭 */
  130. closeAdd(status) {
  131. this.openOff = status
  132. this.getBrand()
  133. }
  134. }
  135. }
  136. </script>

使用组件:

  1. <el-form-item label="商品品牌">
  2. <about-add-brand ref="brandInfo" @handleChange="handleBrandChanges" />
  3. </el-form-item>
  4. <script>
  5. // ... 其他内容略过
  6. import AboutAddBrand from './components/add/brandadd.vue'
  7. components: {
  8. AboutAddBrand,
  9. },
  10. data(){
  11. return {
  12. itemForm:{
  13. brand_id:''
  14. }
  15. }
  16. },
  17. // 商品品牌选中改变设置
  18. handleBrandChanges(value) {
  19. // eslint-disable-next-line eqeqeq
  20. this.itemForm.brand_id = value[0]
  21. },
  22. </script>

注:添加品牌是弹框表单添加的内容;

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号