dept-select.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <div class="dept-select">
  3. <el-cascader
  4. ref="cascader"
  5. :value="value"
  6. :options="treeItems"
  7. :props="created"
  8. :size="size"
  9. @change="handleChange"
  10. @visible-change="handleVisibleChange"
  11. :placeholder="placeholder"
  12. clearable
  13. ></el-cascader>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'dept-select',
  19. props: {
  20. props: Object,
  21. size: { type: String, default: 'small' },
  22. navItems: Array,
  23. dataItems: Array,
  24. value: { required: false },
  25. placeholder: { type: String, default: '请选择用户部门' },
  26. },
  27. data() {
  28. return {
  29. cascaderProps: {
  30. multiple: false,
  31. checkStrictly: true,
  32. emitPath: false,
  33. children: 'children',
  34. label: 'name',
  35. value: 'id',
  36. },
  37. selectedValue: this.value,
  38. panelVisible: false,
  39. };
  40. },
  41. computed: {
  42. treeItems() {
  43. if (this.navItems) return this.navItems;
  44. const items = this.dataItems || [];
  45. const root = items.filter(a => !items.some(b => b.id === a.pid));
  46. const fetchChildren = item => {
  47. if (!item.path) item.path = [item.name];
  48. const children = items.filter(p => p.pid === item.id).map(p => fetchChildren({ ...p, parent: item, path: item.path.concat(p.name) }));
  49. if (children && children.length > 0) return { ...item, children };
  50. return { ...item, leaf: true };
  51. };
  52. return root.map(p => fetchChildren(p));
  53. },
  54. created() {
  55. const cascaderProps = { ...this.cascaderProps, ...(this.props || {}) };
  56. return cascaderProps;
  57. },
  58. },
  59. methods: {
  60. handleChange(payload) {
  61. this.selectedValue = payload;
  62. this.$emit('input', payload);
  63. this.$emit('change', payload);
  64. if (!this.panelVisible) {
  65. this.$emit('select', this.selectedValue);
  66. }
  67. },
  68. handleVisibleChange(visible) {
  69. this.panelVisible = visible;
  70. if (visible === false) {
  71. this.$emit('select', this.selectedValue);
  72. }
  73. },
  74. toggleDropDownVisible(visible) {
  75. this.$refs.cascader.toggleDropDownVisible(visible);
  76. },
  77. },
  78. };
  79. </script>
  80. <style lang="less" scoped>
  81. .el-cascader {
  82. width: 100%;
  83. }
  84. </style>