• antd-vue a-transfer 中 a-tree接口异步加载问题


    背景:

    为了可以让antd-vue  穿梭框能够把异步的树形图,发现异步树能正常的使用该组件,搜索全网发现全网找不到案例,研究一天搞定了。

    代码:

    1. <template>
    2. <div >
    3. <div>
    4. <a-transfer
    5. class="tree-transfer"
    6. :data-source="dataSource"
    7. :target-keys="targetKeys"
    8. :render="item => item.title"
    9. @change="transferchange"
    10. show-search
    11. :list-style="{
    12. width: '510px',
    13. height: '528px',
    14. }"
    15. :show-select-all="false"
    16. >
    17. <template
    18. slot="children"
    19. slot-scope="{ props: { direction, selectedKeys }, on: { itemSelect } }"
    20. >
    21. <a-tree
    22. v-if="direction === 'left'"
    23. :load-data="onLoadData" blockNode checkable checkStrictly :tree-data="treeData" :checkedKeys="[...selectedKeys, ...targetKeys]"
    24. @check=" (_, props) => {
    25. onChecked(_, props, [...selectedKeys, ...targetKeys], itemSelect);
    26. }
    27. "
    28. @select="
    29. (_, props) => {
    30. onChecked(_, props, [...selectedKeys, ...targetKeys], itemSelect);
    31. }"
    32. >
    33. a-tree>
    34. template>
    35. a-transfer>
    36. div>
    37. div>
    38. template>
    39. <script>
    40. const treeData = [
    41. {
    42. title: 'parent 1',
    43. key: '0-0',
    44. slots: {
    45. icon: 'smile',
    46. },
    47. selectable:false,
    48. children: [
    49. { title: 'leaf1' , key: '0-0-0', slots: { icon: 'meh' }, children:[{ title: 'leaf12', key: '0-0-11', scopedSlots: { icon: 'custom' } }] },
    50. { title: 'leaf2', key: '0-0-1', scopedSlots: { icon: 'custom' } ,checked:true},
    51. { title: 'leaf3', key: '0-0-3', scopedSlots: { icon: 'custom' } },
    52. ],
    53. testdatatestdata:['0-0-1', '0-0-3']
    54. },
    55. ];
    56. let transferDataSource = [];
    57. function flatten(list = []) {
    58. list.forEach(item => {
    59. transferDataSource.push(item);
    60. flatten(item.children);
    61. });
    62. }
    63. flatten(JSON.parse(JSON.stringify(treeData)));
    64. function isChecked(selectedKeys, eventKey) {
    65. return selectedKeys.indexOf(eventKey) !== -1;
    66. }
    67. function handleTreeData(data, targetKeys = []) {
    68. data.forEach(item => {
    69. item['disabled'] = targetKeys.includes(item.key);
    70. if (item.children) {
    71. handleTreeData(item.children, targetKeys);
    72. }
    73. });
    74. return data;
    75. }
    76. export default {
    77. name: "rx-user",
    78. data() {
    79. return {
    80. curval:"",
    81. curUser:{},
    82. targetKeys: [],
    83. dataSource: transferDataSource,
    84. treeData,
    85. treeData1:treeData
    86. }
    87. },
    88. created() {
    89. this.curval=this.value;
    90. },
    91. methods: {
    92. transferchange(targetKeys, direction, moveKeys){
    93. console.log('transferchange',targetKeys,direction,moveKeys);
    94. this.targetKeys = targetKeys;
    95. console.log('this.testdata',this.testdata);
    96. },
    97. onLoadData(treeNode) {
    98. return new Promise(resolve => {
    99. if (treeNode.dataRef.children) {
    100. resolve();
    101. return;
    102. }
    103. // 模拟接口加载
    104. setTimeout(() => {
    105. treeNode.dataRef.children = [
    106. { title: `${treeNode.eventKey}-0`, key: `${treeNode.eventKey}-0` ,scopedSlots: { icon: 'custom' }},
    107. { title: `${treeNode.eventKey}-1`, key: `${treeNode.eventKey}-1` ,isLeaf:true,scopedSlots: { icon: 'custom' }},
    108. ];
    109. this.treeData = [...this.treeData];
    110. // 更新穿梭框原数据
    111. // 修复穿梭宽带disabled的问题
    112. this.dataSource = [...this.dataSource ,...JSON.parse(JSON.stringify(treeNode.dataRef.children).replaceAll('disabled: true,','').replaceAll('disabled: true',''))];
    113. resolve();
    114. }, 1000);
    115. });
    116. },
    117. callback(){},
    118. onSelect(selectedKeys, info) {
    119. console.log('selected', selectedKeys, info);
    120. },
    121. onChange(targetKeys) {
    122. console.log('Target Keys:', targetKeys);
    123. this.targetKeys = targetKeys;
    124. console.log('this.testdata',this.testdata);
    125. },
    126. onChecked(_, e, checkedKeys, itemSelect) {
    127. // 增加如果是不是叶节点,就不让选中-配置下面注释的代码,
    128. // if(!e.node.dataRef.isLeaf){
    129. // return;
    130. //}
    131. console.log('onChecked _ treeData',this.treeData)
    132. console.log('onChecked _',_);
    133. console.log('onChecked e',e);
    134. console.log('onChecked checkedKeys',checkedKeys);
    135. console.log('onChecked itemSelect',itemSelect);
    136. if(e.node.dataRef.test){
    137. return;
    138. }
    139. const { eventKey } = e.node;
    140. itemSelect(eventKey, !isChecked(checkedKeys, eventKey));
    141. },
    142. },
    143. }
    144. script>
    145. <style scoped>
    146. /* 开启css样式的话,可以控制只能叶节点能被选择*/
    147. /* .tree-transfer /deep/ .ant-tree-switcher + .ant-tree-checkbox{
    148. display:none;
    149. }
    150. .tree-transfer /deep/ .ant-tree-switcher.ant-tree-switcher-noop + .ant-tree-checkbox{
    151. display:inline-block!important;
    152. } */
    153. style>

    上面代码是所有节点都能选中的,我实际是公司组织人员选择需要使用,所以只能选择叶节点的,

    js和css部分就是要上面代码注释的

    1. <style scoped>
    2. .tree-transfer /deep/ .ant-tree-switcher + .ant-tree-checkbox{
    3. display:none;
    4. }
    5. .tree-transfer /deep/ .ant-tree-switcher.ant-tree-switcher-noop + .ant-tree-checkbox{
    6. display:inline-block!important;
    7. }
    8. style>
    1. onChecked(_, e, checkedKeys, itemSelect) {
    2. // 增加如果是不是叶节点,就不让选中,
    3. if(!e.node.dataRef.isLeaf){
    4. return;
    5. }
    6. console.log('onChecked _ treeData',this.treeData)
    7. console.log('onChecked _',_);
    8. console.log('onChecked e',e);
    9. console.log('onChecked checkedKeys',checkedKeys);
    10. console.log('onChecked itemSelect',itemSelect);
    11. const { eventKey } = e.node;
    12. itemSelect(eventKey, !isChecked(checkedKeys, eventKey));
    13. },

    最终结果:

     哈哈哈,完美^-^ ,有问题点赞关注,免费解答

  • 相关阅读:
    《数据库的原理与应用》--用两种方法实现S-T数据库创建
    JAVA宠物寄存中心计时收费系统计算机毕业设计Mybatis+系统+数据库+调试部署
    MySQL系统与内建函数
    此芯科技加入百度飞桨硬件生态共创计划,加速端侧AI生态布局
    【云原生】2.1 Kubernetes基础概念
    Linux- 后台运行符&、nohup、disown
    计算机毕业设计Java物业信息管理系统(源码+系统+mysql数据库+Lw文档)
    Web前端:2022年最佳Web开发框架比较—你需要了解的一切
    【JavaScript】如何获取指定范围内的随机数
    Leetcode 2713. 矩阵中严格递增的单元格数(DFS DP)
  • 原文地址:https://blog.csdn.net/web_ding/article/details/127629789