iview-form內(nèi)table修改字段效驗(yàn)

場(chǎng)景:使用iview-ui,form表單內(nèi)嵌table支持編輯,對(duì)某個(gè)字段進(jìn)行單獨(dú)效驗(yàn)

3.gif

頁(yè)面中form表單中包含的table,table的columns使用render函數(shù)進(jìn)行渲染,本身form表單有多條數(shù)據(jù)進(jìn)行效驗(yàn),編輯表格內(nèi)容時(shí)單獨(dú)對(duì)某個(gè)input框進(jìn)行效驗(yàn)。
將表格內(nèi)效驗(yàn)的條件也寫在form的rules 內(nèi),表格render函數(shù)渲染input時(shí),使用form-item包裹,并填寫prop,作為表單中的一項(xiàng)可調(diào)用效驗(yàn),需在data中提前聲明。(注意:同時(shí)效驗(yàn)多行同一個(gè)字段可能會(huì)出錯(cuò))

<template>
  <i-form :rules="ruleValidate" :model="formValidate">
    <div>
      <form-item prop="applyMatters">
        <i-input v-model="formValidate.inputValue" type="textarea" :rows="4" style="width: 100%" />
      </form-item>
    </div>
    <div>
      <form-item>
        <i-table :columns="tableColumns" :data="tableData" ></i-table>
      </form-item>
    </div>
  </i-form>
</template>

data(){
  const validateMobile = (rule, value, callback) => {
     if(!this.isMobile(value)) {
      callback(new Error('手機(jī)格式錯(cuò)誤'))
     } else {
      callback()
     }
   }
  return {
    ruleValidate: {
      applyMatters:[{required: true, message: '請(qǐng)?zhí)顚憘渥?, trigger: 'change'}],
      deviceTel: [{required: true, validator: validateMobile, trigger: 'blur'}]
    }
  },
  formValidate: {
    inputValue: '',
    deviceTel: ''
  },
  tableColumns: [
    {
      title: '手機(jī)號(hào)',
      key: 'deviceTel',
      minWidth: 140,
      align: 'center',
      render(h, {row, index}){
        let edit
        if(this.editIndex === index) { // 判斷table 指定編輯行
          this.department = row.deviceTel 
          edit = [
            h('FormItem', { // 將table 中修改的input, 作為form 表單中的formItem
              props: {
                prop: 'deviceTel'
              }
            }, [
              h('Input', {
                props: {
                  value: row.deviceTel
                },
                on: {
                  'on-blur': (e) => {
                    this.formValidate.deviceTel = e.target.value
                    this.department = e.target.value
                  }
                }
              })
            ])
          ]
        } else {
          edit = row.deviceTel
        }
        return h('div', [edit])
      }
    }
  ]
},
methods: {
  isMobile(s){
    return /^1[0-9]{10}$/.test(s)
  }
}

在table新增時(shí)先對(duì)form 內(nèi)容進(jìn)行部分效驗(yàn)
this.$refs['formValidate'].validate((valid) => {})
table 編輯 input 失去焦點(diǎn)時(shí)再次觸發(fā)form表單效驗(yàn)

日常記錄,有錯(cuò)誤or優(yōu)化,請(qǐng)指出不勝感謝!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容