平安校园
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

356 rader
12 KiB

  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. <!-- <view style="">
  14. <u-search height="90rpx" @change="searChange(searchText,treeList)" :inputStyle="{fontSize:'30rpx'}"
  15. v-model="searchText" placeholder="请输入" shape="square" :showAction="false"></u-search>
  16. </view> -->
  17. <scroll-view class="tki-tree-view-sc" :scroll-y="true">
  18. <block v-for="(item, index) in treeList" :key="index">
  19. <view 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. confirmColor: { // 确定按钮颜色
  89. type: String,
  90. default: '' // #2388FF
  91. },
  92. cancelColor: { // 取消按钮颜色
  93. type: String,
  94. default: '' // #757575
  95. },
  96. titleColor: { // 标题颜色
  97. type: String,
  98. default: '' // #757575
  99. },
  100. currentIcon: { // 展开时候的ic
  101. type: String,
  102. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L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aebCz6TmnHWyXyY+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=='
  103. },
  104. defaultIcon: { // 折叠时候的ic
  105. type: String,
  106. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAACE0lEQVR4Xu3c200DMRCF4XEltJAOkEugA+ggpUAHoQMqiFMCdEAJUMEiS4mEELlIO7bPOeN9i6K1rG/952myyea1WiCtXmEuYBPR4RBMxInoIOCwhOtJLKVszWyXc/5y2BvNEq6I+/3+kFK6M7OHnPM7jcLKjbZAvD/uaZtzflm5P4rbWyJWgDcze1LPuzVihfxUz7sH4ilJ2bx7Isrm3RtRMu8RiHJ5j0SUyXs0okTeCIj0eSMh0uaNhkiZNyIiXd7IiDR5oyNS5M2ACJ83EyJs3myIkHkzIsLlzYwIkzc7IkTeCojD81ZCHJa3GuKQvBURu+etjNgtb3XELnlHQGyedyTEZnlHQ2ySd0RE97wjI7rlHR3RJe+JeIrbLOecD6ePpZQ6W1kn2epo4MUrPOKyLN8ppYq1+y1VStncOjIdGnFZlo+U0uOtWOeOY2TE12Ouq//pEA7xXL7XfvcufR8K0Svfv6CREN3yDYfYIt9QiK3yjYTYLF95xB75SiP2ylcZsVu+cogj8pVCHJWvEuKwfOkREfKlRkTJlxkRJl86RMR8qRBR82VChM0XHpEhX2hElnyREWnyhUNkzBcKkTVfJETafIcjKuQ7FFEl35GIMvl2R1TMtyuiar49EWXzbY5oZpv/hibXTF2h3+s60FRKeT6+3TjMS3nrA3ZFRD8xrfY3ER1kJ+JEdBBwWGKeRAfEH1wS5WFZSDB/AAAAAElFTkSuQmCC'
  107. },
  108. lastIcon: { // 没有子集的ic
  109. type: String,
  110. default: ''
  111. },
  112. border: { // 是否有分割线
  113. type: Boolean,
  114. default: false
  115. },
  116. },
  117. data() {
  118. return {
  119. showTree: false,
  120. treeList: [],
  121. selectIndex: -1,
  122. searchText: ''
  123. }
  124. },
  125. computed: {},
  126. methods: {
  127. _show() {
  128. this.showTree = true
  129. },
  130. _hide() {
  131. this.showTree = false
  132. },
  133. _cancel() {
  134. this._hide()
  135. this.$emit("cancel", '');
  136. },
  137. _confirm() {
  138. // 处理所选数据
  139. let rt = [],
  140. obj = {};
  141. this.treeList.forEach((v, i) => {
  142. if (this.treeList[i].checked) {
  143. obj = {}
  144. obj.parents = this.treeList[i].parents
  145. obj = Object.assign(obj, this.treeList[i].source)
  146. // 移除子元素
  147. delete obj.children
  148. rt.push(obj)
  149. }
  150. })
  151. this._hide()
  152. this.$emit("confirm", rt);
  153. },
  154. //扁平化树结构
  155. _renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
  156. list.forEach(item => {
  157. this.treeList.push({
  158. id: item[this.idKey],
  159. name: item[this.rangeKey],
  160. source: item,
  161. parentId, // 父级id数组
  162. parents, // 父级id数组
  163. rank, // 层级
  164. showChild: false, //子级是否显示
  165. open: false, //是否打开
  166. show: rank === 0, // 自身是否显示
  167. hideArr: [],
  168. orChecked: item.checked ? item.checked : false,
  169. checked: item.checked ? item.checked : false,
  170. })
  171. if (Array.isArray(item.children) && item.children.length > 0) {
  172. // console.log(item)
  173. let parentid = [...parentId],
  174. parentArr = [...parents],
  175. childrenid = [...childrenid];
  176. delete parentArr.children
  177. parentid.push(item[this.idKey]);
  178. parentArr.push({
  179. [this.idKey]: item[this.idKey],
  180. [this.rangeKey]: item[this.rangeKey]
  181. })
  182. this._renderTreeList(item.children, rank + 1, parentid, parentArr);
  183. } else {
  184. this.treeList[this.treeList.length - 1].lastRank = true;
  185. }
  186. })
  187. // console.log(list)
  188. },
  189. // 处理默认选择
  190. _defaultSelect() {
  191. this.treeList.forEach((v, i) => {
  192. if (v.checked) {
  193. this.treeList.forEach((v2, i2) => {
  194. if (v.parentId.toString().indexOf(v2.parentId.toString()) >= 0) {
  195. v2.show = true
  196. if (v.parentId.includes(v2.id)) {
  197. v2.showChild = true;
  198. v2.open = true;
  199. }
  200. }
  201. })
  202. }
  203. })
  204. },
  205. // 点击
  206. _treeItemTap(item, index) {
  207. if (item.lastRank === true) {
  208. //点击最后一级时触发事件
  209. this.treeList[index].checked = !this.treeList[index].checked
  210. this._fixMultiple(index)
  211. return;
  212. }
  213. let list = this.treeList;
  214. let id = item.id;
  215. item.showChild = !item.showChild;
  216. item.open = item.showChild ? true : !item.open;
  217. list.forEach((childItem, i) => {
  218. if (item.showChild === false) {
  219. //隐藏所有子级
  220. if (!childItem.parentId.includes(id)) {
  221. return;
  222. }
  223. if (!this.foldAll) {
  224. if (childItem.lastRank !== true && !childItem.open) {
  225. childItem.showChild = false;
  226. }
  227. // 为隐藏的内容添加一个标记
  228. if (childItem.show) {
  229. childItem.hideArr[item.rank] = id
  230. }
  231. } else {
  232. if (childItem.lastRank !== true) {
  233. childItem.showChild = false;
  234. }
  235. }
  236. childItem.show = false;
  237. } else {
  238. // 打开子集
  239. if (childItem.parentId[childItem.parentId.length - 1] === id) {
  240. childItem.show = true;
  241. }
  242. // 打开被隐藏的子集
  243. if (childItem.parentId.includes(id) && !this.foldAll) {
  244. // console.log(childItem.hideArr)
  245. if (childItem.hideArr[item.rank] === id) {
  246. childItem.show = true;
  247. if (childItem.open && childItem.showChild) {
  248. childItem.showChild = true
  249. } else {
  250. childItem.showChild = false
  251. }
  252. childItem.hideArr[item.rank] = null
  253. }
  254. // console.log(childItem.hideArr)
  255. }
  256. }
  257. })
  258. // console.log(this.treeList)
  259. },
  260. _treeItemSelect(item, index) {
  261. this.treeList[index].checked = !this.treeList[index].checked
  262. this._fixMultiple(index)
  263. },
  264. // 处理单选多选
  265. _fixMultiple(index) {
  266. if (!this.multiple) {
  267. // 如果是单选
  268. this.treeList.forEach((v, i) => {
  269. if (i != index) {
  270. this.treeList[i].checked = false
  271. } else {
  272. this.treeList[i].checked = true
  273. }
  274. })
  275. }
  276. },
  277. // 重置数据
  278. _reTreeList() {
  279. this.treeList.forEach((v, i) => {
  280. this.treeList[i].checked = v.orChecked
  281. })
  282. },
  283. _initTree(range = this.range) {
  284. this.treeList = [];
  285. this._renderTreeList(range);
  286. this.$nextTick(() => {
  287. this._defaultSelect(range)
  288. })
  289. },
  290. // searChange(value, data) {
  291. // // this.treeList = this.filterTree(value, data)
  292. // },
  293. filterTree(val, tree, newArr = []) {
  294. if (!(tree.length && val)) { // 如果搜索关键字为空直接返回源数据
  295. return tree
  296. }
  297. for (let item of tree) {
  298. if (item.name.indexOf(val) > -1) { // 匹配到关键字的逻辑
  299. newArr.push(item) // 如果匹配到就在数值中添加记录
  300. continue // 匹配到了就退出循环了此时如果有子集也会一并带着
  301. }
  302. if (item.children && item.children.length) { // 如果父级节点没有匹配到就看看是否有子集,然后做递归
  303. let subArr = this.filterTree(val, item.children) // 缓存递归后的子集数组
  304. if (subArr && subArr.length) { // 如果子集数据有匹配到的节点
  305. let node = {
  306. ...item,
  307. children: subArr
  308. } // 关键逻辑,缓存父节点同时将递归后的子节点作为新值
  309. newArr.push(node) // 添加进数组
  310. }
  311. }
  312. }
  313. return newArr
  314. },
  315. },
  316. watch: {
  317. range(list) {
  318. this._initTree(list);
  319. },
  320. multiple() {
  321. if (this.range.length) {
  322. this._reTreeList();
  323. }
  324. },
  325. selectParent() {
  326. if (this.range.length) {
  327. this._reTreeList();
  328. }
  329. },
  330. // searchText: {
  331. // handler(newValue, oldValue) {
  332. // console.log(111)
  333. // console.log(this.treeList,11)
  334. // console.log(this.range,11)
  335. // // let arr = JSON.parse(JSON.stringify(this.treeList))
  336. // this.filterTrees = this.filterTree(newValue, this.range)
  337. // console.log(this.range,22)
  338. // console.log(this.treeList,22)
  339. // },
  340. // deep: true
  341. // }
  342. },
  343. mounted() {
  344. this._initTree();
  345. }
  346. }
  347. </script>
  348. <style scoped>
  349. @import "./style.css";
  350. </style>