el-tree回显的问题

//   1、直接用:default-checked-keys='checkedKey',将Id push进去,会导致没有全部选中的第二级全部选中

list.forEach(item => {
    this.checkedKey.push(item)
})

//  2、this.$refs.tree.setCheckedKeys(this.defalutArr);

解决方式
setChecked

var that = this
setTimeout(function () {
       list.forEach((value)=>{
       //1. 勾选节点的 key 或者 data 2. boolean 类型,节点是否选中 3. boolean 类型,是否设置子节点 ,默认为 false)如果全部选中setCheckedKeys
          that.$refs.tree.(value, true, false)
       })
},500);

setCheckedKeys

var that = this
let list = []
setTimeout(function () {
     res.data.forEach((value)=>{
         list.push(value.id)
         // that.$refs.tree.setCheckedKeys([])
         // that.$refs.tree.setCheckedKeys(list) 也会导致没有选中的子节点全部选中
         that.$nextTick(() => {
              const nodes = []
              list.forEach(item => {
                  const node = that.$refs.tree.getNode(item);
                  if (node.isLeaf) { //关键,过滤掉不是叶子节点的
                     nodes.push(item)
                  }
               })
               that.$refs.tree.setCheckedKeys(nodes,true)
          })
})
`el-tree-select`是Element UI库中的一个下拉树形选择组件。关于问题,当你需要在页面加载时示已选的值,或者从服务器获取数据并保持先前的选择时,可能会遇到问题。这里有几个常见的解决步骤: 1. **初始化数据**:在组件的`data`属性中提供初始的数据结构,包括已选节点的id或唯一标识符。 ```html <el-tree :data="treeData" :props="treeProps" v-model="selectedValue"> </el-tree> ``` 其中,`v-model`绑定的是你需要保存当前选择值的对象。 2. **设置默认值**:在Vue实例的data中定义`selectedValue`,如果存在已选节点,则包含相应的节点信息。 ```javascript export default { data() { return { treeData, selectedValue: // 如果有之前的选择,可以是一个节点对象 { id: '某个节点ID', ... } }; } } ``` 3. **填数据**:当获取到服务器数据时,遍历数据,查找是否存在已选的节点,然后将其添加到`selectedValue`中。 ```javascript async fetchData() { const response = await someApi(); this.treeData = response.data; if (this.selectedValue) { this.treeData.some(node => { if (node.id === this.selectedValue.id) { this.$set(this.selectedValue, 'expanded', true); return true; // 找到了就停止循环 } }); } } ``` 4. **处理填后的事件**:使用`@input`事件监听用户的更改,以便在用户手动改变选择时更新数据模型。 ```html <el-tree :data="treeData" :props="treeProps" v-model="selectedValue" @input="handleInputChange"></el-tree> <script> methods: { handleInputChange(value) { this.selectedValue = value; } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值