当前位置:   article > 正文

二次封装element-plus的el-select组件_element-plus select远程二次封装

element-plus select远程二次封装

因为 element-plus的Virtualized Select 虚拟化选择器(el-select-v2)全选有bug

故封装了普通版el-select 并记录

上图

1.子组件

  1. <el-select v-model="selected" filterable multiple clearable collapse-tags collapse-tags-tooltip :collapse-tags-tooltip-placement="tooltipPlacement" :placeholder="placeholder" :props="props.props" popper-class="custom-header" :max-collapse-tags="maxCollapseTags" style="width: 240px">
  2. <template #header>
  3. <el-checkbox v-model="isAllChecked" :indeterminate="isIndeterminate" @change="handleCheckAll">全选</el-checkbox>
  4. </template>
  5. <el-option v-for="item in options" :key="item.id" :label="item.title" :value="item.id" />
  6. </el-select>

js部分:

  1. <script setup lang="ts">
  2. import { ref, watch, onMounted } from 'vue';
  3. import type { CheckboxValueType } from 'element-plus';
  4. // 定义组件接收的属性
  5. const props = defineProps({
  6. value: {
  7. type: Array,
  8. default: () => [],
  9. },
  10. options: {
  11. default() {
  12. return []
  13. },
  14. type: Array as () => Array<{ id: number, title: string }>,
  15. },
  16. placeholder: {
  17. type: String,
  18. default: '请选择',
  19. },
  20. maxCollapseTags: {
  21. type: Number,
  22. default: 3,
  23. },
  24. tooltipPlacement: {
  25. type: String,
  26. default: 'top',
  27. },
  28. props: {
  29. type: Object,
  30. default: () => ({
  31. label: 'title',
  32. value: 'id',
  33. }),
  34. },
  35. });
  36. onMounted(()=>{
  37. updateCheckAllStatus()
  38. })
  39. // 定义组件发出的事件
  40. const emit = defineEmits(['update:value']);
  41. // 定义组件内部状态
  42. const selected = ref<any>(props.value);
  43. const isAllChecked = ref(false);
  44. const isIndeterminate = ref(false);
  45. // 监听 value 的变化
  46. watch(
  47. () => props.value,
  48. (newVal) => {
  49. selected.value = newVal;
  50. console.log(selected.value);
  51. updateCheckAllStatus();
  52. },
  53. );
  54. // 处理全选逻辑
  55. const handleCheckAll = (val: CheckboxValueType) => {
  56. if (isAllChecked.value) {
  57. selected.value = props.options.map((option: any) => option.id);
  58. } else {
  59. selected.value = [];
  60. }
  61. emit('update:value', selected.value);
  62. };
  63. // 更新全选状态
  64. const updateCheckAllStatus = () => {
  65. if (selected.value.length === props.options.length) {
  66. isAllChecked.value = true;
  67. isIndeterminate.value = false;
  68. } else if (selected.value.length > 0) {
  69. isAllChecked.value = false;
  70. isIndeterminate.value = true;
  71. } else {
  72. isAllChecked.value = false;
  73. isIndeterminate.value = false;
  74. }
  75. };
  76. // 监听 selected 的变化,同步给父组件
  77. watch(selected, (newVal) => {
  78. emit('update:value', newVal);
  79. updateCheckAllStatus();
  80. });
  81. </script>

2.父组件:

<ElSelectV1 class="ml-1" :value="selectedValues" :options="options" :params="params" @update:value="handleValueUpdate" :placeholder="'请选择任务'"></ElSelectV1>

js部分:

  1. <script setup lang="ts">
  2. import ElSelectV1 from '@/components/Selector/index.vue';
  3. const selectedValues = ref<any>();
  4. const options = ref<any>([]);
  5. let params = reactive<any>({});
  6. // 处理子组件发出的更新事件
  7. function handleValueUpdate(newValues: string[]) {
  8. selectedValues.value = newValues;
  9. }

selectedValues:选择器已选中内容的id

options:选择器中所有的内容 (lable:展示名字,id:。。。)子组件中可修改

placeholder:输入框中的提示语。 默认为‘请选择’

handleValueUpdate:子组件更新后的回调函数

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

闽ICP备14008679号