Vue之功能全面的表格(八)表格数据的更新和删除

传递Todo对象

1、在打开对话框时,将选中的todo对象复制后设置为currentTodo

<el-table-column label="操作">
  <template slot-scope="scope">
    <el-button size="small" type="warning" icon="el-icon-edit" @click="editTodo(scope.row)"></el-button>
    <el-button size="small" type="danger" icon="el-icon-delete"></el-button>
  </template>
</el-table-column>
editTodo (row) {
  this.currentTodo = JSON.parse(JSON.stringify(row))
  this.currentAuthors = this.currentTodo.author
  this.editShow = true
}

2、此时点击编辑按钮后,即可看到数据已传入
在这里插入图片描述

添加路由

3、在server端新增put方法路由用于更新数据

router.route('/:id').put((req, res) => {
    Todo.findByIdAndUpdate(req.params.id, {$set:req.body}, {new: true}, (err, todo) => {
        if (err) console.log(err)
        res.json(todo)
    })
})

异步请求

4、回到client端,使用axios发送更新请求

editAjax () {
  this.$ajax.put('todos/' + this.currentTodo._id, this.currentTodo).then((res) => {
    if (res.data) {
      var index = this.data.findIndex(item => item.id === res.data._id)
      this.data.splice(index, 1, res.data)
    }
    this.closeEditDialog()
  }).catch((err) => this.$notify({
    type: 'error',
    message: err
  }))
},

更新Todo状态

5、在操作栏中新增三个按钮

<el-table-column label="操作">
  <template slot-scope="scope">
    <!-- 启动学习计划 -->
    <el-button v-if="scope.row.status === 0 || scope.row.status === 2" @click="updateStatusAjax(scope.row, 1)" size="small" type="primary" icon="el-icon-arrow-right"></el-button>
    <!-- 搁置学习计划 -->
    <el-button v-if="scope.row.status === 1" @click="updateStatusAjax(scope.row, 2)" size="small" type="info" icon="el-icon-loading"></el-button>
    <!-- 完成学习计划 -->
    <el-button v-if="scope.row.status === 1" @click="updateStatusAjax(scope.row, 3)" size="small" type="success" icon="el-icon-check"></el-button>
    <el-button size="small" type="warning" icon="el-icon-edit" @click="editTodo(scope.row)"></el-button>
    <el-button size="small" type="danger" icon="el-icon-delete"></el-button>
  </template>
</el-table-column>

6、向之前写好的PUT路由发送ajax请求更新状态

updateStatusAjax (row, status) {
  var todo = {_id: row._id, status}
  this.$ajax.put('todos/' + todo._id, todo).then((res) => {
    if (res.data) {
      var index = this.data.findIndex(item => item._id === res.data._id)
      this.data.splice(index, 1, res.data)
    }
  }).catch((err) => this.$notify({
    type: 'error',
    message: err
  }))
}

7、现在就可以更新todo的状态了
在这里插入图片描述

删除数据

8、与之前更新方法类似,为按钮绑定ajax方法

<el-button size="small" type="danger" icon="el-icon-delete" @click="removeTodoAjax(scope.row)"></el-button>
removeTodoAjax (row) {
  this.$confirm('确定要删除?').then(() => {
    this.$ajax.delete('todos/' + row._id).then((res) => {
      if (res.data) {
        var index = this.data.findIndex(item => item._id === res.data._id)
        this.data.splice(index, 1)
      }
    })
  }).catch((err) => this.$notify({
    type: 'error',
    message: err
  }))
}

9、在服务器端添加delete方法路由

router.route('/:id').put((req, res) => {
    Todo.findByIdAndUpdate(req.params.id, {$set:req.body}, {new: true}, (err, todo) => {
        if (err) console.log(err)
        res.json(todo)
    })
}).delete((req, res) => {
    Todo.findByIdAndRemove(req.params.id, (err, todo) => {
        if (err) console.log(err)
        res.json(todo)
    })
})

10、实现删除功能
在这里插入图片描述
在这里插入图片描述
删除成功

小结

目前已实现了数据的增删改查功能,接下来将实现数据的导入导出功能

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现Vue批量删除表格数据: 1. 首先,在Vue组件中定义一个数组来存储表格数据,例如: ``` data() { return { tableData: [ { id: 1, name: 'John Doe', age: 25 }, { id: 2, name: 'Jane Doe', age: 30 }, { id: 3, name: 'Bob Smith', age: 40 }, { id: 4, name: 'Alice Johnson', age: 35 } ], selectedRows: [] } } ``` 其中,`tableData`数组存储表格数据,`selectedRows`数组用于存储选中的行数据。 2. 接下来,在表格中添加复选框列,用于选中要删除的行数据,例如: ``` <el-table :data="tableData" @selection-change="handleSelectionChange"> <el-table-column type="selection"></el-table-column> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> ``` 在上面的代码中,`type="selection"`用于定义复选框列,`@selection-change`用于监听选中的行数据。 3. 然后,在Vue组件中定义一个方法来处理选中行数据的变化,例如: ``` methods: { handleSelectionChange(selection) { this.selectedRows = selection } } ``` 在上面的代码中,`selection`参数表示选中的行数据,将其赋值给`selectedRows`数组。 4. 最后,在Vue组件中定义一个方法来处理批量删除操作,例如: ``` methods: { handleDelete() { for (let i = 0; i < this.selectedRows.length; i++) { const index = this.tableData.indexOf(this.selectedRows[i]) this.tableData.splice(index, 1) } this.selectedRows = [] } } ``` 在上面的代码中,使用`indexOf`方法获取选中行数据在`tableData`数组中的索引,然后使用`splice`方法从数组中删除该行数据。最后,清空`selectedRows`数组。 5. 绑定删除按钮的点击事件,调用`handleDelete`方法,例如: ``` <el-button type="danger" @click="handleDelete">Delete</el-button> ``` 至此,就完成了Vue批量删除表格数据的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值