SelectTree.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!-- 树状选择器 -->
  2. <template>
  3. <div>
  4. <el-popover
  5. v-if="!disabled"
  6. class="selecttree-box"
  7. ref="popover"
  8. placement="bottom-start"
  9. trigger="click"
  10. @show="onShowPopover"
  11. @hide="onHidePopover"
  12. >
  13. <p class="select-tree-title">{{ selectTreeTitle }}</p>
  14. <el-scrollbar>
  15. <el-tree
  16. ref="tree"
  17. class="select-tree"
  18. highlight-current
  19. :style="`min-width: ${treeWidth}`"
  20. :data="data"
  21. :props="props"
  22. :expand-on-click-node="false"
  23. :filter-node-method="filterNode"
  24. :default-expand-all="true"
  25. @node-click="onClickNode"
  26. >
  27. </el-tree>
  28. </el-scrollbar>
  29. <el-input
  30. slot="reference"
  31. ref="input"
  32. v-model="labelModel"
  33. clearable
  34. :style="`width: ${width}px`"
  35. :class="{ rotate: showStatus }"
  36. suffix-icon="el-icon-arrow-down"
  37. :placeholder="placeholder"
  38. >
  39. </el-input>
  40. </el-popover>
  41. <div v-else>
  42. <el-input type="text" :disabled="disabled" :value="labelModel"></el-input>
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. name: 'SelectTree',
  49. props: {
  50. // 接收绑定参数
  51. value: String,
  52. // 输入框宽度
  53. width: String,
  54. // selectTreeTitle
  55. selectTreeTitle: String,
  56. // 选项数据
  57. options: {
  58. type: Array,
  59. required: true
  60. },
  61. // 输入框占位符
  62. placeholder: {
  63. type: String,
  64. required: false,
  65. default: '请选择'
  66. },
  67. // 树节点配置选项
  68. props: {
  69. type: Object,
  70. required: false,
  71. default: () => ({
  72. parent: 'parentId',
  73. value: 'id',
  74. label: 'label',
  75. children: 'children'
  76. })
  77. },
  78. disabled: {
  79. type: Boolean,
  80. required: false,
  81. default: () => {
  82. return false;
  83. }
  84. }
  85. },
  86. // 设置绑定参数
  87. model: {
  88. prop: 'value',
  89. event: 'selected'
  90. },
  91. computed: {
  92. // 是否为树状结构数据
  93. dataType() {
  94. const jsonStr = JSON.stringify(this.options);
  95. return jsonStr.indexOf(this.props.children) !== -1;
  96. },
  97. // 若非树状结构,则转化为树状结构数据
  98. data() {
  99. return this.dataType ? this.options : this.switchTree();
  100. }
  101. },
  102. watch: {
  103. labelModel(val) {
  104. if (this.disabled) {
  105. return;
  106. } else {
  107. if (!val) {
  108. this.valueModel = '';
  109. }
  110. this.$refs.tree.filter(val);
  111. }
  112. },
  113. value(val) {
  114. this.labelModel = this.queryTree(this.data, val);
  115. }
  116. },
  117. data() {
  118. return {
  119. // 树状菜单显示状态
  120. showStatus: false,
  121. // 菜单宽度
  122. treeWidth: 'auto',
  123. // 输入框显示值
  124. labelModel: '',
  125. // 实际请求传值
  126. valueModel: ''
  127. };
  128. },
  129. created() {
  130. // 检测输入框原有值并显示对应 label
  131. if (this.value) {
  132. this.labelModel = this.queryTree(this.data, this.value);
  133. }
  134. if (this.disabled) {
  135. return;
  136. } else {
  137. // 获取输入框宽度同步至树状菜单宽度
  138. this.$nextTick(() => {
  139. this.treeWidth = `${(this.width || this.$refs.input.$refs.input.clientWidth) - 24}px`;
  140. });
  141. }
  142. },
  143. methods: {
  144. // 单击节点
  145. onClickNode(node) {
  146. this.labelModel = node[this.props.label];
  147. this.valueModel = node[this.props.value];
  148. this.onCloseTree();
  149. },
  150. // 偏平数组转化为树状层级结构
  151. switchTree() {
  152. return this.cleanChildren(this.buildTree(this.options, '0'));
  153. },
  154. // 隐藏树状菜单
  155. onCloseTree() {
  156. this.$refs.popover.showPopper = false;
  157. },
  158. // 显示时触发
  159. onShowPopover() {
  160. // if(this.disabled)
  161. this.showStatus = true;
  162. this.$refs.tree.filter(false);
  163. },
  164. // 隐藏时触发
  165. onHidePopover() {
  166. this.showStatus = false;
  167. this.$emit('selected', this.valueModel);
  168. this.$emit('selectedLable', this.labelModel);
  169. },
  170. // 树节点过滤方法
  171. filterNode(query, data) {
  172. if (!query) return true;
  173. return data[this.props.label].indexOf(query) !== -1;
  174. },
  175. // 搜索树状数据中的 ID
  176. queryTree(tree, id) {
  177. let stark = [];
  178. stark = stark.concat(tree);
  179. while (stark.length) {
  180. const temp = stark.shift();
  181. if (temp[this.props.children]) {
  182. stark = stark.concat(temp[this.props.children]);
  183. }
  184. if (temp[this.props.value] === id) {
  185. return temp[this.props.label];
  186. }
  187. }
  188. return '';
  189. },
  190. // 将一维的扁平数组转换为多层级对象
  191. buildTree(data, id = '0') {
  192. const fa = (parentId) => {
  193. const temp = [];
  194. for (let i = 0; i < data.length; i++) {
  195. const n = data[i];
  196. if (n[this.props.parent] === parentId) {
  197. n.children = fa(n.rowGuid);
  198. temp.push(n);
  199. }
  200. }
  201. return temp;
  202. };
  203. return fa(id);
  204. },
  205. // 清除空 children项
  206. cleanChildren(data) {
  207. const fa = (list) => {
  208. list.map((e) => {
  209. if (e.children.length) {
  210. fa(e.children);
  211. } else {
  212. delete e.children;
  213. }
  214. return e;
  215. });
  216. return list;
  217. };
  218. return fa(data);
  219. }
  220. }
  221. };
  222. </script>
  223. <style>
  224. .selecttree-box .el-input.el-input--suffix {
  225. cursor: pointer;
  226. overflow: hidden;
  227. }
  228. .el-input.el-input--suffix.rotate .el-input__suffix {
  229. transform: rotate(180deg);
  230. }
  231. .select-tree {
  232. max-height: 350px;
  233. overflow-y: scroll;
  234. }
  235. /* 菜单滚动条 */
  236. .select-tree::-webkit-scrollbar {
  237. z-index: 11;
  238. width: 6px;
  239. }
  240. .select-tree::-webkit-scrollbar-track,
  241. .select-tree::-webkit-scrollbar-corner {
  242. background: #fff;
  243. }
  244. .select-tree::-webkit-scrollbar-thumb {
  245. border-radius: 5px;
  246. width: 6px;
  247. background: #b4bccc;
  248. }
  249. .select-tree::-webkit-scrollbar-track-piece {
  250. background: #fff;
  251. width: 6px;
  252. }
  253. .select-tree-title {
  254. background: #f7fbff;
  255. height: 40px;
  256. line-height: 40px;
  257. padding-left: 8px;
  258. }
  259. </style>