平安校园
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tki-tree.vue 13 KiB

2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
2 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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">取消
  7. </view>
  8. <view class="tki-tree-bar-title" :style="{'color':titleColor}">{{title}}</view>
  9. <view class="tki-tree-bar-confirm" :style="{'color':confirmColor}" hover-class="hover-c"
  10. @tap="_confirm">确定</view>
  11. </view>
  12. <view class="tki-tree-view">
  13. <scroll-view class="tki-tree-view-sc" :scroll-y="true">
  14. <view style="">
  15. <u-search height="90rpx" :inputStyle="{fontSize:'30rpx'}"
  16. v-model="searchText" placeholder="请输入" shape="square" :showAction="false"></u-search>
  17. </view>
  18. <block v-for="(item, index) in treeList" :key="index">
  19. <view v-if="(item[rangeKey].includes(searchText))" class="tki-tree-item" :style="[{
  20. paddingLeft: item.rank*15 + 'px',
  21. zIndex: item.rank*-1 +50
  22. }]" :class="{
  23. border: border === true,
  24. show: item.show,
  25. last: item.lastRank,
  26. showchild: item.showChild,
  27. open: item.open,
  28. }">
  29. <view class="tki-tree-label" @tap.stop="_treeItemTap(item, index)">
  30. <image class="tki-tree-icon"
  31. :src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon">
  32. </image>
  33. {{item.name}}
  34. </view>
  35. <view class="tki-tree-check" @tap.stop="_treeItemSelect(item, index)"
  36. v-if="selectParent?true:item.lastRank">
  37. <view class="tki-tree-check-yes" v-if="item.checked" :class="{'radio':!multiple}"
  38. :style="{'border-color':confirmColor}">
  39. <view class="tki-tree-check-yes-b" :style="{'background-color':confirmColor}">
  40. <u-icon name="checkbox-mark" color="#ffffff" size="16"></u-icon>
  41. </view>
  42. </view>
  43. <view class="tki-tree-check-no" v-else :class="{'radio':!multiple}"
  44. :style="{'border-color':confirmColor}"></view>
  45. </view>
  46. </view>
  47. </block>
  48. </scroll-view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. name: "tki-tree",
  56. props: {
  57. range: {
  58. type: Array,
  59. default: function() {
  60. return []
  61. }
  62. },
  63. idKey: {
  64. type: String,
  65. default: 'id'
  66. },
  67. rangeKey: {
  68. type: String,
  69. default: 'label'
  70. },
  71. title: {
  72. type: String,
  73. default: ''
  74. },
  75. multiple: { // 是否可以多选
  76. type: Boolean,
  77. default: false
  78. // default: true
  79. },
  80. selectParent: { //是否可以选父级
  81. type: Boolean,
  82. default: false
  83. },
  84. foldAll: { //折叠时关闭所有已经打开的子集,再次打开时需要一级一级打开
  85. type: Boolean,
  86. default: false
  87. },
  88. showChild: {//是否显示子级
  89. type: Boolean,
  90. default: false
  91. },
  92. confirmColor: { // 确定按钮颜色
  93. type: String,
  94. default: '' // #2388FF
  95. },
  96. cancelColor: { // 取消按钮颜色
  97. type: String,
  98. default: '' // #757575
  99. },
  100. titleColor: { // 标题颜色
  101. type: String,
  102. default: '' // #757575
  103. },
  104. currentIcon: { // 展开时候的ic
  105. type: String,
  106. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MEQ0QTM0MzQ1Q0RBMTFFOUE0MjY4NzI1Njc1RjI1ODIiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaW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G9SBXiGn8GNyDnCEm8SMLD3KHGOdHNh7kDjHOj2w8mAeIi/5arX+c6b/fxHz9oADEdGdjR/fXCw/OOB5oVfCOgnepz8IB14PMw03jCmTE+QBx5z0gAmKSqK9OUF+hcAeIhu/QYr4Qie8rjW83hhMBERARQAREQAREBBABERCLnH8BBgA+TQI7U4t53AAAAABJRU5ErkJggg=='
  107. },
  108. defaultIcon: { // 折叠时候的ic
  109. type: String,
  110. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAACE0lEQVR4Xu3c200DMRCF4XEltJAOkEugA+ggpUAHoQMqiFMCdEAJUMEiS4mEELlIO7bPOeN9i6K1rG/952myyea1WiCtXmEuYBPR4RBMxInoIOCwhOtJLKVszWyXc/5y2BvNEq6I+/3+kFK6M7OHnPM7jcLKjbZAvD/uaZtzflm5P4rbWyJWgDcze1LPuzVihfxUz7sH4ilJ2bx7Isrm3RtRMu8RiHJ5j0SUyXs0okTeCIj0eSMh0uaNhkiZNyIiXd7IiDR5oyNS5M2ACJ83EyJs3myIkHkzIsLlzYwIkzc7IkTeCojD81ZCHJa3GuKQvBURu+etjNgtb3XELnlHQGyedyTEZnlHQ2ySd0RE97wjI7rlHR3RJe+JeIrbLOecD6ePpZQ6W1kn2epo4MUrPOKyLN8ppYq1+y1VStncOjIdGnFZlo+U0uOtWOeOY2TE12Ouq//pEA7xXL7XfvcufR8K0Svfv6CREN3yDYfYIt9QiK3yjYTYLF95xB75SiP2ylcZsVu+cogj8pVCHJWvEuKwfOkREfKlRkTJlxkRJl86RMR8qRBR82VChM0XHpEhX2hElnyREWnyhUNkzBcKkTVfJETafIcjKuQ7FFEl35GIMvl2R1TMtyuiar49EWXzbY5oZpv/hibXTF2h3+s60FRKeT6+3TjMS3nrA3ZFRD8xrfY3ER1kJ+JEdBBwWGKeRAfEH1wS5WFZSDB/AAAAAElFTkSuQmCC'
  111. },
  112. lastIcon: { // 没有子集的ic
  113. type: String,
  114. default: ''
  115. },
  116. border: { // 是否有分割线
  117. type: Boolean,
  118. default: false
  119. },
  120. },
  121. data() {
  122. return {
  123. showTree: false,
  124. treeList: [],
  125. selectIndex: -1,
  126. searchText: ''
  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: this.showChild, //子级是否显示
  169. open: false, //是否打开
  170. show: this.showChild, // 自身是否显示
  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. childItem.showChild = true
  253. } else {
  254. childItem.showChild = false
  255. }
  256. childItem.hideArr[item.rank] = null
  257. }
  258. // console.log(childItem.hideArr)
  259. }
  260. }
  261. })
  262. // console.log(this.treeList)
  263. },
  264. _treeItemSelect(item, index) {
  265. this.treeList[index].checked = !this.treeList[index].checked
  266. this._fixMultiple(index)
  267. },
  268. // 处理单选多选
  269. _fixMultiple(index) {
  270. if (!this.multiple) {
  271. // 如果是单选
  272. this.treeList.forEach((v, i) => {
  273. if (i != index) {
  274. this.treeList[i].checked = false
  275. } else {
  276. this.treeList[i].checked = true
  277. }
  278. })
  279. }
  280. },
  281. // 重置数据
  282. _reTreeList() {
  283. this.treeList.forEach((v, i) => {
  284. this.treeList[i].checked = v.orChecked
  285. })
  286. },
  287. _initTree(range = this.range) {
  288. this.treeList = [];
  289. this._renderTreeList(range);
  290. this.$nextTick(() => {
  291. this._defaultSelect(range)
  292. })
  293. },
  294. // searChange(value, data) {
  295. // // this.treeList = this.filterTree(value, data)
  296. // },
  297. // filterTree(val, tree, newArr = []) {
  298. // if (!(tree.length && val)) { // 如果搜索关键字为空直接返回源数据
  299. // return tree
  300. // }
  301. // for (let item of tree) {
  302. // if (item.name.indexOf(val) > -1) { // 匹配到关键字的逻辑
  303. // newArr.push(item) // 如果匹配到就在数值中添加记录
  304. // continue // 匹配到了就退出循环了此时如果有子集也会一并带着
  305. // }
  306. // if (item.children && item.children.length) { // 如果父级节点没有匹配到就看看是否有子集,然后做递归
  307. // let subArr = this.filterTree(val, item.children) // 缓存递归后的子集数组
  308. // if (subArr && subArr.length) { // 如果子集数据有匹配到的节点
  309. // let node = {
  310. // ...item,
  311. // children: subArr
  312. // } // 关键逻辑,缓存父节点同时将递归后的子节点作为新值
  313. // newArr.push(node) // 添加进数组
  314. // }
  315. // }
  316. // }
  317. // return newArr
  318. // },
  319. },
  320. watch: {
  321. range(list) {
  322. this._initTree(list);
  323. },
  324. multiple() {
  325. if (this.range.length) {
  326. this._reTreeList();
  327. }
  328. },
  329. selectParent() {
  330. if (this.range.length) {
  331. this._reTreeList();
  332. }
  333. },
  334. // searchText: {
  335. // handler(newValue, oldValue) {
  336. // console.log(111)
  337. // console.log(this.treeList,11)
  338. // console.log(this.range,11)
  339. // // let arr = JSON.parse(JSON.stringify(this.treeList))
  340. // this.filterTrees = this.filterTree(newValue, this.range)
  341. // console.log(this.range,22)
  342. // console.log(this.treeList,22)
  343. // },
  344. // deep: true
  345. // }
  346. },
  347. mounted() {
  348. this._initTree();
  349. }
  350. }
  351. </script>
  352. <style scoped>
  353. @import "./style.css";
  354. </style>