el-table 新增行 新增列 编辑删除

1 篇文章 0 订阅
1 篇文章 0 订阅

1、html 代码 红色为重点

:data替换为 自己的数据 

showTable 控制里表格的显示隐藏,用于增加列后的表格重新渲染

slot="header" 使用自定义表头 

列是动态遍历出来的 

<el-form >
  <el-form-item prop="servin">
    <template>
      <el-table v-if="showTable" v-loading="dataListLoading" :data="dataList" border  style="width: 100%;">
          <el-table-column header-align="center" align="center" v-for="(col,i) in colList" :key="i">
            <template slot="header" slot-scope="scope">
              <el-button @click.native.prevent="deleteCol(scope.$index, colList)" size="small">
                移除
              </el-button>
              <el-input size="mini" name="colNameList" :value='col'></el-input>
            </template>
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row[col]"></el-input>
              </template>
          </el-table-column>
        <el-table-column align="center" header-align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click.native.prevent="deleteRow(scope.$index, dataList)" size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-row>
      <el-col :span="12">
        <el-button style="width: 100%;" @click="addRow()" type="info" >新增行</el-button>
      </el-col>
      <el-col :span="12">
        <el-button style="width: 100%;" @click="addCol()" type="info" >新增列</el-button>
      </el-col>
    </el-row>
  </el-form-item>
</el-form>

2、vue js 部分 

 

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      showTable: true,
     dataList: [],
      colList: [] // 动态获取自己列数据
    }
  },
  methods: {
    deleteRow (index, rows) {//删除改行
      rows.splice(index, 1);
    },
    deleteCol (index, col) {//删除改列
      col.splice(index, 1);
    },
    addRow () {             let row = {}
        this.colList.forEach(col=>{
        row[col]=''
        })    },
    addCol () {
      this.colList[this.colList.length] = ''
      this.showTable = false
      this.$nextTick(() => {
        this.showTable = true
      })
    }
  }
}

先记录后续完善 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值