checkbox.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div v-loading="loading" :element-loading-background="elementLoadingBackground">
  3. <el-checkbox-group
  4. v-bind="$attrs"
  5. v-model="paramVal"
  6. @change="handleChange"
  7. >
  8. <el-checkbox
  9. v-for="item in options"
  10. :label="item[valueKey]"
  11. >
  12. {{ item[labelKey] }}
  13. </el-checkbox>
  14. </el-checkbox-group>
  15. </div>
  16. </template>
  17. <script lang="ts">
  18. import {
  19. defineComponent,
  20. computed,
  21. onMounted,
  22. ref,
  23. reactive,
  24. watch,
  25. getCurrentInstance,
  26. ComponentInternalInstance,
  27. toRefs,
  28. nextTick,
  29. inject
  30. } from 'vue'
  31. import {useStore} from 'vuex'
  32. import {useRouter, useRoute} from 'vue-router'
  33. export default defineComponent({
  34. name: '',
  35. components: {},
  36. props: {
  37. param: {},
  38. label: {},
  39. options: { type: Array, default: () => [] },
  40. labelKey: { type: String, default: 'dictLabel' },
  41. valueKey: { type: String, default: 'dictValue' },
  42. static: {default: false}
  43. },
  44. setup(props, { emit }) {
  45. const store = useStore();
  46. const router = useRouter();
  47. const route = useRoute();
  48. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  49. const state = reactive({
  50. paramVal: props.param,
  51. loading: true,
  52. elementLoadingBackground: inject('element-loading-background', null)
  53. })
  54. watch(() => state.paramVal, (n) => {
  55. emit('emitParam', n)
  56. })
  57. watch(() => props.param, (n) => {
  58. state.paramVal = n
  59. })
  60. watch(() => [props.options, props.static], () => {
  61. state.loading = false
  62. })
  63. const optionsMapCpt = computed(() => {
  64. const map = new Map()
  65. props.options?.forEach((v: any) => {
  66. map.set(v[props.valueKey], v)
  67. })
  68. return map
  69. })
  70. const handleChange = (val: any) => {
  71. emit('getObject', val.map((v: any) => optionsMapCpt.value.get(v)))
  72. }
  73. onMounted(() => {
  74. if (props.static !== false || props.options?.length > 0) {
  75. state.loading = false
  76. }
  77. })
  78. return {
  79. ...toRefs(state),
  80. optionsMapCpt,
  81. handleChange
  82. }
  83. },
  84. })
  85. </script>
  86. <style scoped lang="scss">
  87. </style>