平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tki-tree.vue 11 KiB

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