zoukankan      html  css  js  c++  java
  • element-ui的el-table和el-form表单校验嵌套使用

    在项目有遇到table中嵌套form,并且带有表单验证的需求,效果图如下:

    刚开始el-form-item定义了静态prop(prop=“sn”),结果input填入值后错误提示一直存在,自定义校验的value也为undefined,然后经百度大神提点,得知出错在prop

    • ⚠️应该写动态prop保证唯一性。
    :prop="'monitorData.' + scope.$index + '.sn'" 
    

     

    • ⚠️不只是el-form才配置,每一个el-form-item都要配置rules属性
    • <el-form :model="monitorForm" :rules="monitorForm.monitorRules" ref="monitorForm">
        <el-table
            class="blue-theme add-virtual-table"
            empty-text="请 新增填写行 进行填写"
            :data="monitorForm.monitorData"
            style=" 100%;">
            <el-table-column
                type="index"
                align="center"
                width="50"/>
            <el-table-column
                prop="sn"
                label="SN">
                <template slot-scope="scope">
                  <el-form-item 
                      :prop="'monitorData.' + scope.$index + '.sn'" 
                      :rules="monitorForm.monitorRules.sn">
                      <el-input
                          class="light-blue-theme"
                          clearable v-model="scope.row.sn"
                          placeholder="请输入sn"/>
                  </el-form-item>
                </template>
            </el-table-column>
            <el-table-column
                label="品牌">
                <template slot-scope="scope">
                    <el-form-item
                        :prop="'monitorData.' + scope.$index + '.type'"
                        :rules="monitorForm.monitorRules.type">
                        <el-input 
                            class="light-blue-theme" 
                            v-model="scope.row.type" 
                            placeholder="请输入品牌名称"/>
                    </el-form-item>
                </template>
            </el-table-column>
            <el-table-column
                label="高度"
                min-width="160">
                <template slot-scope="scope">
                    <el-form-item 
                        :prop="'monitorData.' + scope.$index + '.height'" 
                        :rules="monitorForm.monitorRules.height">
                        <el-input 
                            class="light-blue-theme"
                            v-model="scope.row.height"
                            placeholder="请输入高度"/>
                    </el-form-item>
                </template>
            </el-table-column>
            <el-table-column
                prop="date"
                label="操作"
                width="100">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete" 
                        circle
                        @click="handleDeleteRow(scope.$index, scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
      </el-form>
      

       data

    • monitorForm: {
          monitorData: [],
          monitorRules: {
             sn: [
                 { required: true, message: 'SN不能为空', trigger: 'blur' }
             ],
             type: [
                 { required: true, message: '品牌名称不能为空', trigger: 'blur' }
             ],
             height: [
                 { required: true, message: '高度不能为空', trigger: 'blur' }
             ]
          }
      }, // 存储表格table信息
      

       



    时而疯狂女汉子,时而温柔软妹子
  • 相关阅读:
    hdu2112 HDU Today 基础最短路
    HDU 4597 Play Game(记忆化搜索,深搜)
    HDU 4496 D-City(并查集,逆思维)
    集训心情记录,,,,(2014.6.20-6.29)
    ZOJ 1115 Digital Roots(简单,字符串与数)
    ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
    HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)
    poj 3903 Stock Exchange(最长上升子序列,模版题)
    POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
    hdu 2571 命运(递推,请小心)
  • 原文地址:https://www.cnblogs.com/csji/p/14035663.html
Copyright © 2011-2022 走看看