VUE 表格中添加表單驗證


            表格數據有多行,針對每一行的input輸入值加入表單驗證,關鍵代碼是::prop="'data1.'+scope.$index+'.newBudget'"
 
<div>
        <el-form ref="form1" :model="readyUpdateNodes" label-width="150px">
          <el-table
            :data="readyUpdateNodes.data1"
            :span-method="changeUpdateType"
            align="center"
            border
            stripe
            class="table1"
          >
            <el-table-column prop="campaignName" align="center" label="廣告組名稱">
              <template slot-scope="{row}">
                {{ row.campaignName }}
              </template>
            </el-table-column>
            <el-table-column prop="budget" align="center" label="當前預算">
              <template slot-scope="{row}">
                {{ (row.budgetModeDesc ? row.budgetModeDesc : '') + '\t' + (row.budget ? row.budget : '--') }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="修改預算">
              <template slot-scope="scope">

                <el-form-item label="預算" :prop="'data1.'+scope.$index+'.newBudget'" :rules="readyUpdateNodes.fromaDataRules.newBudget">
                  <el-input v-model.number="scope.row.newBudget" @input="updateBudget($event)" />
                  <span v-if="scope.row.newBudget==='0'||scope.row.newBudget===0" class="messageTip">0為不限預算</span>
                </el-form-item>

              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
data() {
var validateBuget = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('請輸入預算'))
      }
      //   if (value === 0 || value === '0') {
      //     callback(new Error('0為預算不限'))
      //   }
      if ((value > 0 && value < 300) || value > 20000) {
        callback(new Error('預算金額范圍為[300, 20000],請修改'))
      }
      callback()
    }
return {
readyUpdateNodes: {
        'data1': [],
        'fromaDataRules': {
          newBudget: [
            { required: true, message: '請輸入預算', trigger: 'blur' },
            { validator: validateBuget, trigger: 'blur' }
          ]
        }
      }
}
}

        

            效果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM