tki-tree.vue 11 KB

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