vue element项目常见实现表格内部可编辑功能

前言

后台系统都是各种表格表单编辑,整理了下常见的几种实现表格编辑的方式,希望有用。使用框架:vue+element

  1. 表格行内内部可编辑
  2. 数据从后台取得的表格行内可编辑
  3. 表格整体的可编辑

正文

1.简单表格行内内部可编辑

原理就是span 和 input 的切换显隐
在这里插入图片描述
代码:

<template>
    <div>
        <el-table :data="tabledatas" border>
            <el-table-column label="tab1">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.tab1"></el-input>
                    <span v-show="!scope.row.show">{{scope.row.tab1}}</span>
                </template>
            </el-table-column>
            <el-table-column label="tab2">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.tab2"></el-input>
                    <span v-show="!scope.row.show">{{scope.row.tab2}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="scope.row.show =true">编辑</el-button>
                    <el-button @click="scope.row.show =false">保存</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tabledatas: [
                    { tab1: '111', tab2: '2222',show:true},
                    { tab1: 'aaa', tab2: 'bbb' ,show:false},
                ],
            }
        },
    }
</script>

2. 数据从后端取得表格行内可编辑

从后台取得的数据,可能没有show这个属性,所以在接收数据的时候操作一下,加这个属性,效果依然。但此时也会遇见保存时候需要把这个属性去掉,不影响传输正确数据。

代码:

<template>
    <div>
        <el-table :data="tabledatas" border>
            <el-table-column type="selection"></el-table-column>
            <el-table-column label="tab1">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.tab1"></el-input>
                    <span v-show="!scope.row.show">{{scope.row.tab1}}</span>
                </template>
            </el-table-column>
            <el-table-column label="tab2">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.tab2"></el-input>
                    <span v-show="!scope.row.show">{{scope.row.tab2}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                 <el-button @click="scope.row.show =true">编辑</el-button>
                    <el-button @click="scope.row.show =false">保存</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tabledatas: [],
            }
        },
        created() {
            // 发请求去后台拿数据,如果有api,就正常请求,
            //我这里是demo,就简单给list赋值了,原理一样。
            // getlistApi().then(res => {
                // let list = res.data.list
                let list = [
                    { tab1: 'tast2', tab2: 'tast333' },
                    { tab1: 'aaa', tab2: 'bbb' },
                ]
                list.forEach(element => {
                    element["show"] = false
                });
                this.tabledatas = list
            // })
        },
    }
</script>

3.批量表格整体的可编辑

效果图:
在这里插入图片描述
代码:

<template>
    <div>
        <el-button @click="show =true">编辑</el-button>
        <el-button @click="show =false">提交</el-button>
        <el-table :data="tabledatas" border>
            <el-table-column label="tab1">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="show" v-model="scope.row.tab1"></el-input>
                    <span v-show="!show">{{scope.row.tab1}}</span>
                </template>
            </el-table-column>
            <el-table-column label="tab2">
                <template slot-scope="scope">
                    <el-input placeholder="请输入内容" v-show="show" v-model="scope.row.tab2"></el-input>
                    <span v-show="!show">{{scope.row.tab2}}</span>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tabledatas: [
                    { tab1: '111', tab2: '2222' },
                    { tab1: 'aaa', tab2: 'bbb'},
                ],
                show:false
            }
        },
    }
</script>

结语

这是编辑保存功能,下篇顺便写一下行内和批量删除和增加行的功能吧~
附:vue+element的表格最优实现单条和批量修改、保存、复制、删除、新增、提交数据功能

如果本文对你有帮助的话,请给我点赞打call哦~o( ̄▽ ̄)do
有其他问题留言 over~

  • 67
    点赞
  • 248
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
要在Vue3和element-plus中实现表格行内的编辑和新增,你可以使用element-plus提供的el-table-plus组件来替代el-table,el-table-plus支持行内编辑和新增功能。具体步骤如下: 1. 安装并引入element-plus组件库。 2. 在el-table-plus的column中设置prop属性,用来指定表格列的字段名。 3. 在el-table-plus的column中设置editable属性为true,表示该列可编辑。 4. 在el-table-plus的column中设置edit-template slot来自定义编辑时的表单组件。 5. 使用el-table-plus的@save事件监听保存数据事件,当保存数据时,将数据提交到后台或者更新本地数据。 以下是一个示例代码: ```html <template> <el-table-plus :data="tableData" @save="handleSave"> <el-table-column label="姓名" prop="name" editable></el-table-column> <el-table-column label="年龄" prop="age" editable> <template #edit-template="{ modelValue, row, column }"> <el-input-number v-model="modelValue" :min="0" :max="200" /> </template> </el-table-column> <el-table-column label="地址" prop="address" editable></el-table-column> <el-table-column label="操作" width="120px"> <template #default="{ row, column }"> <el-button @click="handleEdit(row)">编辑</el-button> <el-button @click="handleDelete(row)">删除</el-button> </template> <template #edit="{ row, column }"> <el-button @click="handleSaveEdit(row)">保存</el-button> <el-button @click="handleCancelEdit(row)">取消</el-button> </template> </el-table-column> </el-table-plus> </template> <script> import { ref } from 'vue'; import { ElTablePlus } from 'element-plus'; export default { components: { ElTablePlus, }, setup() { const tableData = ref([ { name: '张三', age: 18, address: '北京市' }, { name: '李四', age: 20, address: '上海市' }, { name: '王五', age: 22, address: '广州市' }, ]); const editData = ref({}); const editIndex = ref(-1); const handleEdit = (row) => { editData.value = Object.assign({}, row); editIndex.value = tableData.value.indexOf(row); }; const handleSaveEdit = (row) => { Object.assign(tableData.value[editIndex.value], editData.value); editIndex.value = -1; editData.value = {}; }; const handleCancelEdit = () => { editIndex.value = -1; editData.value = {}; }; const handleDelete = (row) => { tableData.value.splice(tableData.value.indexOf(row), 1); }; const handleSave = () => { // 提交表格数据到后台或者更新本地数据 }; return { tableData, editData, editIndex, handleEdit, handleSaveEdit, handleCancelEdit, handleDelete, handleSave, }; }, }; </script> ``` 在上面的代码中,我们使用el-table-plus组件替代el-table,并在el-table-plus的column中设置editable属性为true,表示该列可编辑。在edit-template slot中,我们使用el-input-number组件来自定义编辑时的表单组件。在@save事件中,我们监听保存数据事件,并将数据提交到后台或者更新本地数据。在操作列中,我们使用default slot来显示编辑和删除按钮,使用edit slot来显示保存和取消按钮。在handleEdit方法中,我们保存要编辑的数据到editData中,并将要编辑的行的索引保存到editIndex中。在handleSaveEdit方法中,我们将编辑的数据保存到表格数据中,并清空editIndex和editData。在handleCancelEdit方法中,我们清空editIndex和editData。这样就实现表格行内的编辑和新增功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值