SelectOrgs.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div
  3. :class="[
  4. 'select-orgs part-box part-box-pad part-box-border',
  5. { 'select-orgs-disabled': disabled },
  6. ]"
  7. >
  8. <el-tree
  9. :data="orgs"
  10. show-checkbox
  11. default-expand-all
  12. node-key="id"
  13. ref="MenuTree"
  14. :props="defaultProps"
  15. :check-strictly="checkStrictly"
  16. check-on-click-node
  17. :expand-on-click-node="false"
  18. @check="checkClick"
  19. @check-change="checkChange"
  20. >
  21. </el-tree>
  22. </div>
  23. </template>
  24. <script>
  25. import { organizationList } from "../api";
  26. export default {
  27. name: "select-orgs",
  28. props: {
  29. value: {
  30. type: Array,
  31. default() {
  32. return [];
  33. },
  34. },
  35. multiple: {
  36. type: Boolean,
  37. default: true,
  38. },
  39. disabled: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. checkStrictly: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. },
  48. data() {
  49. return {
  50. selectedOrgs: [],
  51. // orgs: [],
  52. orgs: [],
  53. leafOrgIds: [],
  54. defaultProps: {
  55. label: "name",
  56. },
  57. };
  58. },
  59. created() {
  60. this.getList();
  61. },
  62. watch: {
  63. value(val, oldVal) {
  64. if (val.join() !== oldVal.join()) this.setCheckedNode(val);
  65. },
  66. },
  67. methods: {
  68. async getList() {
  69. const orgs = await organizationList();
  70. this.orgs = orgs || [];
  71. if (this.orgs.length) {
  72. this.orgs[0].children.sort((a, b) => {
  73. if (a.type === "PRINTING_HOUSE") return 1;
  74. if (b.type === "PRINTING_HOUSE") return -1;
  75. return 0;
  76. });
  77. }
  78. let leafOrgIds = [];
  79. const getLeafOrg = (orgs) => {
  80. orgs.forEach((org) => {
  81. org.disabled = false;
  82. if (org["children"] && org["children"].length) {
  83. getLeafOrg(org.children);
  84. } else {
  85. leafOrgIds.push(org.id);
  86. }
  87. });
  88. };
  89. getLeafOrg(this.orgs);
  90. this.leafOrgIds = leafOrgIds;
  91. this.$nextTick(() => {
  92. this.setCheckedNode(this.value);
  93. });
  94. },
  95. setDisabledOrgs(disabledOrgIds) {
  96. const updateInfo = (orgs) => {
  97. orgs.forEach((org) => {
  98. org.disabled = disabledOrgIds.includes(org.id);
  99. if (org["children"] && org["children"].length) {
  100. updateInfo(org.children);
  101. }
  102. });
  103. };
  104. updateInfo(this.orgs);
  105. },
  106. setCheckedNode(selectedIds) {
  107. if (this.multiple) {
  108. const leafSelectedIds = selectedIds.filter((id) =>
  109. this.leafOrgIds.includes(id)
  110. );
  111. this.$refs.MenuTree.setCheckedKeys(leafSelectedIds);
  112. return;
  113. }
  114. const selectedOrgs = this.$refs.MenuTree.getCheckedKeys();
  115. if (selectedOrgs.join() === selectedIds.join()) return;
  116. this.$refs.MenuTree.setCheckedKeys(selectedIds);
  117. },
  118. getCheckedNode() {
  119. return this.$refs.MenuTree.getCheckedKeys();
  120. },
  121. checkChange() {
  122. if (!this.multiple) return;
  123. const halfCheckedIds = this.$refs.MenuTree.getHalfCheckedKeys();
  124. const checkedIds = this.$refs.MenuTree.getCheckedKeys();
  125. const checkedOrgs = [...halfCheckedIds, ...checkedIds];
  126. this.emitChange(checkedOrgs);
  127. },
  128. checkClick(data) {
  129. if (this.multiple) return;
  130. const checkedIds = this.$refs.MenuTree.getCheckedKeys();
  131. // console.log(checkedIds);
  132. // console.log(data.id);
  133. if (!checkedIds.includes(data.id)) {
  134. this.emitChange([]);
  135. return;
  136. }
  137. const selectedOrgs = [data.id];
  138. this.$refs.MenuTree.setCheckedKeys([data.id]);
  139. this.emitChange(selectedOrgs);
  140. },
  141. emitChange(vals) {
  142. this.$emit("input", vals);
  143. this.$emit("change", vals);
  144. },
  145. },
  146. };
  147. </script>