PropertyTreeSelect.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. console.log(val, oldval);
  130. if (val !== oldval) {
  131. this.resetData();
  132. this.emitChange();
  133. this.getList();
  134. }
  135. },
  136. },
  137. mounted() {
  138. this.getList();
  139. },
  140. methods: {
  141. resetData() {
  142. this.$refs.PropertyTree.setCheckedKeys([]);
  143. this.selectedPropName = "";
  144. this.selectedPropList = [];
  145. this.selectedPropIds = [];
  146. this.visible = false;
  147. this.isFocus = false;
  148. this.inputHovering = false;
  149. this.proptree = [];
  150. },
  151. async getList() {
  152. if (!this.courseId) return;
  153. const res = await propertyNameQueryApi(this.courseId);
  154. let propList = res.data || [];
  155. const fetchAll = propList.map((item) => propertyTreeQueryApi(item.id));
  156. const datas = await Promise.all(fetchAll).catch(() => {});
  157. if (!datas) return;
  158. this.proptree = propList.map((item, index) => {
  159. let nitem = { ...item, disabled: true };
  160. nitem.propertyList = datas[index].data;
  161. return nitem;
  162. });
  163. if (this.value) this.initSelected(this.value);
  164. },
  165. switchOpen() {
  166. if (this.visible) {
  167. this.handleClose();
  168. } else {
  169. this.handleOpen();
  170. }
  171. },
  172. handleOpen() {
  173. this.isFocus = true;
  174. setTimeout(() => {
  175. this.visible = true;
  176. }, 100);
  177. },
  178. handleClose() {
  179. this.visible = false;
  180. this.isFocus = false;
  181. },
  182. initSelected(val) {
  183. this.$refs.PropertyTree.setCheckedKeys(val);
  184. },
  185. handleClearClick(event) {
  186. event.stopPropagation();
  187. this.$refs.PropertyTree.setCheckedKeys([]);
  188. this.selectedPropName = "";
  189. this.emitChange();
  190. this.handleClose();
  191. },
  192. checkChange() {
  193. const nodes = this.$refs.PropertyTree.getCheckedNodes();
  194. this.selectedPropName = nodes.map((item) => item.name).join(";");
  195. this.emitChange();
  196. },
  197. getCheckedPropertyInfos() {
  198. let propertyInfos = [];
  199. const getProperty = (propertyList, parents) => {
  200. propertyList.forEach((item) => {
  201. let nitem = Object.assign({}, item, { propertyList: null });
  202. let nparents = [...parents, nitem];
  203. if (this.selectedPropIds.includes(item.id)) {
  204. propertyInfos.push(nparents);
  205. }
  206. if (item.propertyList && item.propertyList.length) {
  207. getProperty(item.propertyList, nparents);
  208. }
  209. });
  210. };
  211. getProperty(this.proptree, []);
  212. return propertyInfos;
  213. },
  214. emitChange() {
  215. this.selectedPropIds = this.$refs.PropertyTree.getCheckedKeys();
  216. const propertyInfos = this.getCheckedPropertyInfos();
  217. this.$emit("input", this.selectedPropIds);
  218. this.$emit("change", propertyInfos);
  219. },
  220. },
  221. };
  222. </script>
  223. <style lang="scss">
  224. .property-popover {
  225. &.el-popover {
  226. width: 300px;
  227. padding: 0;
  228. }
  229. &-tree {
  230. max-height: 300px;
  231. overflow: auto;
  232. padding: 10px;
  233. }
  234. }
  235. </style>