随笔 - 142  文章 - 14  评论 - 0  阅读 - 70250

日常开发记录-elementUI 表格的编辑数据回显和删除操作

点击编辑,可以编辑对应行的数据内容,更新内容

复制代码
<template>
    <el-table
        :data="tableData"
        style="width: 100%">

            <el-table-column
                v-for="(item, i) in tableColumns"
                :key="i"
                :prop="item.prop"
                :label="item.label"
                v-bind="item.other">
                <template slot-scope="{ row }">
                    <span v-if="!exchange">{{ row[item.prop] }}</span>
                    <el-input v-else v-model="row[item.prop]" />
                </template>
                </el-table-column>
                <el-table-column>
                    <template slot-scope="scope">
                        <el-button
                        @click.native.prevent="editRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                        {{ exchange ? '保存': '编辑'}}
                        </el-button>
                        <el-button
                        @click.native.prevent="delRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                        删除
                        </el-button>
                    </template>
                </el-table-column>
    </el-table>
</template>

<script>
export default {
  data () {
    return {
      tableData: [{
        date: '2016-05-03',
        name: 'Tom1',
        address: 'No. 181, Grove St, Los Angeles',
        other: { width: '180' }
      }, {
        date: '2016-05-02',
        name: 'Tom2',
        address: 'No. 183, Grove St, Los Angeles'
      }, {
        date: '2016-05-04',
        name: 'Tom3',
        address: 'No. 184, Grove St, Los Angeles'
      }, {
        date: '2016-05-01',
        name: 'Tom4',
        address: 'No. 185, Grove St, Los Angeles'
      }],
      tableColumns: [
        { prop: 'date', label: 'Date' },
        { prop: 'name', label: 'Name' },
        { prop: 'address', label: 'Address' }
      ],
      exchange: false
    }
  },
  methods: {
    editRow () {
      this.exchange = !this.exchange
    },
    delRow (i) {
      // splice会改变原数组
      this.tableData.splice(i, 1)
    }
  }
}
</script>
复制代码

效果展示:

 

posted on   法老的微笑  阅读(763)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示