Ant Design Vue 框架的a-table嵌套a-form-model达到验证效果

简介: Ant Design Vue 框架的a-table嵌套a-form-model达到验证效果

Ant Design Vue 框架的a-table嵌套a-form-model达到验证效果

注:开发环境vue2,ant版本1.7.8

需要先了解表头和内容自定义插槽

https://blog.csdn.net/weixin_41620505/article/details/125886643

需求:动态添加item并校验输入框



1、script中创建表头

<script>
  export default {
  data() {
      return {
        // 表单参数
        form: {
          modelName: '',
          modelNote: '',
          editList: [{
            value1: 0,
            value2: 10,
            level: '',
            note: '',
            market: ''
          }]
        },
        rules: {
          modelName: Validate.requiredRules('名称', ['change', 'blur']),
          modelNote: Validate.requiredRules('说明', ['change', 'blur']),
          level: [...Validate.requiredRules('等级', ['change', 'blur']),
            {
              validator: (rule, value, callback) => {
                let findItem = this.form.editList.filter(item => item.level === value)
                if (findItem.length > 1) {
                  callback(new Error('等级重复'))
                } else {
                  callback()
                }
              }, trigger: ['change', 'blur']
            }],
        },
         columns: [
          { 
            dataIndex: 'value',
            slots: { title: 'titleValue' },//表头插槽
            scopedSlots: { customRender: 'value' },//表格内容插槽
            align: 'center',
            width: 240,
          {
            dataIndex: 'level',
            scopedSlots: { customRender: 'level' },
            slots: { title: 'titleLevel' },
            align: 'center'
          },
          {
            dataIndex: 'note',
            scopedSlots: { customRender: 'note' },
            slots: { title: 'titleNote' },
            align: 'center'
          },
          {
            title: '备注',
            dataIndex: 'market',
            scopedSlots: { customRender: 'market' },
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'operation',
            scopedSlots: { customRender: 'operation' },
            align: 'center',
            width: 50
          }
     ]
   }
  }
 }
    </script>

2、在template中引用

<template>
  <div>
   <a-form-model
        :colon="false"
        class="form"
        ref="form"
        :model="form"
        :rules="rules"
      >
      <a-table
              :rowKey="
      (record, index) => {
        return index
      }"
              :columns="columns"
              :data-source="form.editList"
              :pagination="false">
<!-- 自定义表头-->
   <span slot="titleValue" class="form-table-heard">
         分值区间
      </span>
              <span slot="titleLevel" class="form-table-heard">
         评价等级
      </span>
             <span slot="titleNote" class="form-table-heard">
         评价说明
      </span>
<!--自定义内容-->
        <span slot="value" slot-scope="text, record,index">
                     <a-form-model-item class="item" :prop="'editList.'+index+'.value1'" :rules="rules.value1">
                                <a-input-number class="item" v-model="record.value1" size="small" :max="100" :min="0"
                                                :formatter="value => `${Number.parseInt(value||0).toFixed(0)}`"
                                />
                              </a-form-model-item>
                     <span class="item-line"/>
                        <a-form-model-item class="item" :prop="'editList.'+index+'.value2'"
                                           :rules="rules.value2">
                                <a-input-number class="item" :max="100" :min="0" v-model="record.value2" size="small"
                                                :formatter="value => `${Number.parseInt(value||0).toFixed(0)}`"
                                />
                     </a-form-model-item>
                       </span>
              <template slot="level" slot-scope="text, record,index">
                <a-form-model-item :prop="'editList.'+index+'.level'" :rules="rules.level">
                  <a-input v-model="record.level" placeholder="请输入" :disabled="disabled" size="small"/>
                </a-form-model-item>
              </template>
              <template slot="note" slot-scope="text, record,index">
                <a-form-model-item :prop="'editList.'+index+'.note'" :rules="rules.note">
                  <a-input v-model="record.note" placeholder="请输入" :disabled="disabled" size="small"/>
                 </a-form-model-item>
              </template>
              <template slot="market" slot-scope="text, record,index">
                <a-form-model-item>
                  <a-input v-model="record.market" placeholder="请输入" :disabled="disabled" size="small"/>
                </a-form-model-item>
              </template>
          <span slot="operation" slot-scope="text, record,index">
            <a-button name="删除" btnType="primary" :isDanger="true" 
                  @click="handleDelete(index)"/>
             </span>
   </a-table>
      </a-form-model>
  </div>
</template>
<style lang="less" scoped>
 .form-table-heard:before {
      content: '*';
      color: red;
    }
</style>


目录
相关文章
|
3月前
|
JavaScript 容器
乾坤qiankun框架搭建 主应用为vue3的项目。
乾坤qiankun框架搭建 主应用为vue3的项目。
263 2
|
2月前
|
缓存 监控 JavaScript
Vue.js 框架下的性能优化策略与实践
Vue.js 框架下的性能优化策略与实践
|
3月前
|
JavaScript 前端开发 API
Vue.js:现代前端开发的强大框架
【10月更文挑战第11天】Vue.js:现代前端开发的强大框架
107 41
|
2月前
|
JavaScript 前端开发 开发者
前端框架对比:Vue.js与Angular的优劣分析与选择建议
【10月更文挑战第27天】在前端开发领域,Vue.js和Angular是两个备受瞩目的框架。本文对比了两者的优劣,Vue.js以轻量级和易上手著称,适合快速开发小型到中型项目;Angular则由Google支持,功能全面,适合大型企业级应用。选择时需考虑项目需求、团队熟悉度和长期维护等因素。
77 1
|
2月前
|
前端开发 数据库
芋道框架审批流如何实现(Cloud+Vue3)
芋道框架审批流如何实现(Cloud+Vue3)
221 3
|
2月前
|
JavaScript 前端开发 API
前端框架对比:Vue.js与Angular的优劣分析与选择建议
【10月更文挑战第26天】前端技术的飞速发展让开发者在构建用户界面时有了更多选择。本文对比了Vue.js和Angular两大框架,介绍了它们的特点和优劣,并给出了在实际项目中如何选择的建议。Vue.js轻量级、易上手,适合小型项目;Angular结构化、功能强大,适合大型项目。
81 1
|
3月前
|
JavaScript UED
Vue + ElementUI 实现动态添加和删除表单项的多层嵌套表单
【10月更文挑战第5天】本示例展示了如何在 Vue.js 中使用 Element UI 组件实现动态添加和删除嵌套表单项。该表单包含设备信息、设备部位及其对应的任务列表,支持用户动态添加设备部位和任务,并提供相应的表单验证规则。
352 0
Vue + ElementUI 实现动态添加和删除表单项的多层嵌套表单
|
3月前
|
JavaScript 前端开发 API
|
21天前
|
JavaScript
vue使用iconfont图标
vue使用iconfont图标
111 1
|
1天前
|
存储 设计模式 JavaScript
Vue 组件化开发:构建高质量应用的核心
本文深入探讨了 Vue.js 组件化开发的核心概念与最佳实践。
10 1