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>


目录
相关文章
|
22天前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
158 2
|
3月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
127 0
|
3月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
269 1
|
4月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
601 0
|
6月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
809 4
|
4月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
5月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
568 77
|
6月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
4月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
384 17
下一篇
日志分析软件