vue 结合form表单对可编辑table进行校验问题

这个博客展示了一个使用Vue.js实现的表格组件,其中包括日期选择器、输入框等元素,并针对不同车间类型实现了不同的显示逻辑。表格数据与表单模型双向绑定,每个单元格的数据都遵循特定的验证规则,例如必须为整数。此外,代码还展示了如何通过`v-if`和`v-else`根据车间名称决定某些字段是否显示。
直接上代码:
<template>
<el-form
  ref="tableDataForm"
  :model="formData">
  <el-table
    :data="formData.tableDataAdd"
    border
    style="width: 100%; margin-top: 20px"
  >
    <el-table-column
      label="日期"
      width="170"
      prop="defDate"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <el-form-item
          style="margin-bottom: 0px"
          :prop="'tableDataAdd.' + scope.$index + '.beginTime'"
          :rules="rowRules.beginTime">
          <el-date-picker
            @change="weekChange(scope.row)"
            format="yyyy第WW周"
            v-model="scope.row.beginTime"
            size="small"
            type="week">
          </el-date-picker>
        </el-form-item>
      </template>
    </el-table-column>
    <el-table-column
      label="车间名称"
      width="150"
      prop="workShopName"
      align="center"
      header-align="center">
    </el-table-column>
    <el-table-column
      label="0.5μm标准值"
      width="150"
      prop="standardValueMin"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <el-form-item
          style="margin-bottom: 0px"
          :prop="'tableDataAdd.' + scope.$index + '.standardValueMin'"
          :rules="rowRules.standardValueMin">
          <el-input
            clearable
            v-model="scope.row.standardValueMin">
          </el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <el-table-column
      label="0.5μm监测值"
      width="150"
      prop="monitoringValueMin"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <el-form-item
          style="margin-bottom: 0px"
          :prop="'tableDataAdd.' + scope.$index + '.monitoringValueMin'"
          :rules="rowRules.monitoringValueMin">
          <el-input
            clearable
            v-model="scope.row.monitoringValueMin">
          </el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <el-table-column
      label="5μm标准值"
      width="150"
      prop="standardValueMax"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <el-form-item
          style="margin-bottom: 0px"
          :prop="'tableDataAdd.' + scope.$index + '.standardValueMax'"
          :rules="rowRules.standardValueMax">
          <el-input
            clearable
            v-model="scope.row.standardValueMax">
          </el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <el-table-column
      label="5μm监测值"
      width="150"
      prop="monitoringValueMax"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <el-form-item
          style="margin-bottom: 0px"
          :prop="'tableDataAdd.' + scope.$index + '.monitoringValueMax'"
          :rules="rowRules.monitoringValueMax">
          <el-input
            clearable
            v-model="scope.row.monitoringValueMax">
          </el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <el-table-column
      label="气压监测值"
      width="150"
      prop="pressureStandardValue"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <div v-if="scope.row.workShopName === '电子材料库'">
          {{scope.row.pressureStandardValue}}
        </div>
        <div v-else>
          <el-form-item
            style="margin-bottom: 0px"
            :prop="'tableDataAdd.' + scope.$index + '.pressureStandardValue'"
            :rules="rowRules.pressureStandardValue">
            <el-input
              clearable
              v-model="scope.row.pressureStandardValue">
            </el-input>
          </el-form-item>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="温度上限"
      width="150"
      prop="temperatureUpperLimit"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <div v-if="scope.row.workShopName === '电子材料库'">
        </div>
        <div v-else>
          <el-form-item
            style="margin-bottom: 0px"
            :prop="'tableDataAdd.' + scope.$index + '.temperatureUpperLimit'"
            :rules="rowRules.temperatureUpperLimit">
            <el-input
              clearable
              v-model="scope.row.temperatureUpperLimit">
            </el-input>
          </el-form-item>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="温度下限"
      width="150"
      prop="temperatureLowerLimit"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <div v-if="scope.row.workShopName === '电子材料库'">
        </div>
        <div v-else>
          <el-form-item
            style="margin-bottom: 0px"
            :prop="'tableDataAdd.' + scope.$index + '.temperatureLowerLimit'"
            :rules="rowRules.temperatureLowerLimit">
            <el-input
              clearable
              v-model="scope.row.temperatureLowerLimit">
            </el-input>
          </el-form-item>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="湿度上限"
      width="150"
      prop="humidityUpperLimit"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <div v-if="scope.row.workShopName === '电子材料库'">
        </div>
        <div v-else>
          <el-form-item
            style="margin-bottom: 0px"
            :prop="'tableDataAdd.' + scope.$index + '.humidityUpperLimit'"
            :rules="rowRules.humidityUpperLimit">
            <el-input
              clearable
              v-model="scope.row.humidityUpperLimit">
            </el-input>
          </el-form-item>
        </div>
      </template>
    </el-table-column>
    <el-table-column
      label="湿度下限"
      width="150"
      prop="humidityLowerLimit"
      align="center"
      header-align="center">
      <template slot-scope="scope">
        <div v-if="scope.row.workShopName === '电子材料库'">
        </div>
        <div v-else>
          <el-form-item
            style="margin-bottom: 0px"
            :prop="'tableDataAdd.' + scope.$index + '.humidityLowerLimit'"
            :rules="rowRules.humidityLowerLimit">
            <el-input
              clearable
              v-model="scope.row.humidityLowerLimit">
            </el-input>
          </el-form-item>
        </div>
      </template>
    </el-table-column>
  </el-table>
</el-form>

</template>

<script>
export default {
data () {
const validInteger = (rule, value, callback) => {
  let r = /^\+?[1-9][0-9]*$/;  //正整数
  if (!r.test(value)) {
    callback(new Error('不是整数'))
  } else {
    callback()
  }
}

      return {
formData: {
  tableDataAdd: [
    {
      actualOneData: null,
    }
  ],
},
rowRules: {
  beginTime: [
    { required: true, message: '必填项', trigger: 'change' },
  ],
  lineName: [
    { required: true, message: '必填项', trigger: 'change' },
  ],
  actualOneData: [
    { required: true, validator: validInteger, trigger: 'change' },
  ],
},
        }
}
}
</script>

根据提供的引用内容,ant-design-vue可以通过内置的Form组件和Table组件实现表格内部字段验证功能。具体步骤如下: 1.表格中添加需要验证的字段,例如下面的代码中的name和age字段: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> </a-form> <a-table :columns="columns" :dataSource="dataSource" :pagination="false" /> ``` 2.表格中添加操作列,例如下面的代码中的操作列包含了编辑和删除按钮: ```html <a-table :columns="columns" :dataSource="dataSource" :pagination="false"> <template #action="text, record"> <a-button @click="edit(record)">Edit</a-button> <a-button @click="delete(record)">Delete</a-button> </template> </a-table> ``` 3. 在编辑操作中打开表单,并将当前行的数据绑定到表单中: ```javascript edit(record) { this.form.setFieldsValue(record); this.editingKey = record.key; } ``` 4.表单中添加保存按钮,并在点击保存按钮时进行表单验证和数据更新: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> <a-form-item> <a-button type="primary" @click="save">Save</a-button> </a-form-item> </a-form> ``` ```javascript save() { this.form.validateFields((err, values) => { if (!err) { const newData = [...this.dataSource]; const index = newData.findIndex((item) => this.editingKey === item.key); if (index > -1) { const item = newData[index]; newData.splice(index, 1, { ...item, ...values }); this.dataSource = newData; this.editingKey = ''; } else { newData.push(values); this.dataSource = newData; this.editingKey = ''; } } }); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值