elementUI的el-tree的赋值回显问题

elementUI的el-tree

  • 回显方式一:default-checked-keys
 <el-tree
        ref="treeRef"
        :data="templateList"
        default-expand-all
        node-key="templateId"
        highlight-current
        :props="defaultProps"
        show-checkbox
        :default-checked-keys="treeDataSelect"
        :expand-on-click-node="false"
        @check="checkChange"
      >
      </el-tree>

数据请求:

 this.axios
        .get(this.loadTemplateInfoUrl, {
          params: { this.axios
        .get(this.loadTemplateInfoUrl, {
          params: {
            userId: this.relationId,
          },
        })
        .then(({ data }: { data: any }) => {
          if (data.errorcode == 0) {
            this.templateList = data.data.templateGroupList;
            this.templateList.map((item: any) => {
              item.templateName = item.sourceName;
              item.templateId = item.souceIndex;
            });
            let dataId = [] as any;
            this.templateList.map((element: any) => {
              element.customTemplateInfoList.map((item: any) => {
                if (item.selStatus == 1) {
                  dataId.push(item.templateId);
                }
              });
            });
            this.treeDataSelect = dataId;
          } else {
            ElMessage.warning(data.message);
          }
        });
            userId: this.relationId,
          },
        })
        .then(({ data }: { data: any }) => {
          if (data.errorcode == 0) {
            this.templateList = data.data.templateGroupList;
            this.templateList.map((item: any) => {
              item.templateName = item.sourceName;
              item.templateId = item.souceIndex;
            });
            let dataId = [] as any;
            this.templateList.map((element: any) => {
              element.customTemplateInfoList.map((item: any) => {
                if (item.selStatus == 1) {
                  dataId.push(item.templateId);
                }
              });
            });
            this.treeDataSelect = dataId;
            setTimeout(() => {
              dataId.forEach((item: any) => {
                (this.$refs.treeRef as any).setChecked(item, true, false);
              });
            }, 200);
          } else {
            ElMessage.warning(data.message);
          }
        });
  • 回显方式二:this.$refs.treeRef .setCheckedKeys();
 this.axios
        .get(this.loadTemplateInfoUrl, {
          params: {
            userId: this.relationId,
          },
        })
        .then(({ data }: { data: any }) => {
          if (data.errorcode == 0) {
            this.templateList = data.data.templateGroupList;
            this.templateList.map((item: any) => {
              item.templateName = item.sourceName;
              item.templateId = item.souceIndex;
            });
            let dataId = [] as any;
            this.templateList.map((element: any) => {
              element.customTemplateInfoList.map((item: any) => {
                if (item.selStatus == 1) {
                  dataId.push(item.templateId);
                }
              });
            });
            setTimeout(() => {
              dataId.forEach((item: any) => {
                (this.$refs.treeRef as any).setChecked(item, true, false);
              });
            }, 200);
          } else {
            ElMessage.warning(data.message);
          }
        });
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于 el-tree 组件的选中回显问题,我可以为你提供一些帮助。当你在 el-tree 中选中一个节点,可以通过修改对应节点的属性来实现选中状态的回显。 首先,你需要给 el-tree 组件绑定一个 v-model,将选中的节点绑定到一个变量上。例如: ```html <el-tree v-model="selectedNode" :data="treeData"></el-tree> ``` 其中,`treeData` 是 el-tree数据源,`selectedNode` 是一个变量,用于存储选中的节点。 接下来,在你需要进行选中回显候,可以修改 `selectedNode` 变量的值。例如,假设你有一个按钮,点击按钮后希望选中 id 为 1 的节点: ```html <button @click="selectNode(1)">选中节点1</button> ``` 然后,在 Vue 实例的方法中实现 `selectNode` 方法: ```javascript methods: { selectNode(id) { // 根据 id 找到对应的节点 const node = this.findNodeById(id, this.treeData); // 将节点设置为选中状态 this.$set(node, 'selected', true); }, findNodeById(id, nodes) { for (const node of nodes) { if (node.id === id) { return node; } if (node.children) { const found = this.findNodeById(id, node.children); if (found) { return found; } } } return null; } } ``` 在 `selectNode` 方法中,首先通过 `findNodeById` 方法找到 id 对应的节点,然后使用 `$set` 方法将节点的 `selected` 属性设置为 `true`,这样 el-tree 组件就会自动选中该节点。 希望这些信息能够对你有所帮助!如果还有其他问题,请随提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值