当前位置:   article > 正文

select下拉菜单自由扩展-输入框添加/options删除_ant a-select下拉列表内增加删除

ant a-select下拉列表内增加删除

背景
antdv select 官方使用dropdownRender对下拉菜单进行自由扩展

效果展示

可添加,删除

代码

  1. <template>
  2. <div class="select-wrapper">
  3. <a-select v-model="value"
  4. style="width: 300px"
  5. dropdownClassName="dropdown-class-name"
  6. placeholder="请选择学历"
  7. :open="open"
  8. @select="selectOption"
  9. :class="{'ant-select-open': open}"
  10. option-label-prop="label"
  11. :filter-option="filterOption"
  12. show-search
  13. :disabled="disabled"
  14. >
  15. <div slot="dropdownRender" slot-scope="menu" @click="(e) => {e.stopPropagation()}">
  16. <v-nodes :vnodes="menu" />
  17. <a-divider style="margin: 4px 0" />
  18. <div style="padding: 4px 8px; cursor: pointer">
  19. <a-input class="input-txt" v-model="text" placeholder="请输入其他学历" allowClear @blur="addItem" />
  20. </div>
  21. </div>
  22. <a-select-option v-for="(item,index) in items" :key="item" :value="item" :label="item">
  23. {{ item }}
  24. <a-icon v-if="index > 2" style="position: absolute;top:30%;right: 10%;" type="delete" @click.stop="deleteItem(item)" />
  25. </a-select-option>
  26. </a-select>
  27. <!--原来的,因为有遮罩的div导致不能使用自带的搜索和删除
  28. <div class="select_overlap" @mousedown="openSelect"></div>-->
  29. <!--最新方案,展开后隐藏遮罩即可使用组件本身的功能-->
  30. <div v-show="!open" class="select_overlap" @mouseup="openSelect"></div>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. props: {
  36. value:{
  37. type: String,
  38. required: false
  39. },
  40. disabled:{
  41. type: Boolean,
  42. required: false,
  43. default: false
  44. },
  45. },
  46. components: {
  47. VNodes: {
  48. functional: true,
  49. render: (h, ctx) => ctx.props.vnodes
  50. }
  51. },
  52. data: () => ({
  53. items: ['大学本科', '硕士研究生','博士研究生'],
  54. // value: '',
  55. open: false,
  56. clickHandle: null,
  57. text: '',
  58. }),
  59. methods: {
  60. /**
  61. * 删除分组选项
  62. */
  63. deleteItem (label) {
  64. const index = this.items.indexOf(label)
  65. this.items.splice(index, 1)
  66. },
  67. addItem() {
  68. this.items.push(this.text)
  69. },
  70. openSelect() {
  71. this.open = !this.open
  72. },
  73. selectOption(value) {
  74. // 单选时设置
  75. this.$emit('change',value)
  76. this.open = false
  77. },
  78. filterOption(input, option) {
  79. return option.componentOptions.propsData.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
  80. },
  81. },
  82. mounted() {
  83. this.clickHandle = (e) => {
  84. if (e.target && 'className' in e.target && this.open) {
  85. const className = e.target.className;
  86. if (className.indexOf('select_overlap') === -1) {
  87. this.open = false
  88. }
  89. } else {
  90. this.open = false
  91. }
  92. }
  93. document.body.addEventListener('click', this.clickHandle)
  94. },
  95. beforeDestroy() {
  96. if (this.clickHandle) {
  97. document.body.removeEventListener('click', this.clickHandle)
  98. this.clickHandle = null
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped>
  104. .select-wrapper {
  105. position: relative;
  106. .select_overlap {
  107. cursor: pointer;
  108. height: 32px;
  109. width: 300px;
  110. position: absolute;
  111. top: 0;
  112. left: 0;
  113. right: 0;
  114. bottom: 0;
  115. opacity: 0;
  116. }
  117. }
  118. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/151714
推荐阅读
相关标签
  

闽ICP备14008679号