select.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <div class="cl-dept-select">
  3. <el-tree-select
  4. v-model="value"
  5. node-key="id"
  6. :data="list"
  7. :props="{
  8. label: 'name',
  9. value: 'id',
  10. children: 'children'
  11. }"
  12. :multiple="multiple"
  13. :check-strictly="checkStrictly"
  14. :show-checkbox="multiple"
  15. default-expand-all
  16. @change="onChange"
  17. @check="onCheckChange"
  18. ></el-tree-select>
  19. </div>
  20. </template>
  21. <script lang="ts" name="cl-dept-select" setup>
  22. import { ElMessage } from "element-plus";
  23. import { onMounted, ref, useModel } from "vue";
  24. import { useCool } from "/@/cool";
  25. import { deepTree } from "/@/cool/utils";
  26. const props = defineProps({
  27. modelValue: [Array, Number, String],
  28. multiple: Boolean,
  29. checkStrictly: {
  30. type: Boolean,
  31. default: true
  32. }
  33. });
  34. const emit = defineEmits(["update:modelValue", "change"]);
  35. const { service } = useCool();
  36. const value = useModel(props, "modelValue");
  37. const list = ref();
  38. // 单选改变
  39. function onChange(val: string) {
  40. if (!props.multiple) {
  41. emit("update:modelValue", val);
  42. }
  43. }
  44. // 多选改变
  45. function onCheckChange(_: any, { checkedKeys }: any) {
  46. if (props.multiple) {
  47. emit("update:modelValue", checkedKeys);
  48. }
  49. }
  50. // 刷新
  51. function refresh() {
  52. service.base.sys.department
  53. .list()
  54. .then((res) => {
  55. list.value = deepTree(res);
  56. })
  57. .catch((err) => {
  58. list.value = [];
  59. ElMessage.error(err.message);
  60. });
  61. }
  62. onMounted(() => {
  63. refresh();
  64. });
  65. </script>
  66. <style lang="scss" scoped>
  67. .cl-dept-select {
  68. :deep(.el-select) {
  69. width: 100%;
  70. }
  71. }
  72. </style>