当前位置:   article > 正文

vue+elementui实现下拉框(el-select)增加checkbox并可全选或取消_el-select 多选 去掉输入框的 x

el-select 多选 去掉输入框的 x
  1. <template>
  2. <div>
  3. <el-select v-model="city" filterable clearable multiple collapse-tags :filter-method="dataFilter" placeholder="请选择">
  4. <el-checkbox v-model="allCheckList" :disabled="checkAllIsShow" class="el-select-dropdown__item el-select-allTop">全选</el-checkbox>
  5. <div class="allPosition">
  6. <el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.value">
  7. <span style="padding-left:20px;">{{ item.label }}</span>
  8. </el-option>
  9. </div>
  10. </el-select>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. city: [],
  18. cityList: [], //存储自定义城市搜索数据
  19. cityListCopy: [], //存储原始City数据
  20. checkAllIsShow: false //自定义搜索时-限制全选
  21. }
  22. },
  23. computed: {
  24. allCheckList: {
  25. get() {
  26. return this.cityList.every(item => this.city.indexOf(item.value) !== -1)
  27. },
  28. set(value) {
  29. this.city = []
  30. if (value) {
  31. this.cityList.forEach(item => {
  32. this.city.push(item.value)
  33. })
  34. }
  35. }
  36. }
  37. },
  38. created() {
  39. let arr = [
  40. {
  41. cityId: '-1',
  42. cityName: '全国'
  43. },
  44. {
  45. cityId: '1',
  46. cityName: '北京'
  47. },
  48. {
  49. cityId: '2',
  50. cityName: '上海'
  51. },
  52. {
  53. cityId: '3',
  54. cityName: '太原'
  55. },
  56. {
  57. cityId: '4',
  58. cityName: '新乡'
  59. },
  60. {
  61. cityId: '5',
  62. cityName: '永安'
  63. }
  64. ]
  65. //模拟请求
  66. setTimeout(() => {
  67. this.cityList = arr.map(item => {
  68. return {
  69. label: item.cityName,
  70. value: Number(item.cityId)
  71. }
  72. })
  73. this.cityListCopy = arr.map(item => {
  74. return {
  75. label: item.cityName,
  76. value: Number(item.cityId)
  77. }
  78. })
  79. }, 1000)
  80. },
  81. methods: {
  82. //自定义搜索方法
  83. dataFilter(val) {
  84. if (val) {
  85. this.checkAllIsShow = true
  86. this.cityList = this.cityList.filter(item => {
  87. return item.label.indexOf(val) !== -1
  88. })
  89. } else {
  90. this.checkAllIsShow = false
  91. this.cityList = this.cityListCopy
  92. }
  93. return true
  94. }
  95. }
  96. }
  97. </script>
  98. <style scoped>
  99. .el-select-allTop {
  100. width: 100%;
  101. border-bottom: 1px solid rgba(204, 204, 204, 0.596);
  102. }
  103. .allPosition {
  104. overflow: auto;
  105. height: 200px;
  106. }
  107. </style>

结果展示

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