点击获取数据 ..._a-table children">
当前位置:   article > 正文

ant a-table 树表格级联选择_a-table children

a-table children

  1. <template>
  2. <div>
  3. <div class="table-box">
  4. <a-button type="primary" @click="btnclick">点击获取数据</a-button>
  5. <a-table :columns="columns" :data-source="data" :row-selection="rowSelection" />
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. columns: [
  14. {
  15. title: '姓名',
  16. dataIndex: 'name',
  17. key: 'name',
  18. },
  19. {
  20. title: '年龄',
  21. dataIndex: 'age',
  22. key: 'age',
  23. width: '12%',
  24. },
  25. {
  26. title: '地址',
  27. dataIndex: 'address',
  28. width: '30%',
  29. key: 'address',
  30. },
  31. ],
  32. data: [
  33. {
  34. key: 1,
  35. name: '张三',
  36. age: 60,
  37. address: '北京朝阳',
  38. children: [
  39. {
  40. key: 11,
  41. name: '王五',
  42. age: 42,
  43. address: '北京石景山',
  44. },
  45. {
  46. key: 12,
  47. name: '李四',
  48. age: 30,
  49. address: '河北邯郸',
  50. children: [
  51. {
  52. key: 121,
  53. name: '李飒',
  54. age: 16,
  55. address: '河北石家庄',
  56. },
  57. ],
  58. },
  59. {
  60. key: 13,
  61. name: '马六',
  62. age: 72,
  63. address: '山东济南',
  64. children: [
  65. {
  66. key: 131,
  67. name: '马林',
  68. age: 42,
  69. address: '山东济宁',
  70. children: [
  71. {
  72. key: 1311,
  73. name: '马里兰',
  74. age: 25,
  75. address: '山东滨州',
  76. },
  77. {
  78. key: 1312,
  79. name: '马安山',
  80. age: 18,
  81. address: '山东青岛',
  82. },
  83. ],
  84. },
  85. ],
  86. },
  87. ],
  88. },
  89. {
  90. key: 2,
  91. name: '冯七',
  92. age: 32,
  93. address: '东北大庆',
  94. },
  95. {
  96. key: 3,
  97. name: '冯七3',
  98. age: 32,
  99. address: '东北大庆',
  100. },
  101. {
  102. key: 4,
  103. name: '冯七4',
  104. age: 32,
  105. address: '东北大庆',
  106. },
  107. {
  108. key: 5,
  109. name: '冯七5',
  110. age: 32,
  111. address: '东北大庆',
  112. },
  113. {
  114. key: 6,
  115. name: '冯七6',
  116. age: 32,
  117. address: '东北大庆',
  118. },
  119. {
  120. key: 7,
  121. name: '冯七7',
  122. age: 32,
  123. address: '东北大庆',
  124. },
  125. {
  126. key: 8,
  127. name: '冯七8',
  128. age: 32,
  129. address: '东北大庆',
  130. },
  131. {
  132. key: 9,
  133. name: '冯七9',
  134. age: 32,
  135. address: '东北大庆',
  136. },
  137. {
  138. key: 10,
  139. name: '冯七10',
  140. age: 32,
  141. address: '东北大庆',
  142. },
  143. {
  144. key: 101,
  145. name: '冯七101',
  146. age: 32,
  147. address: '东北大庆',
  148. },
  149. {
  150. key: 102,
  151. name: '冯七102',
  152. age: 32,
  153. address: '东北大庆',
  154. },
  155. ],
  156. selectedRowKeys: [2],
  157. }
  158. },
  159. created() {},
  160. computed: {
  161. rowSelection() {
  162. const { selectedRowKeys } = this
  163. return {
  164. selectedRowKeys,
  165. onChange: this.onSelectChange,
  166. hideDefaultSelections: true,
  167. onSelect: this.onSelect,
  168. onSelectAll: this.onSelectAll,
  169. }
  170. },
  171. },
  172. methods: {
  173. // 按钮点击事件
  174. btnclick() {
  175. this.selectedRowKeys = [...new Set(this.selectedRowKeys)]
  176. console.log('最后拿到的数据:', this.selectedRowKeys)
  177. },
  178. // 选中项发生变化时的回调
  179. onSelectChange(selectedRowKeys) {
  180. // this.selectedRowKeys=[]
  181. this.selectedRowKeys = selectedRowKeys
  182. },
  183. // 用户手动选择/取消选择某列的回调
  184. onSelect(record, selected) {
  185. // this.selectedRowKeys=[]
  186. //每个复选框点击都会触发
  187. const selectrows = [record.key]
  188. if (record.hasOwnProperty('children')) {
  189. const generator = (record) => {
  190. record.forEach((item) => {
  191. selectrows.push(item.key)
  192. if (item.children && item.children.length > 0) {
  193. generator(item.children)
  194. }
  195. })
  196. }
  197. generator(record.children)
  198. }
  199. const newselect = this.selectedRowKeys.filter((item) => !selectrows.includes(item))
  200. selected
  201. ? (this.selectedRowKeys = [...this.selectedRowKeys, ...selectrows])
  202. : (this.selectedRowKeys = newselect)
  203. // console.log('剩余的数据:', [...new Set(this.selectedRowKeys)])
  204. },
  205. // 勾选全部
  206. onSelectAll(selected, selectedRows, changeRows) {
  207. this.selectedRowKeys = []
  208. selectedRows.forEach((item) => {
  209. this.selectedRowKeys.push(item.key)
  210. })
  211. // console.log('全部:', [...new Set(this.selectedRowKeys)])
  212. // console.log('全部:', selected, selectedRows, changeRows)
  213. },
  214. // 默认勾选
  215. getCheckboxProps: (record) => ({
  216. //重点部分
  217. props: {
  218. defaultChecked: this.selectedRowKeys.indexOf(record.key) > -1 ? true : false, //defaultCheckedId里面是默认选中的id,判断是否含有这些id,有的那就选中,没有的就不选中
  219. key: record.key + '', //使得id的数据类型为string
  220. },
  221. }),
  222. },
  223. }
  224. </script>
  225. <style lang="less" scoped>
  226. .table-box {
  227. background-color: #fff;
  228. }
  229. </style>

 

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