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>


目录
相关文章
|
1天前
|
JavaScript
vue2_引入Ant design vue
vue2_引入Ant design vue
5 0
|
2天前
|
JavaScript 索引
【vue】框架搭建
【vue】框架搭建
6 1
|
7天前
|
JavaScript
Ant design Vue 父子组件传递(代码案例--不懂的地方可以私信本博主)
Ant design Vue 父子组件传递(代码案例--不懂的地方可以私信本博主)
11 0
|
7天前
|
JavaScript 前端开发
ant design vue 配置菜单外部打开
ant design vue 配置菜单外部打开
14 0
|
7天前
|
JavaScript
ant design vue 在列表中使用插槽 例如当性别为0的时候在列表中我想显示男
ant design vue 在列表中使用插槽 例如当性别为0的时候在列表中我想显示男
8 0
|
1天前
|
JavaScript
【vue】 父组件调用子组件方法
【vue】 父组件调用子组件方法
6 0
|
1天前
|
JavaScript
【vue】 如何给封装好的组件加click事件
【vue】 如何给封装好的组件加click事件
6 1
|
1天前
|
存储 JavaScript 前端开发
【Vue】绝了!这生命周期流程真...
【Vue】绝了!这生命周期流程真...
|
2天前
|
JavaScript 前端开发 容器
< 每日小技巧: 基于Vue状态的过渡动画 - Transition 和 TransitionGroup>
Vue 的 `Transition` 和 `TransitionGroup` 是用于状态变化过渡和动画的组件。`Transition` 适用于单一元素或组件的进入和离开动画,而 `TransitionGroup` 用于 v-for 列表元素的增删改动画,支持 CSS 过渡和 JS 钩子。
< 每日小技巧: 基于Vue状态的过渡动画 - Transition 和 TransitionGroup>
|
2天前
|
JavaScript
【vue】setInterval的嵌套实例
【vue】setInterval的嵌套实例
6 1