【el-table】

前言

el-table 使用总结


一、需求描述

实现树形数据与懒加载、列字段排序、表头固定、详情路由跳转、后端分页

图示:
在这里插入图片描述

二、实现步骤

1.实现树形结构

添加属性:

渲染树形数据时,必须要指定 row-key
        row-key=“id”
通过指定 row 中的 hasChildren 字段来指定哪些行是包含子节点。children 与 hasChildren 通过 tree-props 配置
        :tree-props="{children: ‘children’, hasChildren: ‘hasChildren’}"
row-class-name 属性来为 Table 中的某一行添加 class,本例用于设置父子内容背景色
        :row-class-name=“tableRowBgColor”

设置父子内容背景色:

	<style>
		// style 不加 lang="less" scoped
		.el-table .has-children-row {
   
		    background: #EBF8F8;
		}
		.el-table .is-children-row {
   
		    background: #FAFAFA;
		}
	</style>
	tableRowBgColor ({
    row}) {
   
	    if (row.hasChildren) {
   
	        return 'has-children-row'
	    }
	    if (row.isChildren) {
   
	        return 'is-children-row'
	    }
	    return ''
	}

单元格内容换行:
<br/>

	<el-table-column 
	    :label="$t('loc.assignedTo')"
	    width="120">
	    <template slot-scope="scope">
	        <span>{
  {$t('loc.recipients')}} ({
  {scope.row.sharedUsers.length}})</span>
	        <br/>
	        <span class="overflow-ellipsis w-full" :title="scope.row.sharedUserNames">{
  {scope.row.sharedUserNames}}</span>
	    </template>
	</el-table-column>

解决限制宽度使列内内容超限省略,引起懒加载按钮与内容换行:
在这里插入图片描述

	/deep/ .creator-col > .cell {
   
	    display: flex;
	    align-items: center;
	    .el-table__expand-icon {
   
	        flex: none;
	    }
	    .creator-name {
   
	        flex: auto;
	        min-width: 0;
	    }
	}
	
	.overflow-ellipsis {
   
	    white-space: nowrap;
	    overflow: hidden;
	    text-overflow: ellipsis;
	}

解决 el-table-column 限宽,列内容与表头错位问题:
在这里插入图片描述

	/deep/ .el-table table{
   
	  width: 0px;
	}

2.实现懒加载

添加属性:

设置 Table 的 lazy 属性为 true 与加载函数 load
        lazy
        :load=“getSharedRecordPlans”

懒加载列:

            <el-table-column 
                :label="$t('loc.assignedBy2')"
                sortable="custom"
                width="120"
                class-name="creator-col"
                :column-key="'CREATE_SHARE_USER'">
                <template slot-scope="scope">
                    <div class="overflow-ellipsis creator-name" :title="scope.row.createUsername">{
  {scope.row.createUsername}}</div>
                </template>
            </el-table-column>

加载子节点数据的函数:
resolve() 中传入子节点数据

methods: {
   
	...
	        getSharedRecordPlans (tree, treeNode, resolve) {
   
            let params = {
   
                shareId: tree.id,
                pageSize: 50,
            }
            this.$axios.post($api.urls().listSharedRecordPlans, params)
                .then(response => {
   
                    // 分享记录下周计划列表
                    let childrenItems = []
                    if (response.items.length > 0) {
   
                        // 向子记录中添加父记录内容
                        response.items.map((item) => {
   
                            childrenItems.push(Object.assign({
   }, item, {
   
                                'shareUser': tree.shareUser, 
                                'planCount': 1,
                                'sharedAtUtc': tree.sharedAtUtc,
                                'sharedUsers': tree.sharedUsers,
                                'sharedUserNames': tree.sharedUserNames,
                                'comment': tree.comment,
                                'isChildren': true
                                }))
                        })
                    }
                    resolve(childrenItems)
                }).catch(error => {
   })
        },
	...
}

3.实现列字段排序

添加属性:

