Element ui 表格树形控件的使用

Element ui 表格树形控件的使用

注意点:

  • element ui已经做好了封装,主要是下面三句代码:
<el-table
        :data="items"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
  • 必须要指定 row-key:tree-props可以不写,会有默认值。
  • 数据里面需要有chirdren字段,也可以是hasChildren(boolean类型)字段。
  • 如果不是懒加载,后端不要设置hasChildren这个属性,否则不能显示树形;如果是懒加载,则需要设置hasChildren字段。

部分实现代码如下:

  • 前端:
<template>
  <div>
    <el-table
      :data="dictList"
      :load="loadChildrens"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      row-key="id"
      style="width:100%"
      border
      lazy
    >
      <el-table-column label="名称" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="dictCode" label="编码" width="220" align="left">
<!--        <template slot-scope="scope">
          <span>{{ scope.row.dictCode }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import dict from '@/api/dict'
export default {
  data() {
    return {
      dictList: []
    }
  },
  created() {
    this.getDictList(1) // 获取第一级
  },
  methods: {
    getDictList(id) { // 查询第一级的内容
      dict.getDictList(id)
        .then(response => {
          this.dictList = response.data
        })
    },
    loadChildrens(tree, treeNode, resolve) { // 查询下面的层级内容,类似递归
      dict.getDictList(tree.id)
        .then(response => {
          resolve(response.data)
        })
    }
  }
}
</script>

<style scoped>

</style>
  • 后端
    1.实体类:重点hasChildren; // 数据库中不存在
@Data
@ApiModel(description = "数据字典")
@TableName("dict")
public class Dict extends BaseEnity {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")
    @TableLogic
    @TableField("is_deleted")
    private Integer isDeleted;

    @ApiModelProperty(value = "其他参数")
    @TableField(exist = false)
    private Map<String,Object> param = new HashMap<>();

    @ApiModelProperty(value = "上级id")
    @TableField("parent_id")
    private Long parentId;

    @ApiModelProperty(value = "名称")
    @TableField("name")
    private String name;

    @ApiModelProperty(value = "值")
    @TableField("value")
    private String value;

    @ApiModelProperty(value = "编码")
    @TableField("dict_code")
    private String dictCode;

    @ApiModelProperty(value = "是否包含子节点")
    @TableField(exist = false)
    private boolean hasChildren; // 数据库中不存在

}

2.逻辑处理

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     *     @Autowired
     *     protected M baseMapper;
     *     ServiceImpl 源码中已经做了mapper自动注入,因此在这里可以不用再注入
     */

    // 根据id查询子数据列表
    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.eq("parent_id",id); // select * from dict where parent_id = id
        List<Dict> dictList = baseMapper.selectList(wrapper);
        // 遍历List,向list集合中每个dict对象设置hasChildren
        for (Dict dict:dictList
             ) {
            boolean hasChildren = this.isHasChildren(dict.getId());
            dict.setHasChildren(hasChildren);
        }

        return dictList;
    }

    // 判断id下面 是否有 子结点hasChildren
    public boolean isHasChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);
        return count>0;
    }

}

启发文章:https://www.cnblogs.com/pzw23/p/12058457.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在左侧树形控件中绑定节点选中事件,然后根据选中的节点信息来更新右侧表格的内容。具体实现过程如下: 1. 在左侧树形控件中绑定节点选中事件,可以使用 Element UI 的 `node-click` 属性来实现。例如: ```html <template> <div> <el-tree :data="treeData" :props="treeProps" @node-click="handleNodeClick"></el-tree> </div> </template> ``` 2. 在 data 中定义变量 `selectedNode` 和 `tableData`,用于保存当前选中的节点和右侧表格的数据,例如: ```javascript <script> export default { data() { return { treeData: [...], // 左侧树形控件的数据 treeProps: {...}, // 左侧树形控件的配置项 selectedNode: null, // 当前选中的节点 tableData: [], // 右侧表格的数据 }; }, methods: { handleNodeClick(node) { // 更新选中的节点 this.selectedNode = node; // 根据选中的节点来获取对应的表格数据 this.tableData = this.fetchTableData(node.id); }, fetchTableData(nodeId) { // 根据节点 ID 来获取对应的表格数据 // ... }, }, }; </script> ``` 3. 在右侧表格使用 `computed` 属性来动态获取数据。例如: ```html <template> <div> <el-table :data="tableData"> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <!-- 其他表格列 --> </el-table> </div> </template> <script> export default { computed: { tableData() { // 根据当前选中的节点来动态获取表格数据 return this.selectedNode ? this.$parent.tableData : []; }, }, }; </script> ``` 这样,当左侧树形控件中的节点被选中时,就会触发 `handleNodeClick` 方法,更新 `selectedNode` 和 `tableData` 数据,从而动态更新右侧表格的内容。注意,使用 Element UI树形控件时,节点对象中的 `id` 属性默认为 `node-id`,可以通过 `props` 属性来自定义节点属性的名称。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值