element table表格支持添加编辑校验

实现效果:

将table表格与form表单结合使用 (用el-form外层包裹el-table结合rules进行校验)

代码实现

<template>
  <div>
    <el-card class="box-card" shadow="never">
      <div>
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="AddData"
          >添加</el-button
        >
      </div>
    </el-card>
    <el-card class="box-card" shadow="never">
      <el-form ref="Issuerform" :model="Issuerform">
        <el-table :data="Issuerform.tableIssuerData" border>
          <el-table-column
            prop="Name"
            label="简称"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span v-if="!scope.row.editCell">{{ scope.row.Name }}</span>
              <el-form-item
                v-else
                :prop="'tableIssuerData.' + scope.$index + '.Name'"
                :rules="rules.Name"
              >
                <el-autocomplete
                  ref="autocomplete"
                  v-model="scope.row.Name"
                  class="querySearch"
                  style="width: 100%"
                  size="mini"
                  clearable
                  :fetch-suggestions="querySearchAsync"
                  placeholder="请输入搜索内容"
                  :trigger-on-focus="false"
                  @select="handleSelect"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="Code"
            label="代码"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span v-if="!scope.row.editCell">{{ scope.row.Code }}</span>
              <el-form-item
                v-else
                :prop="'tableIssuerData.' + scope.$index + '.Code'"
                :rules="rules.Code"
              >
                <el-input size="mini" v-model="scope.row.Code"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="issuer"
            label="主体"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            prop="Type"
            label="类型"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span v-if="!scope.row.editCell">
                {{ scope.row.Type }}
              </span>
              <el-form-item
                v-else
                :prop="'tableIssuerData.' + scope.$index + '.Type'"
                :rules="rules.Type"
              >
                <el-select
                  v-model="scope.row.Type"
                  placeholder="请选择"
                  clearable
                  size="mini"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in TypeOptions"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                v-if="scope.row.editCell"
                @click="issuerSave(scope.row, scope.$index)"
              >
                保存
              </el-button>
              <el-button
                size="small"
                type="text"
                v-if="!scope.row.editCell"
                @click="issuerEdit(scope.row, scope.$index)"
              >
                编辑
              </el-button>
              <el-button
                size="small"
                type="text"
                @click="issuerdelete(scope.row, scope.$index)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-card>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      Issuerform: {
        tableIssuerData: [
          {
            Name: null,
            Code: null,
            issuer: null,
            Type: null,
            editCell: true,
          },
        ],
      },
      TypeOptions: [
        {
          value: "1",
          name: "类型1",
        },
        {
          value: "2",
          name: "类型2",
        },
        {
          value: "3",
          name: "类型3",
        },
      ],
      rules: {
        Name: [{ required: true, message: "", trigger: "blur" }],
        Code: [{ required: true, message: "", trigger: "blur" }],
        Type: [{ required: true, message: "", trigger: "change" }],
      },
    };
  },
  methods: {
    AddData() {
      this.Issuerform.tableIssuerData.push({
        Name: null,
        Code: null,
        issuer: null,
        Type: null,
        editCell: true,
      });
    },
    //模糊搜索
    querySearchAsync(queryString, cb) {
      let param = {
        name: queryString,
      };
      console.log(param, cb);
    },
    handleSelect() {},
    issuerSave(row, index) {
      this.$refs.Issuerform.validate((valid) => {
        if (valid) {
          this.Issuerform.tableIssuerData[index].editCell = false;
        } else {
          return false;
        }
      });
    },
    issuerEdit(row, index) {
      this.Issuerform.tableIssuerData[index].editCell = true;
    },
    issuerdelete(row, index) {
      this.Issuerform.tableIssuerData.splice(index, 1);
    },
  },
};
</script>
<style scoped lang='less'>
/deep/.el-form-item__error {
  display: none; //隐藏校验提示
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 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` 等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值