vue element ui表格行编辑功能 新增和修改

在这里插入图片描述
在这里插入图片描述

<div>
<el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      prop="date"
      label="日期">
      <template slot-scope="scope">
        <el-date-picker
          v-model="scope.row.date"
          v-if="scope.row.id==editId"
          value-format='yyyy-MM-dd'
          type="date"
          placeholder="选择日期">
        </el-date-picker>
        <span v-else>{{scope.row.date}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="姓名">
      <template slot-scope="scope">
        <el-input v-if="scope.row.id==editId" v-model="scope.row.name"></el-input>
        <span v-else>{{scope.row.name}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="省份">
      <template slot-scope="scope">
        <el-input v-if="scope.row.id==editId" v-model="scope.row.province"></el-input>
        <span v-else>{{scope.row.province}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="市区">
      <template slot-scope="scope">
        <el-input v-if="scope.row.id==editId" v-model="scope.row.city"></el-input>
        <span v-else>{{scope.row.city}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="地址">
      <template slot-scope="scope">
        <el-input v-if="scope.row.id==editId" v-model="scope.row.address"></el-input>
        <span v-else>{{scope.row.address}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="邮编">
      <template slot-scope="scope">
        <el-input v-if="scope.row.id==editId" v-model="scope.row.zip"></el-input>
        <span v-else>{{scope.row.zip}}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="操作">
      <template slot-scope="scope">
        <el-button type="text" v-if="scope.row.id!=editId" @click="handleClick(scope.row)" size="small">查看</el-button>
        <el-button type="text" v-if="scope.row.id!=editId" @click="changeClick(scope.row)" size="small">编辑</el-button>
        <el-button type="text" v-if="scope.row.id!=editId" @click="delClick(scope.row)" size="small">删除</el-button>
        <el-button type="text" v-if="scope.row.id==editId" @click="cancelClick(scope.row)" size="small">取消</el-button>
        <el-button type="text" v-if="scope.row.id==editId" @click="saveClick(scope.row)" size="small">保存</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button @click="add">新增</el-button>
  </div>
data () {
    return {
      editData:[],  //编辑行初始数据
      editId:'',  //判断编辑的是哪一行
      tableData: [{
        id:'1',
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        id:'2',
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1517 弄',
        zip: 200333
      }, {
        id:'3',
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1519 弄',
        zip: 200333
      }, {
        id:'4',
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1516 弄',
        zip: 200333
      }]
    }
  },
    delClick(row){
      //删除接口 成功以后 this.editId = ''
    },
    cancelClick(row){
      if(row.id){
      	for(let i in row){
          row[i] = this.editData[i]
        }
        this.editId = ''
      }else{
        this.tableData.forEach((item,index)=>{
          if(item.id == ''){
            this.tableData.splice(index,1)
          }
        })
      }
    },
    saveClick(row){
      //保存接口 成功以后 this.editId = ''
    },
    changeClick(row){
      if(this.tableData.some((item)=>{
       return item.id==''
      })){
        this.$message({
          message: '请先保存',
          type: 'warning'
        });
        return
      }
      this.editData = JSON.parse(JSON.stringify(row))    //把当前行数据存一份,取消的时候行数据还原
      this.editId = row.id
    },
    handleClick(row) {
      console.log(row);
    },
    add(){
      if(this.editId!=''){
        this.$message({
          message: '请先保存',
          type: 'warning'
        });
        return
      }
      if(this.tableData.some((item)=>{
       return item.id==''
      })){
        this.$message({
          message: '请先保存',
          type: 'warning'
        });
        return
      }
      this.tableData.push(
        {
          date: '',
          name: '',
          province: '',
          city: '',
          address: '',
          zip:'',
          id:'',
        }
      )
    }

其他根据自己的需求做调整

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要在VueElement UI中添加一个新增功能,你需要完成以下步骤: 1. 首先,在你的Vue组件中引入Element UI中的表格组件,并确保你已经正确安装了Element UI: ```javascript import { Table, TableColumn, Dialog, Button } from 'element-ui'; ``` 2. 在你的Vue组件模板中,添加一个表格组件,并将数据绑定到表格的data属性上: ```html <template> <div> <el-table :data="tableData"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> <el-table-column label="Operation"> <template slot-scope="scope"> <el-button @click="edit(scope.row)">Edit</el-button> <el-button @click="delete(scope.row)">Delete</el-button> </template> </el-table-column> </el-table> </div> </template> ``` 3. 添加一个按钮或其他交互元素,以触发新增事件。在点击事件中,使用Element UI的Dialog组件显示一个对话框,用于输入新增数据: ```html <template> <div> <el-button @click="showAddDialog">Add</el-button> <el-table :data="tableData"> <!-- table columns... --> </el-table> <el-dialog title="Add New Entry" :visible.sync="addDialogVisible"> <!-- form fields... --> </el-dialog> </div> </template> <script> export default { data() { return { tableData: [], addDialogVisible: false, formData: {} // 新增表单数据 }; }, methods: { showAddDialog() { this.addDialogVisible = true; }, addRow() { // 将新增数据添加到表格数据中 this.tableData.push(this.formData); // 关闭对话框并重置表单数据 this.addDialogVisible = false; this.formData = {}; } } } </script> ``` 4. 在对话框中添加表单元素,以允许用户输入新增数据。你可以使用Element UI的Form和FormItem组件来创建表单: ```html <el-dialog title="Add New Entry" :visible.sync="addDialogVisible"> <el-form :model="formData"> <el-form-item label="Name"> <el-input v-model="formData.name"></el-input> </el-form-item> <el-form-item label="Age"> <el-input v-model="formData.age"></el-input> </el-form-item> <el-form-item label="Address"> <el-input v-model="formData.address"></el-input> </el-form-item> </el-form> <div slot="footer"> <el-button @click="addRow">Confirm</el-button> <el-button @click="addDialogVisible = false">Cancel</el-button> </div> </el-dialog> ``` 这样,你就可以在VueElement UI中添加一个表格新增功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

染指悲剧

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

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

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

打赏作者

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

抵扣说明:

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

余额充值