当前位置:   article > 正文

element ui 穿梭框支持 树形结构(仅支持2层)_elementui 穿梭框 树结构

elementui 穿梭框 树结构

代码如下 

  1. <div class="el-transfer flex-space-between-center padding-0-20">
  2. <div class="el-transfer-panel">
  3. <p class="el-transfer-panel__header">
  4. <el-checkbox
  5. v-model="leftObj.checkAll"
  6. :indeterminate="leftObj.isIndeterminate"
  7. :disabled="leftObj.allCount === 0"
  8. @change="(val) => checkboxChange(val, 'leftObj')"
  9. >全部城市</el-checkbox>
  10. <span class="count-float-right">{{ leftObj.chooseCount }}/{{ leftObj.allCount }}</span>
  11. </p>
  12. <div class="el-transfer-panel__body">
  13. <div v-if="!leftObj.treeData.length" class="el-transfer-panel__empty">无数据</div>
  14. <el-tree
  15. v-else
  16. :ref="leftObj.treeRef"
  17. class="transfer-tree"
  18. :data="leftObj.treeData"
  19. show-checkbox
  20. node-key="code"
  21. :props="defaultProps"
  22. @check-change="checkLeftChange"
  23. />
  24. </div>
  25. </div>
  26. <div class="el-transfer__buttons">
  27. <el-button type="primary" icon="el-icon-arrow-left" :disabled="rightObj.chooseCount === 0" @click="goToOpposite('rightObj', 'leftObj')" />
  28. <el-button type="primary" icon="el-icon-arrow-right" :disabled="leftObj.chooseCount === 0" @click="goToOpposite('leftObj', 'rightObj')" />
  29. </div>
  30. <div class="el-transfer-panel">
  31. <p class="el-transfer-panel__header">
  32. <el-checkbox
  33. v-model="rightObj.checkAll"
  34. :indeterminate="rightObj.isIndeterminate"
  35. :disabled="rightObj.allCount === 0"
  36. @change="(val) => checkboxChange(val, 'rightObj')"
  37. >已选城市</el-checkbox>
  38. <span class="count-float-right">{{ rightObj.chooseCount }}/{{ rightObj.allCount }}</span>
  39. </p>
  40. <div class="el-transfer-panel__body">
  41. <div v-if="!rightObj.treeData.length" class="el-transfer-panel__empty">无数据</div>
  42. <el-tree
  43. v-else
  44. :ref="rightObj.treeRef"
  45. class="transfer-tree"
  46. :data="rightObj.treeData"
  47. show-checkbox
  48. node-key="code"
  49. default-expand-all
  50. :props="defaultProps"
  51. @check-change="checkRightChange"
  52. />
  53. </div>
  54. </div>
  55. </div>

 js

  1. checkboxChange(checkAll, attr) {
  2. const ref = this[attr].treeRef
  3. if (!checkAll) {
  4. this.$refs[ref].setCheckedKeys([])
  5. } else {
  6. this.$refs[ref].setCheckedKeys(this[attr].totalChooseIdList)
  7. }
  8. },
  9. goToOpposite(fromAttr, toAttr) {
  10. // console.log(this.$refs.leftTreeData.getHalfCheckedNodes())
  11. if (this[fromAttr].checkAll) {
  12. this[toAttr].treeData = JSON.parse(JSON.stringify(this.treeList))
  13. this[fromAttr].treeData = []
  14. } else {
  15. // console.log(this.$refs.leftTreeData.getCheckedNodes())
  16. const ref = this[fromAttr].treeRef
  17. const list = this.$refs[ref].getCheckedNodes()
  18. const halfList = this.$refs[ref].getHalfCheckedNodes()
  19. const result = []
  20. list.forEach(i => {
  21. const item = this.treeList.find(chi => {
  22. return chi.code === i.code
  23. })
  24. if (item) {
  25. result.push(i)
  26. }
  27. })
  28. if (halfList.length) { // 有半选的,说明选了子级别的一个
  29. const keys = this.$refs[ref].getCheckedKeys()
  30. halfList.forEach(item => {
  31. const obj = {
  32. code: item.code,
  33. label: item.label,
  34. children: []
  35. }
  36. item.children.forEach(child => {
  37. if (keys.indexOf(child.code) > -1) {
  38. obj.children.push(child)
  39. }
  40. })
  41. result.push(obj)
  42. })
  43. }
  44. this[fromAttr].treeData = this.reduceDataByArr(this[fromAttr].treeData, result)
  45. this[toAttr].treeData = this.reduceDataByArr(this.treeList, this[fromAttr].treeData)
  46. }
  47. this[fromAttr].checkAll = false
  48. this[fromAttr].isIndeterminate = false
  49. this.$refs[this[fromAttr].treeRef].setCheckedKeys([])
  50. this.getTotalChooseIdList(fromAttr)
  51. this.getTotalChooseIdList(toAttr)
  52. },
  53. reduceDataByArr(initialList, optList) {
  54. const copyList = JSON.parse(JSON.stringify(initialList))
  55. const arr = []
  56. copyList.forEach(city => {
  57. const item = optList.find(i => i.code === city.code)
  58. if (!item) {
  59. arr.push(city)
  60. } else if (item.children && item.children.length) {
  61. const childList1 = JSON.parse(JSON.stringify(city.children))
  62. const childList2 = JSON.parse(JSON.stringify(item.children))
  63. const ch3List = []
  64. childList1.forEach(ch1 => {
  65. const ch1item = childList2.find(j => j.code === ch1.code)
  66. if (!ch1item) {
  67. ch3List.push(ch1)
  68. }
  69. })
  70. if (ch3List.length) {
  71. arr.push({
  72. ...city,
  73. children: ch3List
  74. })
  75. }
  76. }
  77. })
  78. return arr
  79. },
  80. checkLeftChange() {
  81. this.treeCheckChange('leftObj')
  82. },
  83. checkRightChange() {
  84. this.treeCheckChange('rightObj')
  85. },
  86. treeCheckChange(attr) {
  87. const ref = this[attr].treeRef
  88. const chooseList = this.$refs[ref].getCheckedKeys()
  89. const arr = []
  90. chooseList.forEach(i => {
  91. if (this[attr].totalChooseIdList.indexOf(i) > -1) {
  92. arr.push(i)
  93. }
  94. })
  95. this[attr].chooseCount = arr.length
  96. this[attr].checkAll = this[attr].chooseCount === this[attr].allCount
  97. this[attr].isIndeterminate = this[attr].chooseCount > 0 && !this[attr].checkAll
  98. },
  99. getTotalChooseIdList(attr) {
  100. const arr = []
  101. this[attr].treeData.forEach(i => {
  102. if (i.children && i.children.length) {
  103. i.children.forEach(child => {
  104. arr.push(child.code)
  105. })
  106. } else {
  107. arr.push(i.code)
  108. }
  109. })
  110. this[attr].totalChooseIdList = arr
  111. this[attr].allCount = arr.length
  112. this[attr].chooseCount = 0
  113. },
  114. const leftObj = {
  115. totalChooseIdList: [],
  116. checkAll: false,
  117. isIndeterminate: false,
  118. chooseCount: 0,
  119. allCount: 0,
  120. treeData: [],
  121. treeRef: 'leftTreeData'
  122. }
  123. const rightObj = {
  124. checkAll: false,
  125. isIndeterminate: false,
  126. chooseCount: 0,
  127. allCount: 0,
  128. treeData: [],
  129. totalChooseIdList: [],
  130. treeRef: 'rightTreeData'
  131. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/613529
推荐阅读