vue前端实现表格数据增查改删功能

 更新时间:2024年05月10日 10:34:38   作者:崛起猩球@悟空  
增删改查是我们写项目百分之七十会遇到的代码,下面这篇文章主要给大家介绍了关于vue前端实现表格数据增查改删功能的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

一、添加(增)-unshift首插入

1、【新增按钮】添加点击事件cilck;

<el-button @click="handleAdd()">添加</el-button>

2、点击【新增按钮】:

2.1、打开弹框;

2.2、内容为空。

handleAdd() {
        this.dialogVisible = true
        this.addForm = {
          name: '',
          number: '',
          score: '',
          sex: ''
        }
      },

3、弹框【确定】:

3.1、动态数据表格插入新增数据;

3.2、全部数据表格插入新增数据;

3.3、关闭弹框。

handleOk() {
        this.tableData.unshift(this.addForm)
        this.allData.unshift(this.addForm)
        this.dialogVisible = false
      }

二、搜索(查)-filter过滤

1、【查询】按钮添加点击事件cilck;

<el-button type="primary" @click="handleSelect()">查询</el-button>

2、点击【查询】:

2.1、姓名查询:

 handleSelect() {
         this.tableData = this.allData.filter(item => {
            if (item.name.includes(this.formInline.name)) {
              return true
          }
        })
      }

2.2、学号查询:

handleSelect() {
 this.tableData = this.allData.filter(item => {
            if (item.number === this.formInline.number) {
              return true
          }
        })
}

2.3、姓名+学号查询:

 handleSelect() {
        //姓名+学号同时为空
        if (this.formInline.name === '' && this.formInline.number === '') {
          this.tableData = [...this.allData]
        } else if (this.formInline.name !== '' && this.formInline.number === '') {
          //姓名查询,学号为空
          this.tableData = this.allData.filter(item => {
            if (item.name.includes(this.formInline.name)) {
              return true
            }
          })
        } else if (this.formInline.name === '' && this.formInline.number !== '') {
          //学号查询,姓名为空
          this.tableData = this.allData.filter(item => {
            if (item.number === this.formInline.number) {
              return true
            }
          })
        } else if (this.formInline.name !== '' && this.formInline.number !== '') {
          //姓名+学号查询,都不为空
          this.tableData = this.allData.filter(item => {
            if (item.name.includes(this.formInline.name) && item.number === this.formInline.number) {
              return true
            }
          })
        }
      }

三、编辑(改)-splice替换

1、【编辑】按钮绑定点击事件;

当前行获取(scope)。

<el-button type="success" plain size="small" @click="handleEdit(scope)">编辑</el-button>

2、点击【编辑】:

  2.1、判断为非添加(编辑)状态;

      2.1.1、弹框标题为【编辑】;

      2.1.2、编辑状态姓名不可编辑;

<el-form-item label="姓名">
          <el-input v-model="addForm.name" :disabled="isView || !isAdd"></el-input>
</el-form-item>

 2.2、解构函数:{...scope.row};为了后面获取对象的index;

 2.3、打开弹框。

handleEdit(scope) {
        this.isView = false
        this.isAdd = false
        this.tkTitle = '编辑'
        this.addForm = { ...scope.row }
        this.dialogVisible = true
      },

3、点击【确定】:

  3.1、判断弹框状态是【添加】or【编辑】;

  3.2、获取index;

  3.3、找到表格index的一条,替换成修改后的当前弹框数据。

