element-ui table表格行内编辑加校验

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
  <div id="app">
    <el-form ref="tableFrom" :model="tableFrom" :rules="tableFromRules">
      <el-table :data="tableFrom.tableData" style="width: 100%;">
        <el-table-column label="ID" min-width="180">
          <template slot-scope="scope">
            <el-form-item :prop="'tableData.' + scope.$index + '.id'" :rules="tableFromRules.id">
              <el-input v-model="scope.row.id"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="name" min-width="180">
          <template slot-scope="scope">
            <el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="tableFromRules.name">
              <el-input v-model="scope.row.name"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80">
          <template slot-scope="scope">
            <el-button type="text" @click="insert(scope.$index)">插入当前行</el-button>
            <el-button type="text" @click="del(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="success" icon="el-icon-plus" plain style="margin-top: 10px; width: 100%;" @click="add()">添加
      </el-button>
    </el-form>
  </div>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        tableFrom: {
          tableData: []
        },
        tableFromRules: {
          id: [{
            required: true,
            message: '请输入id',
            trigger: 'blur'
          }],
          name: [{
            required: true,
            message: '请输入name',
            trigger: 'blur'
          }]
        }
      },
      mounted() {},
      methods: {
        add() {
          this.tableFrom.tableData.push({
            id: '',
            name: ''
          })
        },
        insert(index) {
          this.tableFrom.tableData.splice(index, 0, {
            id: '',
            name: ''
          })
        },
        del(index) {
          this.tableFrom.tableData.splice(index, 1)
        }
      }
    })
  </script>
</body>

</html>

在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于 Element UI编辑表格表单校验,你可以使用 Element UI 提供的表单校验规则来实现。 首先,你需要在表单的每个字段中定义校验规则。例如,如果要校验一个输入框输入的内容是否为非空字符串,你可以使用 `required` 规则。在编辑表格中,你可以通过在表格列中设置 `rules` 属性来定义校验规则。 下面是一个简单的示例,展示了如何在 Element UI表格中使用校验规则: ```html <template> <el-table :data="tableData" style="width: 100%"> <el-table-column label="姓名" prop="name"> <template slot-scope="scope"> <el-form-item :prop="'name.' + scope.$index" :rules="nameRules"> <el-input v-model="scope.row.name"></el-input> </el-form-item> </template> </el-table-column> <el-table-column label="年龄" prop="age"> <template slot-scope="scope"> <el-form-item :prop="'age.' + scope.$index" :rules="ageRules"> <el-input v-model.number="scope.row.age"></el-input> </el-form-item> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 20 }, { name: 'Jane', age: null }, { name: '', age: 30 } ], nameRules: [ { required: true, message: '请输入姓名', trigger: 'blur' } ], ageRules: [ { required: true, message: '请输入年龄', trigger: 'blur' }, { type: 'number', message: '年龄必须为数字', trigger: 'blur' } ] }; } }; </script> ``` 在上面的示例中,我们在姓名和年龄字段的 `el-form-item` 中分别设置了校验规则。 `nameRules` 定义了姓名字段的校验规则,要求输入不能为空; `ageRules` 定义了年龄字段的校验规则,要求输入不能为空且必须为数字。 你可以根据实际需求,定义更多的校验规则。除了 `required` 和 `type` 规则之外,还可以使用其他内置的规则或自定义规则来满足你的需求。 当用户在表格编辑数据时,Element UI 会自动触发校验规则,并在不满足规则时显示相应的错误提示信息。你可以根据需求设置校验触发的时机,如 `blur`(失去焦点时触发)、`change`(值发生改变时触发)等。 注意:以上示例是基于 Element UI 2.x 版本的。如果你使用的是 Element Plus,则使用方式类似,只需将组件名换成 `el-input`、`el-table` 等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值