el-table的增删改查

1.编辑操作(1)接口返回的数据进行处理

realTimeData(this.page.pageSize, this.page.pageCurrent, param).then(res=>{
 let arr = res.data.list;
 arr.map(item=>{
    item['isEdit'] = false
})
this.tableData = arr
this.page.pageTotal = res.data.Count
})

(2)在el-table组件中

    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="日期"
        width="180">
        <template slot-scope="scope">
         <span v-show="!scope.row.isEdit">{{scope.row.date}}</span>
         <el-input  v-show="scope.row.isEdit" v-model="scope.row.date" ></el-input>
        </template>
      </el-table-column>
 .......
<el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleEdit(scope.$index)" type="text" size="small">编辑</el-button>
        <el-button @click="handleDelete(scope.$index,scope.row)" type="text" size="small">删除</el-button>
<el-button @click="handleSave(scope.row)" type="text" size="small">保存</el-button>
      </template>
    </el-table-column>
    </el-table>

(3)编辑方法

handleEdit(index){
  this.$set(this.tableData[index],'isEdit',true)
},

2.新增一行

hangdleAddDetails(){ 
  getDefaultTime().then(res=>{
  this.newDate=res.data
  let obj={}
  obj.date='',
  obj.year='',
  obj.time='',
  this.tableData.push(obj)
})
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI Table 提供了一些 API 和事件,可以方便地实现增删改查操作。下面是一个简单的示例: ```vue <template> <div> <el-table :data="tableData" style="width:100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="handleEdit(scope.row)">编辑</el-button> <el-button @click="handleDelete(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> <el-dialog :visible.sync="dialogVisible"> <el-form :model="currentRow"> <el-form-item label="姓名"> <el-input v-model="currentRow.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model.number="currentRow.age"></el-input> </el-form-item> </el-form> <div slot="footer"> <el-button @click="dialogVisible = false">取消</el-button> <el-button type="primary" @click="handleSave">保存</el-button> </div> </el-dialog> <el-button type="primary" @click="handleAdd">新增</el-button> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18 }, { name: '李四', age: 20 }, { name: '王五', age: 22 } ], dialogVisible: false, currentRow: {} } }, methods: { handleAdd() { this.currentRow = {} this.dialogVisible = true }, handleEdit(row) { this.currentRow = Object.assign({}, row) this.dialogVisible = true }, handleDelete(index) { this.tableData.splice(index, 1) }, handleSave() { if (this.currentRow.name && this.currentRow.age) { const index = this.tableData.indexOf(this.currentRow) if (index > -1) { Object.assign(this.tableData[index], this.currentRow) } else { this.tableData.push(this.currentRow) } this.dialogVisible = false } } } } </script> ``` 上面的代码,我们定义了一个 `tableData` 数组,用于存储表格数据。通过 `<el-table>` 组件和 `<el-table-column>` 组件,我们可以将数据呈现为表格形式,并显示操作按钮。 当用户点击“新增”按钮时,我们将 `currentRow` 清空,并打开弹窗。当用户点击“编辑”按钮时,我们将 `currentRow` 设置为当前行数据,并打开弹窗。当用户点击“删除”按钮时,我们从 `tableData` 删除对应的行数据。 弹窗包含一个表单,用于编辑行数据。当用户点击“保存”按钮时,我们根据 `currentRow` 的数据,更新 `tableData` 对应的行数据,或者将 `currentRow` 添加到 `tableData` 。如果 `currentRow` 的数据不完整,我们不执行任何操作,并保持弹窗打开状态。 通过这种方式,我们可以方便地实现基本的增删改查功能。当然,如果需要更复杂的操作,可以根据具体需求进行扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值