列中设置 sortable 属性,通过 Table 的default-sort属性设置默认的排序列和排序顺序。可以使用sort-method或者sort-by使用自定义的排序规则。

如果需要后端排序,需将sortable设置为custom,同时在 Table 上监听sort-change事件
        @sort-change=“sortChange”
本例采用后端排序

html 代码

:column-key="‘SHARE_USER’" 设置对应后端枚举值

    data () {
   
        return {
   
       		...
            sortField: '', //拍序列
            sortOrder: '', //排序规则
       		...
   		}
	}
	
	methods: {
   
		...
		sortChange (params) {
   
		    let order = params.order
		    if (!order) {
   
		        this.sortField = undefined
		        this.sortOrder = undefined
		    } else {
   
		        let columnKey = params.column.columnKey
		        this.sortField = columnKey ? columnKey : params.column.label
		        this.sortOrder = params.order === 'ascending' ? 'ASC' : 'DESC'
		    }
		    // 刷新数据
		    this.loadData()
		}
		...

       	loadData () {
   
			...
            // 选择了排序,则按照字段排序
            if (this.sortField && this.sortOrder) {
   
                params['orderKey'] = this.sortField
                params['orderType'] = this.sortOrder
            }
       		...
            this.loader(params).then(data => {
   
            	...
            }).catch(error => {
   
                ...
            })
  		}
   		...
	}

loadData

	TeacherSharedTable.vue
	
	<template>
		...
	        <div>
	            <shared-plan-table ref="sharededPlanTable" :loader="listSharedPlans"></shared-plan-table>
	        </div>
		...
	</templete>

	<script>
		import LessonApi from '@/api/lessons2'
		import SharedPlanTable from './components/SharedPlanTable'

		export default {
     
		    name: 'SharedPlanList',
		    components: {
     SharedPlanTable},
		    ...
	        methods: {
     
       			listSharedPlans (params) {
     
		            params['shareUserId'] = this.shareUserId
		            return LessonApi.listSharedPlans(params)
		            ...
      			}
			}
		}
	</script>

4.实现表头固定

添加属性:

只要在el-table元素中定义了height属性,即可实现固定表头
        :height=“tableHeight”

原始方案:

新问题:合适的 tableHeight

ofCourse el-table 中添加 ref=“table”

	SharePlanTable.vue 

	created
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-table 是 Element UI 提供的一个表格组件,可以用于展示和编辑数据。嵌套 el-table 勾选是指在一个 el-table 中嵌套另一个 el-table,并且可以通过勾选来控制子表格的显示与隐藏。 实现嵌套 el-table 勾选的步骤如下: 1. 在父表格的列定义中,使用自定义模板来渲染子表格的展开与收起按钮。可以使用 el-table-column 的 scoped-slot 属性来定义自定义模板。 2. 在自定义模板中,使用 el-checkbox 组件来实现勾选功能,并绑定一个变量来控制子表格的显示与隐藏。 3. 在父表格的行数据中,添加一个属性来保存子表格的数据。 4. 在父表格的行展开事件中,根据当前行的数据,将子表格的数据赋值给子表格的数据属性。 5. 在子表格中,使用 v-if 或 v-show 来根据勾选状态来控制子表格的显示与隐藏。 下面是一个示例代码: ```html <template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="expand"> <template slot-scope="props"> <el-checkbox v-model="props.row.expanded" @change="toggleChildTable(props.row)"></el-checkbox> </template> </el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="gender" label="Gender"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 20, gender: 'Male', expanded: false, // 控制子表格的显示与隐藏 children: [ { name: 'Tom', age: 18, gender: 'Male' }, { name: 'Alice', age: 22, gender: 'Female' } ] }, { name: 'Mary', age: 25, gender: 'Female', expanded: false, children: [ { name: 'Bob', age: 30, gender: 'Male' }, { name: 'Linda', age: 28, gender: 'Female' } ] } ] }; }, methods: { toggleChildTable(row) { row.expanded = !row.expanded; } } }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值