PropertyTreeSelect.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <div
  3. class="property-tree-select el-select el-select--medium"
  4. @click="switchOpen"
  5. >
  6. <div
  7. :class="[
  8. 'el-input el-input--medium el-input--suffix',
  9. { 'is-focus': isFocus },
  10. ]"
  11. @mouseenter="inputHovering = true"
  12. @mouseleave="inputHovering = false"
  13. >
  14. <input
  15. ref="inputRef"
  16. type="text"
  17. autocomplete="off"
  18. :placeholder="placeholder"
  19. class="el-input__inner"
  20. :value="selectedPropName"
  21. readonly
  22. />
  23. <span class="el-input__suffix">
  24. <span class="el-input__suffix-inner">
  25. <i
  26. v-show="!showClose"
  27. :class="[
  28. 'el-select__caret',
  29. 'el-input__icon',
  30. 'el-icon-arrow-up',
  31. { 'is-reverse': visible },
  32. ]"
  33. ></i>
  34. <i
  35. v-if="showClose"
  36. class="el-select__caret el-input__icon el-icon-circle-close"
  37. @click="handleClearClick"
  38. ></i>
  39. </span>
  40. </span>
  41. </div>
  42. <el-popover
  43. v-model="visible"
  44. popper-class="property-popover"
  45. placement="bottom"
  46. trigger="manual"
  47. >
  48. <div ref="popoverRef" slot="reference"></div>
  49. <div v-clickoutside="handleClose" class="property-popover-tree">
  50. <el-tree
  51. ref="PropertyTree"
  52. :data="proptree"
  53. default-expand-all
  54. show-checkbox
  55. node-key="id"
  56. :props="defaultProps"
  57. :check-strictly="true"
  58. check-on-click-node
  59. :expand-on-click-node="false"
  60. @check-change="checkChange"
  61. >
  62. <span slot-scope="{ data }" :class="['custom-tree-node']">
  63. <span v-if="!data.code">{{ data.name }}</span>
  64. <span v-else>[{{ data.code }}]{{ data.name }}</span>
  65. </span>
  66. </el-tree>
  67. </div>
  68. </el-popover>
  69. </div>
  70. </template>
  71. <script>
  72. import { propertyTreeQueryApi, propertyNameQueryApi } from "../api";
  73. import Clickoutside from "element-ui/src/utils/clickoutside";
  74. export default {
  75. name: "PropertyTreeSelect",
  76. directives: { Clickoutside },
  77. props: {
  78. value: {
  79. type: [Array, String],
  80. default: "",
  81. },
  82. placeholder: { type: String, default: "请选择" },
  83. multiple: {
  84. type: Boolean,
  85. default: false,
  86. },
  87. disabled: {
  88. type: Boolean,
  89. default: false,
  90. },
  91. clearable: {
  92. type: Boolean,
  93. default: true,
  94. },
  95. courseId: {
  96. type: [String, Number],
  97. default: "",
  98. },
  99. },
  100. data() {
  101. return {
  102. selectedPropName: "",
  103. selectedPropList: [],
  104. selectedPropIds: [],
  105. visible: false,
  106. isFocus: false,
  107. inputHovering: false,
  108. proptree: [],
  109. defaultProps: {
  110. children: "propertyList",
  111. disabled: "disabled",
  112. },
  113. };
  114. },
  115. computed: {
  116. showClose() {
  117. let hasValue = this.multiple
  118. ? Array.isArray(this.value) && this.value.length > 0
  119. : this.value !== undefined && this.value !== null && this.value !== "";
  120. let criteria = this.clearable && this.inputHovering && hasValue;
  121. return criteria;
  122. },
  123. },
  124. watch: {
  125. value(val, oldval) {
  126. if (val !== oldval) this.initSelected(val);
  127. },
  128. courseId(val, oldval) {
  129. if (val !== oldval) {
  130. this.resetData();
  131. this.emitChange();
  132. this.getList();
  133. }
  134. },
  135. },
  136. methods: {
  137. resetData() {
  138. this.selectedPropName = "";
  139. this.selectedPropList = [];
  140. this.selectedPropIds = [];
  141. this.visible = false;
  142. this.isFocus = false;
  143. this.inputHovering = false;
  144. this.proptree = [];
  145. },
  146. async getList() {
  147. if (!this.courseId) return;
  148. const res = await propertyNameQueryApi(this.courseId);
  149. let propList = res.data || [];
  150. const fetchAll = propList.map((item) => propertyTreeQueryApi(item.id));
  151. const datas = await Promise.all(fetchAll).catch(() => {});
  152. if (!datas) return;
  153. this.proptree = propList.map((item, index) => {
  154. let nitem = { ...item, disabled: true };
  155. nitem.propertyList = datas[index].data;
  156. return nitem;
  157. });
  158. if (this.value) this.initSelected(this.value);
  159. },
  160. switchOpen() {
  161. if (this.visible) {
  162. this.handleClose();
  163. } else {
  164. this.handleOpen();
  165. }
  166. },
  167. handleOpen() {
  168. this.isFocus = true;
  169. setTimeout(() => {
  170. this.visible = true;
  171. }, 100);
  172. },
  173. handleClose() {
  174. this.visible = false;
  175. this.isFocus = false;
  176. },
  177. initSelected(val) {
  178. this.$refs.PropertyTree.setCheckedKeys(val);
  179. },
  180. handleClearClick(event) {
  181. event.stopPropagation();
  182. this.$refs.PropertyTree.setCheckedKeys([]);
  183. this.selectedPropName = "";
  184. this.emitChange();
  185. this.handleClose();
  186. },
  187. checkChange() {
  188. const nodes = this.$refs.PropertyTree.getCheckedNodes(true);
  189. this.selectedPropName = nodes.map((item) => item.name).join(";");
  190. this.emitChange();
  191. },
  192. emitChange() {
  193. this.selectedPropIds = this.$refs.PropertyTree.getCheckedKeys();
  194. this.$emit("input", this.selectedPropIds);
  195. this.$emit("change", this.selectedPropIds);
  196. },
  197. },
  198. };
  199. </script>
  200. <style lang="scss">
  201. .property-popover {
  202. &.el-popover {
  203. width: 300px;
  204. padding: 0;
  205. }
  206. &-tree {
  207. max-height: 300px;
  208. overflow: auto;
  209. padding: 10px;
  210. }
  211. }
  212. </style>