• 基于el-select简单封装一个组件


    主要的功能有:

                     1.全选

                     2.传入禁用的ID,把对应是数据禁用

    子组件

    1. <script>
    2. import { cloneDeep } from '@mcfe/tools';
    3. export default {
    4. name: 'SelectAll',
    5. props: {
    6. // v-model绑定的值
    7. value: {
    8. type: [String, Number, Array],
    9. default: '',
    10. },
    11. labelKey: {
    12. type: [String, Function],
    13. default: 'label',
    14. },
    15. valueKey: {
    16. type: String,
    17. default: 'value',
    18. },
    19. // 数据源
    20. source: {
    21. type: [Array, String],
    22. default: () => ([]),
    23. },
    24. // 是否有全选功能
    25. showSelectAll: {
    26. type: Boolean,
    27. default: false,
    28. },
    29. // 禁止的属性
    30. disabledIds: {
    31. type: Array,
    32. default: () => ([]),
    33. },
    34. },
    35. data() {
    36. return {
    37. selectedArray: [],
    38. options: this.source,
    39. isSelectAll: false, // 添加isSelectAll属性
    40. };
    41. },
    42. watch: {
    43. disabledIds: {
    44. deep: true,
    45. immediate: true,
    46. handler() {
    47. this.options = this.disabledList(this.options);
    48. },
    49. },
    50. },
    51. methods: {
    52. // 点击全选时
    53. selectAll() {
    54. if (this.showSelectAll) {
    55. // 如果isSelectAll为true,则清空selectedArray,实现取消全选
    56. if (this.isSelectAll) {
    57. this.selectedArray = [];
    58. this.isSelectAll = false; // 设置isSelectAll为false
    59. } else {
    60. // 否则,将options中非禁止选择的value添加到selectedArray中,实现全选
    61. this.selectedArray = this.options.filter(({ disabled }) => !disabled).map(({ value }) => value);
    62. this.selectedArray.unshift('全选');
    63. // 设置isSelectAll为true
    64. this.isSelectAll = true;
    65. }
    66. }
    67. this.$emit('input', this.selectedArray);
    68. },
    69. // 选中的值发生变化
    70. changeSelect(val) {
    71. if (this.showSelectAll) {
    72. if (typeof val === 'number') {
    73. console.warn('单选的时候就不要传showSelectAll了.');
    74. return;
    75. }
    76. // 获取非禁止选择的数据的长度
    77. const enabledOptionsLength = this.options.filter(({ disabled }) => !disabled).length;
    78. // 如果选中的值不包括全选且选中值的长度和非禁止选择的数据的长度相等,证明这时候已经全选了,把全选追加到选中的数组中
    79. if (!val.includes('全选') && val.length === enabledOptionsLength) {
    80. this.selectedArray.unshift('全选');
    81. } else if (val.includes('全选') && (val.length - 1) < enabledOptionsLength) {
    82. // 当全选时,删除了一项数据
    83. this.selectedArray = this.selectedArray.filter((item) => item !== '全选');
    84. }
    85. }
    86. this.$emit('input', this.selectedArray);
    87. },
    88. // 全选的时候, 如果点击删除全选就把选中的数据清空
    89. removeTag(val) {
    90. if (this.showSelectAll && val === '全选') {
    91. this.selectedArray = [];
    92. }
    93. this.$emit('input', this.selectedArray);
    94. },
    95. // 禁用列表
    96. disabledList(list = []) {
    97. const { disabledIds = [], labelKey, valueKey } = this;
    98. // 使用 cloneDeep 深拷贝列表数据,避免修改原数据
    99. // 使用 map 方法遍历列表数据,对每一项进行格式化处理
    100. const stackList = cloneDeep(list).map((item) => {
    101. const stack = {};
    102. // 如果 disabledIds 存在,且为数组,且包含当前项的 valueKey,则将当前项的 disabled 属性设为 true
    103. if (disabledIds && Array.isArray(disabledIds) && disabledIds.includes(item[valueKey])) {
    104. stack.disabled = true;
    105. }
    106. // 根据 labelKey 的类型,如果是函数则调用函数获取 label,否则直接从 item 中获取 label
    107. const label = (typeof labelKey === 'function') ? labelKey(item) : item[labelKey];
    108. // 返回格式化后的当前项,包含 disabled,label,value 属性
    109. return {
    110. ...stack,
    111. label,
    112. value: item[valueKey],
    113. };
    114. });
    115. // 返回格式化后的列表数据
    116. return stackList;
    117. },
    118. },
    119. };
    120. script>

    父组件

    1. v-model="poiIdList"
    2. show-select-all
    3. collapse-tags
    4. clearable
    5. filterable
    6. :label-key="labelKey"
    7. multiple
    8. :disabled-ids="disabledIds"
    9. :source="PayTypeEnum"
    10. placeholder="请选择xxxx"
    11. />
    12. // 要禁用的属性
    13. disabledIds: [1],
    14. // 自定义labelKey
    15. labelKey(item) {
    16. return `${item.label}[(${item.value})]`;
    17. },
    18. // 数据源
    19. PayTypeEnum: [
    20. { label: '1转账', value: 1 },
    21. { label: '2支票', value: 2 },
    22. { label: '3转账', value: 3 },
    23. { label: '4支票', value: 4 },
    24. { label: '5转账', value: 5 },
    25. { label: '6支票', value: 6 },
    26. { label: '7转账', value: 7 },
    27. { label: '8支票', value: 8 },
    28. { label: '9转账', value: 9 },
    29. { label: '10支票', value: 10 },
    30. { label: '11转账', value: 11 },
    31. ]

  • 相关阅读:
    离线安装/断网安装python第三方库
    Linux基本指令
    关于 K8s 的一些基础概念整理
    Echarts异步数据与动画加载
    基于springboot和mybatis的RealWorld后端项目实战一之hello-springboot
    华为米家智能家居背后一些你不了解的小细节
    Ceph集群部署
    算法----滑动窗口
    Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
    RabbitMQ系列【4】六大工作模式
  • 原文地址:https://blog.csdn.net/weixin_49035434/article/details/132987145