elementui组件中,树形组件的使用

本是古典 何须时尚 2022-10-27 01:34 158阅读 0赞

项目场景:

elementui组件中,树形组件的使用


问题描述:

通过树形结构展示,点击勾选数据。没有展开树结构的情况下,勾选树节点时,其叶子节点全部被勾选,@check-change=“handleCheckChange”,叶子没有响应check-change函数

  1. <el-dialog
  2. title="短信接受对象选择"
  3. :visible.sync="dialogVisible"
  4. width="40%"
  5. :close-on-click-modal="false">
  6. <el-row class="person-selected" :gutter="20">
  7. <div v-for="person in nameList" :key="person.key">
  8. {
  9. {
  10. person.name }}
  11. <i class="el-icon-close" @click="eventPersonRemove(person.key)"></i>
  12. </div>
  13. </el-row>
  14. <el-row>
  15. <el-tree
  16. :data="data"
  17. show-checkbox
  18. node-key="id"
  19. ref="tree"
  20. :render-after-expand=false
  21. highlight-current
  22. @check-change="handleCheckChange">
  23. </el-tree>
  24. </el-row>
  25. <el-row class="form-footer">
  26. <el-button @click="resetChecked">重 置</el-button>
  27. <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  28. </el-row>
  29. </el-dialog>
  30. return {
  31. dialogVisible: false,
  32. data: [
  33. {
  34. id: 2,
  35. label: '研发部',
  36. isLeaf: false,
  37. children: [{
  38. id: 5,
  39. label: '王五',
  40. isLeaf: true,
  41. }, {
  42. id: 6,
  43. label: '赵六',
  44. isLeaf: true,
  45. }]
  46. }, {
  47. id: 3,
  48. label: '综合部',
  49. isLeaf: false,
  50. children: [{
  51. id: 7,
  52. label: '张三',
  53. isLeaf: true,
  54. }, {
  55. id: 8,
  56. label: '李四',
  57. isLeaf: true,
  58. }]
  59. }],
  60. nameList:[],
  61. defaultProps: {
  62. children: 'children',
  63. label: 'label'
  64. },
  65. }

原因分析:

elementUI的tree控件,其属性render-after-expand(默认为true) 是否在第一次展开某个树节点后才渲染其子节点,应绑定为false,不展开节点也渲染其子节点。


解决方案:

  1. resetChecked() {
  2. this.$refs.tree.setCheckedKeys([]);
  3. },
  4. handleCheckChange(data, checked, indeterminate) {
  5. // console.log(data.label)
  6. let self = this;
  7. if (checked === false){
  8. for (let i = 0; i < self.nameList.length; i++) {
  9. if (self.nameList[i].key === data.id){
  10. self.nameList.splice(i, 1);
  11. break;
  12. }
  13. }
  14. }else {
  15. if (data.isLeaf) {
  16. self.nameList.push({
  17. name:data.label, key:data.id})
  18. }
  19. }
  20. },
  21. .person-selected {
  22. padding: 10px;
  23. div {
  24. position: relative;
  25. float: left;
  26. min-width: 100px;
  27. height: 30px;
  28. padding: 0 20px;
  29. margin: 0 10px 10px 0;
  30. line-height: 30px;
  31. color: #fff;
  32. text-align: center;
  33. background-color: #0a8d4f;
  34. border-radius: 15px;
  35. i {
  36. position: absolute;
  37. top: 0;
  38. right: 0;
  39. width: 20px;
  40. height: 30px;
  41. line-height: 30px;
  42. text-align: left;
  43. z-index: 1;
  44. }
  45. }
  46. }

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读