element-ui 可编辑表格 + 动态表头

Element-UI 可编辑表格 + 动态表头

利用el-table-column的自定义列模板可以与其他组件使用实现复杂的表格。

由于我想实现一个可编辑的表格,同时因为表格有很多个,同时列名也是不一样的(数量和名称),所以想偷个赖写个动态的切换的功能。
在这里插入图片描述
以下为代码:

<template>
    <div>
    <el-button @click="change">测试按钮</el-button>
    <el-table
            :data="tableData"
            class="tb-edit"
            style="width: 100%">
        <el-table-column  v-for="item in tableHead":label="item.label" :property="item.property"
                width="180">
            <template slot-scope="scope">
                <el-input  v-model="scope.row[scope.column.property]" ></el-input>
            </template>
        </el-table-column>

        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    </div>
</template>

<script>
    import {example1, example2} from './columns';
    
    export default {
        name:'ruleTable',
        data() {
            return {
                // 表头
                tableHead:example1,
                // 数据值
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎6666',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎45',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎333',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎222',
                    address: '上海市普陀区金沙江路 1516 弄'
                }],

            }
        },
        methods: {
            handleEdit(index, row) {
                console.log(index, row);
            },
            handleDelete(index, row) {
                console.log(index, row);
            },
            change(){
                this.tableHead = example2;
            }
        }
    }
</script>

代码关键在这里:

<el-table-column  v-for="item in tableHead":label="item.label" :property="item.property"
                width="180">
            <template slot-scope="scope">
                <el-input  v-model="scope.row[scope.column.property]" ></el-input>
            </template>
        </el-table-column>

通过一个循环拿到列的标签和列名,格式如下:

// 表头1
let example1=[
    {
        label:'姓名',
        property:'name'
    },{
        label:'地址',
        property:'address'
    }
];

// 表头2
let example2=[
    {
        label:'姓名',
        property:'name'
    },{
        label:'地址',
        property:'address'
    },{
        label:'日期',
        property:'date'
    }
];

export {
    example1,
    example2
};

在自定义模板里面通过scope.row[scope.column.property]取到当前行的列字段与输入框进行双向绑定,于是就是实现了可编辑的动态表头的表格。

当然我们的表格数据也就可以根据实现来变化了。


vue-element-extends 这个基于 el-table 的封装的可编辑表格不错,推荐一波。@q平面人

### element-ui 动态生成表格表头的实现方法 在 VueElement-UI 的开发环境中,动态生成表格表头通常通过自定义 `el-table-column` 组件的方式完成。以下是具体的实现方式: #### 1. 数据准备 为了支持动态生成表头,需要先准备好数据源。假设有一个数组用于描述表头字段及其对应的显示名称。 ```javascript data() { return { tableHeader: [ { prop: 'name', label: '姓名' }, { prop: 'age', label: '年龄' }, { prop: 'address', label: '地址' } ], tableData: [ { name: '张三', age: 20, address: '北京' }, { name: '李四', age: 25, address: '上海' } ] }; } ``` 上述代码中,`tableHeader` 定义了表头的信息,而 `tableData` 是表格的数据内容[^1]。 #### 2. 使用 v-for 渲染列 利用 Vue 的 `v-for` 指令可以动态渲染多个 `el-table-column` 列。 ```html <template> <div> <el-table :data="tableData"> <!-- 动态生成表头 --> <el-table-column v-for="(item, index) in tableHeader" :key="index" :prop="item.prop" :label="item.label" > </el-table-column> </el-table> </div> </template> ``` 这段模板会根据 `tableHeader` 数组中的每一项创建一个 `el-table-column`,从而实现动态生成表头的效果[^2]。 #### 3. 自定义表头样式或功能 如果需要进一步定制表头的行为或者外观,则可以在 `el-table-column` 中使用插槽(slot)。例如,在表头单元格中加入额外的操作按钮或其他交互控件。 ```html <el-table-column v-for="(item, index) in tableHeader" :key="index" :prop="item.prop" :label="item.label" > <template slot="header" slot-scope="scope"> {{ item.label }} <i class="el-icon-edit"></i> <!-- 添加编辑图标作为示例 --> </template> </el-table-column> ``` 此部分展示了如何扩展默认行为,并允许开发者灵活控制每个表头的内容和表现形式[^3]。 #### 4. 导出 Excel 表格 (可选) 当实现了动态表头之后,还可以集成导出功能以便于用户保存当前视图到本地文件系统。这里提供了一个简单的例子说明如何设置点击按钮后触发下载操作。 ```html <el-button type="primary" @click="exportExcel">导出 Excel</el-button> <script> import FileSaver from 'file-saver'; import XLSX from 'xlsx'; methods: { exportExcel() { const worksheet = XLSX.utils.json_to_sheet(this.tableData); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, 'SheetName'); // 将工作簿转换成二进制字符串 let wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' }); try { FileSaver.saveAs(new Blob([wbout], {type:"application/octet-stream"}), '表格.xlsx'); } catch(e){ console.error(e); } } } </script> ``` 以上脚本依赖第三方库 `FileSaver.js` 和 `xlsx` 来处理前端上的电子表格文件生成过程[^4]。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带着天使反上帝 - Kaybee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值