Element-Puls Form表单内嵌套el-table表格,根据表格复选框多选或单选动态设置行的验证规则

需求

根据 Table 表格内的复选框来控制当前选中行是否添加必填校验规则

效果图

实现思想

我们需要设置一个 flag 来标识已勾选的行,el-table渲染数据结构是数组对象形式,我们可以在每个对象中手动加如一个标识,例如默认:selected : false,如你的源数据中已有类似key,则可用它作于唯一标识

重要代码部分

html代码

html 复制代码
<template>
  <div>
    <el-form :model="productInfoForm" ref="productInfoFormRef" :rules="productInfoRules">
      <el-table ref="multipleTableRef" :data="productInfoForm.skuList" @select-all="handleSelectionChangeAll"
        @select="handleSelection" @selection-change="handleSelectionChange">
        <!-- 复选框 -->
        <el-table-column type="selection" width="55" />
        <!-- 需要动态设置校验项 -->
        <el-table-column>
          <template #header>
            <div>
              <span v-text="`您的售价`"></span>
            </div>
          </template>
          <template #default="scope">
            <div>
              <el-form-item :ref="`unitPrice_${scope.$index}`" :prop="`skuList.${scope.$index}.unitPrice`"
                :rules="getRules(scope.row.selected, scope.$index, 'unitPrice')">
                <div class="flex-box">
                  <p class="fs-12 flex-shrink" v-text="`产品单价`"></p>
                  <el-input v-model="scope.row.unitPrice" placeholder="0"
                    @blur="(e: Event) => { scope.row.unitPrice = (e.target as HTMLInputElement).value }">
                    <template #prefix><span v-text="`¥`"></span></template>
                  </el-input>
                </div>
              </el-form-item>

              <el-form-item :ref="`operationFee_${scope.$index}`" :prop="`skuList.${scope.$index}.operationFee`"
                :rules="getRules(scope.row.selected, scope.$index, 'operationFee')">
                <div class="flex-box">
                  <p class="fs-12 flex-shrink" v-text="`操作费`"></p>
                  <el-input v-model="scope.row.operationFee" placeholder="0"
                    @blur="(e: Event) => { scope.row.operationFee = (e.target as HTMLInputElement).value }">
                    <template #prefix><span v-text="`¥`"></span></template>
                  </el-input>
                </div>
              </el-form-item>

              <el-form-item :ref="`finalDeliveryFee_${scope.$index}`" :prop="`skuList.${scope.$index}.finalDeliveryFee`"
                :rules="getRules(scope.row.selected, scope.$index, 'finalDeliveryFee')">
                <div class="flex-box">
                  <p class="fs-12 flex-shrink" v-text="`尾程派送费`"></p>
                  <el-input v-model="scope.row.finalDeliveryFee" placeholder="0"
                    @blur="(e: Event) => { scope.row.finalDeliveryFee = (e.target as HTMLInputElement).value }">
                    <template #prefix><span v-text="`¥`"></span></template>
                  </el-input>
                </div>
              </el-form-item>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 操作按钮 -->
      <div>
        <el-button :disabled="createProductDisabled" plain v-text="`保存`" @click="saveProduct"></el-button>
        <el-button :disabled="createProductDisabled" type="primary" v-text="`提交审核`" @click="saveProduct"></el-button>
      </div>
    </el-form>
  </div>
</template>

js代码

TypeScript 复制代码
<script setup lang="ts">
import { ElForm } from 'element-plus';
import { ref, reactive, toRefs, computed, getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const productInfoFormRef = ref(ElForm)
const multipleTableRef = ref<any>()

const data = reactive<any>({
  productInfoForm: {
    skuList: [
      {
        selected: false,
        unitPrice: '',
        operationFee: '',
        finalDeliveryFee: '',
      }
    ]
  },
  productInfoRules: {
    unitPrice: {
      required: true,
      message: '请完善必须项',
      tirgger: ['blur', 'change']
    },
    operationFee: {
      required: true,
      message: '请完善必须项',
      tirgger: ['blur', 'change']
    },
    finalDeliveryFee: {
      required: true,
      message: '请完善必须项',
      tirgger: ['blur', 'change']
    },
  },
  createProductDisabled: true,
});
const { productInfoForm, productInfoRules, createProductDisabled } = toRefs(data);

// 根据flag动态设置表单校验规则
const getRules = computed(() => (selected: boolean, index: number, type: string) => {
  nextTick(() => {
    if (proxy?.$refs[`${type}_${index}`] && !selected) {
      (proxy?.$refs[`${type}_${index}`] as any).clearValidate()
    }
    // console.log(proxy?.$refs[`${type}_${index}`]);
  })
  return selected ? productInfoRules.value[type] : {}
})

//当选择项发生变化时会触发该事件
const handleSelectionChange = (valArr: any[]) => {
  createProductDisabled.value = valArr.length ? false : true
}

//当用户手动勾选数据行的 Checkbox 时触发的事件
const handleSelection = (valArr: any[], row: { selected: boolean; }) => {
  row.selected = !row.selected
}

//监听table全选
const handleSelectionChangeAll = (valArr: any[]) => {
  const skuList = productInfoForm.value.skuList
  skuList.forEach((i: { selected: boolean; }) => {
    i.selected = valArr.length ? true : false
  })
}

//创建并提交审核商品
const saveProduct = async () => {
  productInfoFormRef.value.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      // 表单验证通过后相关逻辑处理...

    } else {
      console.log('error submit!', fields);
    }
  })
}
</script>

<style scoped lang="scss"></style>
相关推荐
QGC二次开发4 分钟前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
云草桑16 分钟前
逆向工程 反编译 C# net core
前端·c#·反编译·逆向工程
布丁椰奶冻21 分钟前
解决使用nvm管理node版本时提示npm下载失败的问题
前端·npm·node.js
Leyla1 小时前
【代码重构】好的重构与坏的重构
前端
影子落人间1 小时前
已解决npm ERR! request to https://registry.npm.taobao.org/@vant%2farea-data failed
前端·npm·node.js
世俗ˊ1 小时前
CSS入门笔记
前端·css·笔记
子非鱼9211 小时前
【前端】ES6:Set与Map
前端·javascript·es6
6230_1 小时前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
想退休的搬砖人1 小时前
vue选项式写法项目案例(购物车)
前端·javascript·vue.js
加勒比海涛2 小时前
HTML 揭秘:HTML 编码快速入门
前端·html