PropertyTreeSelect.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div
  3. class="property-tree-select el-select el-select--small"
  4. @click="switchOpen"
  5. >
  6. <div
  7. :class="[
  8. 'el-input el-input--small 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-left"
  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. @check="handleCheck"
  62. >
  63. <span slot-scope="{ data }" :class="['custom-tree-node']">
  64. <span v-if="!data.code">{{ data.name }}</span>
  65. <span v-else>[{{ data.code }}]{{ data.name }}</span>
  66. </span>
  67. </el-tree>
  68. </div>
  69. </el-popover>
  70. </div>
  71. </template>
  72. <script>
  73. import { propertyTreeQueryApi, propertyNameQueryApi } from "../api";
  74. import Clickoutside from "element-ui/src/utils/clickoutside";
  75. export default {
  76. name: "PropertyTreeSelect",
  77. directives: { Clickoutside },
  78. props: {
  79. value: {
  80. type: [Array, String],
  81. default: "",
  82. },
  83. placeholder: { type: String, default: "请选择" },
  84. multiple: {
  85. type: Boolean,
  86. default: false,
  87. },
  88. disabled: {
  89. type: Boolean,
  90. default: false,
  91. },
  92. clearable: {
  93. type: Boolean,
  94. default: true,
  95. },
  96. courseId: {
  97. type: [String, Number],
  98. default: "",
  99. },
  100. },
  101. data() {
  102. return {
  103. selectedPropName: "",
  104. selectedPropList: [],
  105. selectedPropIds: [],
  106. visible: false,
  107. isFocus: false,
  108. inputHovering: false,
  109. proptree: [],
  110. defaultProps: {
  111. children: "propertyList",
  112. disabled: "disabled",
  113. },
  114. };
  115. },
  116. computed: {
  117. showClose() {
  118. let hasValue = this.multiple
  119. ? Array.isArray(this.value) && this.value.length > 0
  120. : this.value !== undefined && this.value !== null && this.value !== "";
  121. let criteria = this.clearable && this.inputHovering && hasValue;
  122. return criteria;
  123. },
  124. },
  125. watch: {
  126. value(val, oldval) {
  127. if (val !== oldval) this.initSelected(val);
  128. },
  129. courseId(val, oldval) {
  130. // console.log(val, oldval);
  131. if (val !== oldval) {
  132. this.resetData();
  133. this.emitChange();
  134. this.getList();
  135. }
  136. },
  137. },
  138. mounted() {
  139. this.getList();
  140. },
  141. methods: {
  142. resetData() {
  143. this.$refs.PropertyTree.setCheckedKeys([]);
  144. this.selectedPropName = "";
  145. this.selectedPropList = [];
  146. this.selectedPropIds = [];
  147. this.visible = false;
  148. this.isFocus = false;
  149. this.inputHovering = false;
  150. this.proptree = [];
  151. },
  152. async getList() {
  153. if (!this.courseId) return;
  154. const res = await propertyNameQueryApi(this.courseId);
  155. let propList = res.data || [];
  156. const fetchAll = propList.map((item) => propertyTreeQueryApi(item.id));
  157. const datas = await Promise.all(fetchAll).catch(() => {});
  158. if (!datas) return;
  159. this.proptree = propList.map((item, index) => {
  160. let nitem = { ...item, disabled: true, isCourseProperty: true };
  161. nitem.propertyList = datas[index].data;
  162. return nitem;
  163. });
  164. if (this.value) this.initSelected(this.value);
  165. },
  166. switchOpen() {
  167. if (this.visible) {
  168. this.handleClose();
  169. } else {
  170. this.handleOpen();
  171. }
  172. },
  173. handleOpen() {
  174. this.isFocus = true;
  175. setTimeout(() => {
  176. this.visible = true;
  177. }, 100);
  178. },
  179. handleClose() {
  180. this.visible = false;
  181. this.isFocus = false;
  182. },
  183. initSelected(val) {
  184. this.$refs.PropertyTree.setCheckedKeys(val);
  185. },
  186. handleClearClick(event) {
  187. event.stopPropagation();
  188. this.$refs.PropertyTree.setCheckedKeys([]);
  189. this.selectedPropName = "";
  190. this.emitChange();
  191. this.handleClose();
  192. },
  193. checkChange() {
  194. const nodes = this.$refs.PropertyTree.getCheckedNodes();
  195. this.selectedPropName = nodes.map((item) => item.name).join(";");
  196. this.emitChange();
  197. },
  198. handleCheck(nodeObj, tree) {
  199. let checkedCount = tree.checkedNodes.length;
  200. if (checkedCount > 3) {
  201. // 如果超出限制,则取消最后一次勾选
  202. this.$refs.PropertyTree.setChecked(nodeObj.id, false);
  203. this.$message.error("最多只能选择3个");
  204. }
  205. },
  206. getCheckedPropertyInfos(selectedPropIds = []) {
  207. let propertyInfos = [];
  208. const getProperty = (propertyList, parents) => {
  209. propertyList.forEach((item) => {
  210. let nitem = Object.assign({}, item, { propertyList: null });
  211. let nparents = [...parents, nitem];
  212. if (!item.isCourseProperty && selectedPropIds.includes(item.id)) {
  213. propertyInfos.push(nparents);
  214. }
  215. if (item.propertyList && item.propertyList.length) {
  216. getProperty(item.propertyList, nparents);
  217. }
  218. });
  219. };
  220. getProperty(this.proptree, []);
  221. return propertyInfos;
  222. },
  223. emitChange() {
  224. this.selectedPropIds = this.$refs.PropertyTree.getCheckedKeys();
  225. const propertyInfos = this.getCheckedPropertyInfos(this.selectedPropIds);
  226. this.$emit("input", this.selectedPropIds);
  227. this.$emit("change", propertyInfos);
  228. },
  229. },
  230. };
  231. </script>
  232. <style lang="scss">
  233. .property-popover {
  234. &.el-popover {
  235. width: 300px;
  236. padding: 0;
  237. }
  238. &-tree {
  239. max-height: 300px;
  240. overflow: auto;
  241. padding: 10px;
  242. }
  243. }
  244. </style>