el-table实现根据列数和数据动态渲染可编辑的表

需求

  1. 列的个数和内容随机
  2. 每行可编辑

实现

网页代码
<el-table
  :data="tableData"
  style="width: 100%"
  height="700px">
  <!-- 这是复选框 -->
  <el-table-column
    type="selection"
    width="55">
  </el-table-column>
  <!-- 根据 column 数组内容动态渲染的列,通过 v-for -->
  <el-table-column
    width="150px"
    v-for="item in column"
    :key="item"
    :label="item">
     <template slot-scope="scope">
        <!-- 这个 el-input 是编辑用的,通过 v-if="edit" ,edit 为 true 显示 el-input 元素 -->
        <el-input size="small" v-model="tableData[scope.$index][item]" v-if="edit"></el-input>
        <!-- 这是显示文字的而不是编辑框,通过 v-else 选择渲染 -->
        <div class="center" v-else>{{scope.row[item]}}</div>
     </template>
   </el-table-column>
</el-table>
scope.$index 为行号,所以 tableData[scope.$index] 表示一行的数据
tableData[scope.$index][item] 为 item 列

{{scope.row[item]}}中,scope.row 也表示一行的数据,scope.row[item] 为 item 列
数据
export default {
    name: "table",
    data() {
    	edit: false,
    	column: ['col1', 'col2', 'col3'],
		tableData:[{
			col1: 'hhhhh',
			col2: '12',
			col3: 'zh'
		}]
    }
}

其他

编辑功能还需要其他逻辑和组件才能实现,比如一个按钮及点击事件等,靠你自己发挥了!

结尾

如果觉得有帮助,给我点个赞吧,阿里嘎多

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值