elementPlus组件之表格编辑并校验功能实现

 更新时间:2024年12月05日 10:36:36   作者:码喽的自我修养  
本文详细介绍了如何使用Element Plus组件实现表格的单条数据新增、编辑、删除操作,并对数据进行校验,代码简单易懂,感兴趣的朋友跟随小编一起看看吧

​功能描述

在表格中可以进行单条数据的新增、编辑、删除操作,并能对数据进行校验。具体如下图

功能解析

  • 表格和表单切换,表单嵌套表格,通过v-if判断当前行的数据是否切换为表单组件。
  • 表格编辑定位,通过自带的scope.$index做定位具体是哪条数据做编辑操作,并将该条数据解构赋值给form
  • 取消后数据还原,因为form数据是解构后的,直接取消编辑状态即可。如果是新增状态下的取消,则需要删除最后一条数据。
  • 新增数据,初始化form数据,并push到表格数据

功能实现

<template>
  <el-form ref="tableFormRef" :model="table.form" :rules="table.rules" status-icon>
    <el-table :data="table.data" border stripe>
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="min" label="下限值(kwh)">
        <template #default="scope">
          <el-form-item v-if="scope.$index === table.idx" prop="min">
            <el-input v-model.number="table.form.min" placeholder="请输入下限值" />
          </el-form-item>
          <span v-else>{{ scope.row.min }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="max" label="上限值(kwh)">
        <template #default="scope">
          <el-form-item v-if="scope.$index === table.idx" prop="max">
            <el-input v-model.number="table.form.max" placeholder="请输入上限值" />
          </el-form-item>
          <span v-else>{{ scope.row.max }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="单价(元)">
        <template #default="scope">
          <el-form-item v-if="scope.$index === table.idx" prop="price">
            <el-input v-model.number="table.form.price" placeholder="请输入单价" />
          </el-form-item>
          <span v-else>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <template v-if="scope.$index === table.idx">
            <el-button @click="saveItem(scope.$index)">保存</el-button>
            <el-button @click="cancelItem(scope.$index)">取消</el-button>
          </template>
          <template v-else>
            <el-button @click="editItem(scope.$index, scope.row)">编辑</el-button>
            <el-button @click="deleteItem(scope.$index)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="add-table-item" @click="addItem">新增</div>
  </el-form>
</template>
<script setup>
// 校验规则最小数
const rangeNumberMin = (rule, value, callback) => {
  console.log(value >= table.form.max, value, table.form.max)
  if (value < 0) {
    callback(new Error('数字不能为负数'))
  } else if (value >= table.form.max) {
    callback(new Error('下限值不能大于或等于上限值'))
  } else {
    callback()
  }
}
// 校验规则最大数
const rangeNumberMax = (rule, value, callback) => {
  if (value < 0) {
    callback(new Error('数字不能为负数'))
  } else if (value <= table.form.min) {
    callback(new Error('上限值不能小于或等于下限值'))
  } else {
    callback()
  }
}
// 表格表单数据
const table = reactive({
  data: [ // 表格数据
    { price: 100, max: 100, min: 0 },
    { price: 100, max: 200, min: 0 },
    { price: 100, max: 100, min: 10 },
    { price: 100, max: 200, min: 30 },
    { price: 100, max: 100, min: 50 }
  ],
  idx: -1, // 编辑第几条数据(-1则为不编辑)
  isAdd: false, // 是否为新增数据,如果为新增,取消时则需要做删除操作
  form: { // 表单数据,单独处理是为了方便取消操作和规则校验
    price: '',
    max: '',
    min: ''
  },
  rules: { // 规则校验
    min: [
      { required: true, message: '请输入下限值', trigger: 'blur' },
      { validator: rangeNumberMin, trigger: 'blur' }
    ],
    max: [
      { required: true, message: '请输入上限值', trigger: 'blur' },
      { validator: rangeNumberMax, trigger: 'blur' }
    ],
    price: [{ required: true, message: '请输入价格限值', trigger: 'blur' }]
  }
})
// 新增
const addItem = () => {
  if (table.idx > -1) {
    return
  }
  table.isAdd = true // 标记,取消后会删除编辑数据
  table.idx = table.data.length // 编辑最新条数
  table.data.push(table.form)
}
// 编辑
const editItem = (idx, val) => {
  if (table.idx > -1) {
    return
  }
  table.isAdd = false // 防止取消后删除编辑数据
  table.idx = idx // 编辑当前
  table.form = { ...val } // 将需要编辑的数据解构赋值给表单,此处是做了深拷贝,如果数据复杂,需要另外做深拷贝处理
}
// 取消
const cancelItem = idx => {
  // 如果是新增状态下,则删除编辑数据
  if (table.isAdd) {
    table.data.splice(idx, 1)
  }
  // 初始化数据
  table.form = {
    price: '',
    max: '',
    min: ''
  }
  table.idx = -1
  table.isAdd = false
}
// 删除
const deleteItem = idx => {
  if (table.idx > -1) {
    return
  }
  table.data.splice(idx, 1)
}
const tableFormRef = ref()
const saveItem = idx => {
  tableFormRef.value.validate(valid => {
    if (valid) {
      // 替换插入数据,如数据解构复杂,请自行做深拷贝操作
      table.data.splice(idx, 1, { ...table.form })
      // 初始化数据
      table.form = {
        price: '',
        max: '',
        min: ''
      }
      table.idx = -1
      table.isAdd = false
    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>
<style lang="scss" scoped>
.add-table-item {
  width: 100%;
  height: 49px;
  border-width: 0px 1px 1px 1px;
  border-style: solid;
  border-color: var(--el-border-color-lighter);
  text-align: center;
  line-height: 49px;
  background-color: var(--el-fill-color-lighter);
  cursor: pointer;
}
</style>
 

到此这篇关于elementPlus组件之表格编辑并校验的文章就介绍到这了,更多相关elementPlus表格编辑并校验内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)

    vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)

    这篇文章主要介绍了vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程,需要的朋友可以参考下
    2018-01-01
  • Vue3+Vite使用双token实现无感刷新

    Vue3+Vite使用双token实现无感刷新

    本文主要介绍了Vue3+Vite使用双token实现无感刷新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 关于vue-router路径计算问题

    关于vue-router路径计算问题

    这篇文章主要介绍了关于vue-router路径计算问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 详解关于vue-area-linkage走过的坑

    详解关于vue-area-linkage走过的坑

    这篇文章主要介绍了详解关于vue-area-linkage走过的坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Vue eventBus事件总线封装后再用的方式

    Vue eventBus事件总线封装后再用的方式

    EventBus称为事件总线,当两个组件属于不同的两个组件分支,或者两个组件没有任何联系的时候,不想使用Vuex这样的库来进行数据通信,就可以通过事件总线来进行通信,这篇文章主要给大家介绍了关于Vue eventBus事件总线封装后再用的相关资料,需要的朋友可以参考下
    2022-06-06
  • el-form表单实现校验的示例代码

    el-form表单实现校验的示例代码

    本文主要介绍了el-form表单实现校验的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • vue解决使用$http获取数据时报错的问题

    vue解决使用$http获取数据时报错的问题

    今天小编就为大家分享一篇vue解决使用$http获取数据时报错的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • Vue中用props给data赋初始值遇到的问题解决

    Vue中用props给data赋初始值遇到的问题解决

    这篇文章主要介绍了Vue中用props给data赋初始值遇到的问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • vue创建项目卡住不动,vue create project卡住不动的解决

    vue创建项目卡住不动,vue create project卡住不动的解决

    这篇文章主要介绍了vue创建项目卡住不动,vue create project卡住不动的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue中html2canvas给指定区域添加满屏水印

    vue中html2canvas给指定区域添加满屏水印

    本文主要介绍了vue中html2canvas给指定区域添加满屏水印,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11

最新评论