tki-tree.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template xlang="wxml">
  2. <view class="tki-tree">
  3. <view class="tki-tree-mask" :class="{ show: showTree }" @tap="_cancel"></view>
  4. <view class="tki-tree-cnt" :class="{ show: showTree }">
  5. <view class="tki-tree-bar">
  6. <view class="tki-tree-bar-cancel" :style="{ color: cancelColor }" hover-class="hover-c" @tap="_cancel">取消</view>
  7. <view class="tki-tree-bar-title" :style="{ color: titleColor }">{{ title }}</view>
  8. <view class="tki-tree-bar-confirm" :style="{ color: confirmColor }" hover-class="hover-c" @tap="_confirm">确定</view>
  9. </view>
  10. <view class="tki-tree-view">
  11. <scroll-view class="tki-tree-view-sc" :scroll-y="true">
  12. <block v-for="(item, index) in treeList" :key="index">
  13. <view
  14. class="tki-tree-item"
  15. :style="[
  16. {
  17. paddingLeft: item.rank * 15 + 'px',
  18. zIndex: item.rank * -1 + 50
  19. }
  20. ]"
  21. :class="{
  22. border: border === true,
  23. show: item.show,
  24. last: item.lastRank,
  25. showchild: item.showChild,
  26. open: item.open
  27. }"
  28. >
  29. <view class="tki-tree-label" @tap.stop="_treeItemTap(item, index)">
  30. <image class="tki-tree-icon" :src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon"></image>
  31. {{ item.name }}
  32. </view>
  33. <view class="tki-tree-check" @tap.stop="_treeItemSelect(item, index)" v-if="selectParent ? true : item.lastRank">
  34. <view class="tki-tree-check-yes" v-if="item.checked" :class="{ radio: !multiple }" :style="{ 'border-color': confirmColor }">
  35. <view class="tki-tree-check-yes-b" :style="{ 'background-color': confirmColor }"></view>
  36. </view>
  37. <view class="tki-tree-check-no" v-else :class="{ radio: !multiple }" :style="{ 'border-color': confirmColor }"></view>
  38. </view>
  39. </view>
  40. </block>
  41. </scroll-view>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. name: 'tki-tree',
  49. props: {
  50. range: {
  51. type: Array,
  52. default: function() {
  53. return [];
  54. }
  55. },
  56. idKey: {
  57. type: String,
  58. default: 'id'
  59. },
  60. rangeKey: {
  61. type: String,
  62. default: 'label'
  63. },
  64. title: {
  65. type: String,
  66. default: ''
  67. },
  68. multiple: {
  69. // 是否可以多选
  70. type: Boolean,
  71. default: false
  72. // default: true
  73. },
  74. selectParent: {
  75. //是否可以选父级
  76. type: Boolean,
  77. default: false
  78. },
  79. foldAll: {
  80. //折叠时关闭所有已经打开的子集,再次打开时需要一级一级打开
  81. type: Boolean,
  82. default: false
  83. },
  84. confirmColor: {
  85. // 确定按钮颜色
  86. type: String,
  87. default: '' // #07bb07
  88. },
  89. cancelColor: {
  90. // 取消按钮颜色
  91. type: String,
  92. default: '' // #757575
  93. },
  94. titleColor: {
  95. // 标题颜色
  96. type: String,
  97. default: '' // #757575
  98. },
  99. currentIcon: {
  100. // 展开时候的ic
  101. type: String,
  102. default:
  103. 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwg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f8FumX2q77ROvNXk8UGMEKdUz6tYJHljaZAbuyUH+UR1to5BEohTuqwPCeS4pAA/qY6o/kyHOAMCeRK3owJnj+rH1jjxhqpVsstaebCz6TmnHWyXyY+xHjSBWBY/bvSgadtXBj9u9KCN3rnIfkzkQVsTEEX0Y2IP2oKo/HhMICcFAThUcwVZNGU6FdbX/XURzkbVF4+ybGhjPrFdgP66QdXNurGtSdk6Xdb9nAJ8oDo3OQlsQZzkdPw41ONBo6vI5scDefRjZg+6gpg3Pxp50CXEvPjR2IOuIXL3oxUPuobI3Y9WPOgDIlc/WvOgL4iL/vqFCcD7LH0xB4hj7cfQ/fWH9qCT+FhG0tN+DBk1PzjOM0SVllixcsBT1AvYc/kAPhc0hRg/3uvxoCgKRN9+dOrBUBB9+9GpB0NC9OVH5x4MDdG1H714kANEV3705kEOEBf9dcPi/lQnsuvLg1wgSu3Ha0v7Uh4MMgUXeuG71H407a+VBy9CPQkOdw+MtB+nGbd/D+FBbhBNxo9SjwcngJjNj0E9yBFiFj8G9SBXiGn8GNyDnCEm8SMLD3KHGOdHNh7kDjHOj2w8mAeIi/5arX+c6b/fxHz9oADEdGdjR/fXCw/OOB5oVfCOgnepz8IB14PMw03jCmTE+QBx5z0gAmKSqK9OUF+hcAeIhu/QYr4Qie8rjW83hhMBERARQAREQAREBBABERCLnH8BBgA+TQI7U4t53AAAAABJRU5ErkJggg=='
  104. },
  105. defaultIcon: {
  106. // 折叠时候的ic
  107. type: String,
  108. default:
  109. 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAACE0lEQVR4Xu3c200DMRCF4XEltJAOkEugA+ggpUAHoQMqiFMCdEAJUMEiS4mEELlIO7bPOeN9i6K1rG/952myyea1WiCtXmEuYBPR4RBMxInoIOCwhOtJLKVszWyXc/5y2BvNEq6I+/3+kFK6M7OHnPM7jcLKjbZAvD/uaZtzflm5P4rbWyJWgDcze1LPuzVihfxUz7sH4ilJ2bx7Isrm3RtRMu8RiHJ5j0SUyXs0okTeCIj0eSMh0uaNhkiZNyIiXd7IiDR5oyNS5M2ACJ83EyJs3myIkHkzIsLlzYwIkzc7IkTeCojD81ZCHJa3GuKQvBURu+etjNgtb3XELnlHQGyedyTEZnlHQ2ySd0RE97wjI7rlHR3RJe+JeIrbLOecD6ePpZQ6W1kn2epo4MUrPOKyLN8ppYq1+y1VStncOjIdGnFZlo+U0uOtWOeOY2TE12Ouq//pEA7xXL7XfvcufR8K0Svfv6CREN3yDYfYIt9QiK3yjYTYLF95xB75SiP2ylcZsVu+cogj8pVCHJWvEuKwfOkREfKlRkTJlxkRJl86RMR8qRBR82VChM0XHpEhX2hElnyREWnyhUNkzBcKkTVfJETafIcjKuQ7FFEl35GIMvl2R1TMtyuiar49EWXzbY5oZpv/hibXTF2h3+s60FRKeT6+3TjMS3nrA3ZFRD8xrfY3ER1kJ+JEdBBwWGKeRAfEH1wS5WFZSDB/AAAAAElFTkSuQmCC'
  110. },
  111. lastIcon: {
  112. // 没有子集的ic
  113. type: String,
  114. default: ''
  115. },
  116. border: {
  117. // 是否有分割线
  118. type: Boolean,
  119. default: false
  120. }
  121. },
  122. data() {
  123. return {
  124. showTree: false,
  125. treeList: [],
  126. selectIndex: -1
  127. };
  128. },
  129. computed: {},
  130. methods: {
  131. _show() {
  132. this.showTree = true;
  133. },
  134. _hide() {
  135. this.showTree = false;
  136. },
  137. _cancel() {
  138. this._hide();
  139. this.$emit('cancel', '');
  140. },
  141. _confirm() {
  142. // 处理所选数据
  143. let rt = [],
  144. obj = {};
  145. this.treeList.forEach((v, i) => {
  146. if (this.treeList[i].checked) {
  147. obj = {};
  148. obj.parents = this.treeList[i].parents;
  149. obj = Object.assign(obj, this.treeList[i].source);
  150. // 移除子元素
  151. delete obj.children;
  152. rt.push(obj);
  153. }
  154. });
  155. this._hide();
  156. this.$emit('confirm', rt);
  157. },
  158. //扁平化树结构
  159. _renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
  160. list.forEach(item => {
  161. this.treeList.push({
  162. id: item[this.idKey],
  163. name: item[this.rangeKey],
  164. source: item,
  165. parentId, // 父级id数组
  166. parents, // 父级id数组
  167. rank, // 层级
  168. showChild: false, //子级是否显示
  169. open: false, //是否打开
  170. show: rank === 0, // 自身是否显示
  171. hideArr: [],
  172. orChecked: item.checked ? item.checked : false,
  173. checked: item.checked ? item.checked : false
  174. });
  175. if (Array.isArray(item.children) && item.children.length > 0) {
  176. // console.log(item)
  177. let parentid = [...parentId],
  178. parentArr = [...parents],
  179. childrenid = [...childrenid];
  180. delete parentArr.children;
  181. parentid.push(item[this.idKey]);
  182. parentArr.push({
  183. [this.idKey]: item[this.idKey],
  184. [this.rangeKey]: item[this.rangeKey]
  185. });
  186. this._renderTreeList(item.children, rank + 1, parentid, parentArr);
  187. } else {
  188. this.treeList[this.treeList.length - 1].lastRank = true;
  189. }
  190. });
  191. // console.log(list)
  192. },
  193. // 处理默认选择
  194. _defaultSelect() {
  195. this.treeList.forEach((v, i) => {
  196. if (v.checked) {
  197. this.treeList.forEach((v2, i2) => {
  198. if (v.parentId.toString().indexOf(v2.parentId.toString()) >= 0) {
  199. v2.show = true;
  200. if (v.parentId.includes(v2.id)) {
  201. v2.showChild = true;
  202. v2.open = true;
  203. }
  204. }
  205. });
  206. }
  207. });
  208. },
  209. // 点击
  210. _treeItemTap(item, index) {
  211. if (item.lastRank === true) {
  212. //点击最后一级时触发事件
  213. this.treeList[index].checked = !this.treeList[index].checked;
  214. this._fixMultiple(index);
  215. return;
  216. }
  217. let list = this.treeList;
  218. let id = item.id;
  219. item.showChild = !item.showChild;
  220. item.open = item.showChild ? true : !item.open;
  221. list.forEach((childItem, i) => {
  222. if (item.showChild === false) {
  223. //隐藏所有子级
  224. if (!childItem.parentId.includes(id)) {
  225. return;
  226. }
  227. if (!this.foldAll) {
  228. if (childItem.lastRank !== true && !childItem.open) {
  229. childItem.showChild = false;
  230. }
  231. // 为隐藏的内容添加一个标记
  232. if (childItem.show) {
  233. childItem.hideArr[item.rank] = id;
  234. }
  235. } else {
  236. if (childItem.lastRank !== true) {
  237. childItem.showChild = false;
  238. }
  239. }
  240. childItem.show = false;
  241. } else {
  242. // 打开子集
  243. if (childItem.parentId[childItem.parentId.length - 1] === id) {
  244. childItem.show = true;
  245. }
  246. // 打开被隐藏的子集
  247. if (childItem.parentId.includes(id) && !this.foldAll) {
  248. // console.log(childItem.hideArr)
  249. if (childItem.hideArr[item.rank] === id) {
  250. childItem.show = true;
  251. if (childItem.open && childItem.showChild) {
  252. console.log(childItem)
  253. childItem.showChild = true;
  254. } else {
  255. childItem.showChild = false;
  256. }
  257. childItem.hideArr[item.rank] = null;
  258. }
  259. // console.log(childItem.hideArr)
  260. }
  261. }
  262. });
  263. // console.log(this.treeList)
  264. },
  265. _treeItemSelect(item, index) {
  266. this.treeList[index].checked = !this.treeList[index].checked;
  267. this._fixMultiple(index);
  268. },
  269. // 处理单选多选
  270. _fixMultiple(index) {
  271. if (!this.multiple) {
  272. // 如果是单选
  273. this.treeList.forEach((v, i) => {
  274. if (i != index) {
  275. this.treeList[i].checked = false;
  276. } else {
  277. this.treeList[i].checked = true;
  278. }
  279. });
  280. }
  281. },
  282. // 重置数据
  283. _reTreeList() {
  284. this.treeList.forEach((v, i) => {
  285. this.treeList[i].checked = v.orChecked;
  286. });
  287. },
  288. _initTree(range = this.range) {
  289. this.treeList = [];
  290. this._renderTreeList(range);
  291. this.$nextTick(() => {
  292. this._defaultSelect(range);
  293. });
  294. }
  295. },
  296. watch: {
  297. range(list) {
  298. this._initTree(list);
  299. },
  300. multiple() {
  301. if (this.range.length) {
  302. this._reTreeList();
  303. }
  304. },
  305. selectParent() {
  306. if (this.range.length) {
  307. this._reTreeList();
  308. }
  309. }
  310. },
  311. mounted() {
  312. this._initTree();
  313. }
  314. };
  315. </script>
  316. <style scoped>
  317. @import './style.css';
  318. </style>