4、关闭弹框。

 handleOk() {
        //添加确定
        if (this.isAdd) {
          this.tableData.unshift(this.addForm)
          this.allData.unshift(this.addForm)
          this.dialogVisible = false
        } else {
          //编辑确定
          const index = this.tableData.findIndex(item => {
            return item.name = this.addForm.name
          })
          if (index !== -1) {
            this.tableData.splice(index, 1, this.addForm)
          }
          this.dialogVisible = false
          this.allData = [...this.tabledata]
        }

四、删除(删)-splice删除

1、【删除】按钮绑定点击事件;

<el-button type="warning" plain size="small" @click="handleDelete(scope)">删除</el-button>

2、点击【删除】:

 2.1、找到当前行的index;

 2.2、删除当前index对应的数据。

 handleDelete(scope) {
        const index = this.tableData.findIndex(item => {
          return item.name === scope.row.name
        })
        if (index !== -1) {
          this.tableData.splice(index, 1)
          this.allData =  [...this.tableData]
        }
      }

五、重置

1、【重置】添加点击事件cilck;

<el-button @click="handleReset()">重置</el-button>

2、点击【重置】:

2.1、查询条件为空;

2.2、表格内容显示全部:运用解构函数,allData数组浅拷贝给tableData数组。

handleReset() {
        this.formInline = {
          name: '',
          number: '',
          sex: ''
        }
        this.tableData = [...this.allData]
      }

六、查看

1、【查看】绑定点击事件click;

显示表格时,当前行数据的获取:slot-scope="scope"

<template slot-scope="scope">
          <el-button type="primary" plain size="small" @click="handleView(scope)">查看</el-button>
</template>

2、点击【查看】:

2.1、弹框是“查看”状态;

      2.1.1、弹框标题显示为“查看”;

      2.1.2、查看状态下,内容不可编辑;

2.2、弹框显示当前行数据;

2.3、打开弹框。

:title="tkTitle"

:disabled="isView"
handleView(scope) {
        this.isView = true
        this.tkTitle = '查看'
        this.addForm = scope.row        
        this.dialogVisible = true
      }

总结 

到此这篇关于vue前端实现表格数据增查改删功能的文章就介绍到这了,更多相关vue表格数据增查改删内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue+elementUl导入文件方式(判断文件格式)

    vue+elementUl导入文件方式(判断文件格式)

    这篇文章主要介绍了vue+elementUl导入文件方式(判断文件格式),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 详解Vue-cli中的静态资源管理(src/assets和static/的区别)

    详解Vue-cli中的静态资源管理(src/assets和static/的区别)

    这篇文章主要介绍了Vue-cli中的静态资源管理(src/assets和static/的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 浅析vue-router原理

    浅析vue-router原理

    这篇文章主要围绕Vue的SPA单页面设计展开。SPA(single page application):单一页面应用程序,有且只有一个完整的页面,对vue router原理感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • Vue中的this.$emit()方法详解

    Vue中的this.$emit()方法详解

    这篇文章主要给大家介绍了关于Vue中this.$emit()方法的相关资料,this.$emit()是 Vue.js 中一个很有用的方法,可以帮助子组件向父组件传递事件,需要的朋友可以参考下
    2023-09-09
  • 浅谈vue中resetFields()使用注意事项

    浅谈vue中resetFields()使用注意事项

    这篇文章主要介绍了浅谈vue中resetFields()使用注意事项,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Vue实现注册页面的用户交互详解

    Vue实现注册页面的用户交互详解

    这篇文章主要为大家详细介绍了Vue实现注册页面的用户交互的相关知识,文中的示例代码讲解详细,对我们深入掌握vue有一定的帮助,需要的小伙伴可以参考下
    2023-12-12
  • vue中渐进过渡效果实现

    vue中渐进过渡效果实现

    这篇文章主要为大家详细介绍了vue中渐进过渡效果的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 利用vue实现模态框组件

    利用vue实现模态框组件

    这篇文章主要为大家详细介绍了vue实现模态框组件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • vite打包出现 "default" is not exported by "node_modules/...问题解决办法

    vite打包出现 "default" is not exported by "

    这篇文章主要给大家介绍了关于vite打包出现 "default" is not exported by "node_modules/...问题的解决办法,文中通过代码将解决的办法介绍的非常详细,对同样遇到这个问题的朋友具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-06-06
  • vue.js实现简单计时器功能

    vue.js实现简单计时器功能

    这篇文章主要为大家详细介绍了vue.js实现简单计时器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论