element-ui中的表格与表单混用方法及表单校验

效果图

只是简单写一个样式和使用方法,剩下的可以按照自己所学的知识进行扩展! 


<template>
  <div>
    <el-container>
      <el-header>
        <el-row>
          <el-form :model="tableData" :rules="tableData.Rules" ref="tableData">
            <el-row>
              <el-table :data="tableData.data" border style="width: 100%">
                <el-table-column prop="date" label="时间" align="center">
                </el-table-column>
                <el-table-column
                  align="center"
                  label="工作时长(hr)"
                  prop="workTime"
                >
                <!--
                 scope的其他使用方法
                 <template slot-scope="scope">
                   {{scope.row.workTime}}
                  </template>
                -->
                </el-table-column>
                <el-table-column prop="aBox" align="center" label="A仓库">
                </el-table-column>
                <el-table-column prop="bBox" align="center" label="B仓库">
                </el-table-column>

                <el-table-column align="center" label="A仓库人数">
                  <template slot-scope="scope">
                    <el-form-item
                      :rules="tableData.Rules.Ack"
                      :prop="'data.' + scope.$index + '.Ack'"
                    >
                      <el-input v-model="scope.row.Ack" size="small"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="B仓库人数">
                  <template slot-scope="scope">
                    <el-form-item
                      :rules="tableData.Rules.Bck"
                      :prop="'data.' + scope.$index + '.Bck'"
                    >
                      <el-input v-model="scope.row.Bck" size="small"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>
            </el-row>
          </el-form>
          <el-row>
            <el-button size="small" type="danger" @click="ScheduleBudget()"
              >发送</el-button
            >
          </el-row>
        </el-row>
      </el-header>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: {
        data: [
          {
            date: "2022/04/02",
            workTime: "8",
            aBox: "200台",
            bBox: "158台",
          },
        ],
        Rules: {
          Ack: [{ required: true, message: "必填", trigger: "blur" }],
          Bck: [{ required: true, message: "必填", trigger: "blur" }],
        },
      },
    };
  },
  methods: {
    ScheduleBudget() {
      this.$refs["tableData"].validate((valid) => {
        if (valid) {
          this.$message({
            showClose: true,
            message: "submit!!",
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: "error submit!!",
            type: "error",
          });
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
</style>

对于初始化数据请求,调用接口请求得到的数据渲染到表格里面,数据记得进行深浅拷贝获得数据

JSON.parse(JSON.stringify(this.tableData.data))

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值