tki-tree.vue 11 KB

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