2021-01-21 element ui 树形结构 table

element ui 树形结构 table

项目需要使用树形结构的表格,利用分组字段为父级,展示所有的数据


数据处理:

在element ui 树形表格中,当 row 中包含 children 字段时,被视为树形数据。渲染树形数据时,所以在数据库查出数据以后在后台进行处理。

  1. 运用java8的新特性,会比较便捷
 //测试
 //People对象是用有id、name(名字)、groupName(分组名称)、children(孩子)的对象
    public static void main(String[] args) {
         List<People> data = new ArrayList<>();
         data.add(new People(1,"张三","全员恶人"));
         data.add(new People(2,"李四","全员恶人"));
         data.add(new People(3,"哇哈哈","饮料"));
         data.add(new People(4,"纯牛奶","饮料"));
         data.add(new People(5,"一条鱼","大漂亮"));

        //利用java8新特性,获得所有的分组名称
        List<String> groupNames = data.stream().map(e->e.getGroupName()).distinct().collect(Collectors.toList());

        //将分组与数据对应,更好查找
        LinkedHashMap<String, People> resultLinkedMap = new LinkedHashMap<String, People>();

        List<People> resultData = new LinkedList<>();

        //将带有groupName的list 转化为TestObject对象
        groupNames.forEach(e->{
            People people = new People();
            people.setGroupName(e);
            people.setId(resultData.size()-groupNames.size()); //设置TestObject 的id
            resultData.add(people);
            resultLinkedMap.put(e, people);
        });

        //遍历原始数据 找到分组对应的对象,加入其Children
        data.forEach(a -> resultLinkedMap.get(a.getGroupName()).getChildren().add(a));

		//打印输出
        for (People resultDatum : resultData) {
            System.out.println(resultDatum.getGroupName() + "  Children are:");
            for (int j = 0; j < resultDatum.getChildren().size(); j++) {
                System.out.println(" " + resultDatum.getChildren().get(j));
            }
        }
    }

id 是在前台树形表格渲染的row-key,必须每一个对象都要有

处理后的效果:


前台展示:

前台获取数据时一定要是JSON对象,切记不能是数组,如果是数组不能识别无法渲染!

  <el-table ref="table"
                       v-loading="tableLoading"
                       size="small"
                       class="table-style"
                       :data="data"
                       border
                       highlight-current-row
                       @current-change="handleCurrentChange"
                       :span-method="objectSpanMethod"
                       default-expand-all
                       row-key="id"
                       :tree-props="{children: 'children', hasChildren:'hasChildren'}">
                       
<el-table-column align="left" clalss="setCenter" prop="groupName" label="分组名称" min-width="80" sortable resizable show-overflow-tooltip >   </el-table-column>

<el-table-column align="left" clalss="setCenter" prop="status" label="状态" min-width="80" sortable resizable show-overflow-tooltip></el-table-column>

<el-table-column align="left" clalss="setCenter" prop="remark" label="备注" min-width="80" sortable resizable show-overflow-tooltip></el-table-column>
 
  </el-table>

:span-method=“objectSpanMethod” 是合并列的作用

 objectSpanMethod({row,column,rowIndex,columnIndex}){
            if(row.name===null){//这边是name为判断条件是不是要合并列
              if (columnIndex === 0) {
                // 将第一列向右合并一格
                return [1, 9];
              } else if (columnIndex === 1) {
                // 删除第二列
                return [0, 0];
              }
            }else if (columnIndex === 0) {
              // 对第一列的数据进行行合并
              return {
                rowspan: 1,
                colspan: 1
              }
            }
          },

default-expand-all 是默认在加载时展开children。

row-key=“id” 是必须加上的,id不是一定的可以是其他String类型的值,但必须是在表格渲染的数据都要有的属性


前台展示:


注:此表格与上面的数据无关,但是流程和渲染效果是一样的


过程中踩过的坑

前台可能会报错:Error:for nested data item, row-key is required.

可能是一下原因之一:
1.前台获取数据时一定要是JSON对象,切记不能是数组,如果是数组不能识别无法渲染!
2.id可能会与element ui 中起冲突,建议改其他的名字

提示:网上有Error:for nested data item, row-key is required.的解决方法是,更改element ui 的版本号,我试过无效,还有加 hasChildren=true 也无效果。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI 提供了一个灵活的表格组件 `ElTable`,它支持树形数据的展示。如果你想实现行吸顶效果,即当滚动到某一行时,该行会始终保持在视口顶部,可以使用虚拟滚动和自定义的滚动事件来实现。以下是基本步骤: 1. 使用 `tree-props` 属性配置树形结构:首先,你需要设置 `data` 为树形数据,并配置 `tree-props`,告诉 Element UI 数据的父级和子级关系。 ```javascript <el-table :data="treeData" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" > <!-- ... --> </el-table> ``` 2. 配置虚拟滚动:启用虚拟滚动 (`:virtual-scroll`) 可以提高表格性能,特别是数据量大时。 ```javascript <el-table :virtual-scroll="true" /> ``` 3. 自定义滚动事件:在 `mounted` 或 `created` 生命周期钩子中监听滚动事件,并检查滚动条位置,如果当前行不在视口中,将其滚动到顶部。 ```javascript export default { mounted() { this.handleScroll(); window.addEventListener('scroll', this.handleScroll); }, beforeDestroy() { window.removeEventListener('scroll', this.handleScroll); }, methods: { handleScroll() { const { scrollTop, clientHeight, scrollHeight, scrollElement, rowHeight } = this.$refs.table; if (scrollTop + clientHeight >= scrollHeight) { const currentRow = this.currentRow || {}; // 假设你有一个当前行的变量 const targetIndex = this.treeData.findIndex(item => item === currentRow); if (targetIndex !== -1) { const targetRow = this.treeData[targetIndex]; scrollElement.scrollTop = targetRow.offsetTop - rowHeight; // 吸顶处理 } } } } } ``` 记得在适当的时候更新 `currentRow` 变量,比如当用户点击或选中某一行时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值