PropertyTreeSelect.vue 6.6 KB

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