欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

element中el-table與el-form同用并校驗

 更新時間:2022年08月12日 09:53:48   作者:瀟藍(lán)諾依  
本文主要介紹了element中el-table與el-form同用并校驗,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

基本結(jié)構(gòu)

<template>
  <div>
    <el-form ref="form" :model="form">
      <el-table  ref="table" :data="form.tableData" empty-text='暫無數(shù)據(jù)'>
        <el-table-column label="姓名">
          <template slot-scope="scope">
            <el-form-item >
              <el-input v-model="scope.row.name"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="年齡">
          <template slot-scope="scope">
            <el-form-item >
              <el-input v-model.number="scope.row.age" type="number"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column  label="操作">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="del(scope.$index)" icon='el-icon-delete'></el-button>
            <el-button type="primary" size="mini" @click="add" icon='el-icon-plus'></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        tableData: [
          {
            name: "aaa",
            age: 11
          },
          {
            name: "",
            age: ''
          }
        ]
      }
    };
  },
  methods:{
      add(){
          this.form.tableData.push({
              name: "",
              age: ''
          })
      },
      del(index){
          this.form.tableData.splice(index,1);
      }
  }
};
</script>

el-table 數(shù)據(jù)是數(shù)組 , el-form 數(shù)據(jù)是 對象

添加校驗

Form 組件提供了表單驗證的功能,只需要通過 rules 屬性傳入約定的驗證規(guī)則,并將 Form-Item 的 prop 屬性設(shè)置為需校驗的字段名即可。

方法1:

<el-form :rules='rules'  ...>

<el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules='rules.name'>
data(){
	return {
    	rules: {
	        name: [
	          { required: true, message: '名字不能為空', trigger: 'blur' }
	        ]
	      }
    }
}

方法2:

<el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules="{ required: true, message: '名字不能為空', trigger: 'blur' }">
 <el-table-column label="姓名">
   <template slot-scope="scope">
     <el-form-item :prop=" 'tableData.' + scope.$index + '.name' " 
        :rules="{ required: true, message: '名字不能為空', trigger: 'blur' }">
       <el-input v-model="scope.row.name"></el-input>
     </el-form-item>
   </template>
 </el-table-column>

自己寫校驗方法

方法1 : 對應(yīng)上面的方法1,校驗犯法寫在data中.

data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年齡不能為空'));
        }else if(!Number.isInteger(value) ){
            callback(new Error('請輸入數(shù)字值'));
        }else if(value < 18){
            callback(new Error('必須年滿18歲'));
        }else if(value >50){
            callback(new Error('小于50歲'));
        }else{
            callback();
        }
      };
      var checkName = (rule, value, callback) => {
          let regName =/^[\u4e00-\u9fa5]{2,4}$/;
        if (!value) {
          return callback(new Error('姓名不能為空'));
        }else if(!regName.test(value)){
            callback(new Error('請輸入正確的姓名'));
        }else{
            callback();
        }
      };
    return {
      form: {
        tableData: [
          {
            name: "",
            age: ''
          }
        ]
      },
      rules: {
        name: [
          { required: true, trigger: 'blur' ,validator: checkName}
        ],
        age: [
          { required: true, trigger: 'blur', validator: checkAge}
        ],
      }
    };
  },

方法2: 對應(yīng)上面的方法2,校驗犯法寫在methods中.

<el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules=" { required: true, trigger: 'blur' ,validator: checkName}">

<el-form-item :prop=" 'tableData.' + scope.$index + '.age' " :rules=" { required: true, trigger: 'blur' ,validator: checkAge}">
methods:{
      checkAge  (rule, value, callback)  {
        if (!value) {
          return callback(new Error('年齡不能為空'));
        }else if(!Number.isInteger(value) ){
            callback(new Error('請輸入數(shù)字值'));
        }else if(value < 18){
            callback(new Error('必須年滿18歲'));
        }else if(value >50){
            callback(new Error('小于50歲'));
        }else{
            callback();
        }
      },
      checkName (rule, value, callback) {
        let regName =/^[\u4e00-\u9fa5]{2,4}$/;
        if (!value) {
          return callback(new Error('姓名不能為空'));
        }else if(!regName.test(value)){
            callback(new Error('請輸入正確的姓名'));
        }else{
            callback();
        }
      }
  }

同table中,某字段不能重復(fù)

8.27 更新

之前寫了一個同table中,不能寫相同的電話號碼的校驗,但是并不好。修改后如下

<el-table-column label="電話號碼">
   <template slot-scope="scope">
     <el-form-item
       :prop=" 'tableData.' + scope.$index + '.tel' "
       :rules=" { required: true, trigger: 'blur' ,validator: checkTel}"
     >
       <el-input
         v-model.number="scope.row.tel"
         type="number"
         @blur="telequalCheckblur(scope.$index,scope.row.tel)"
       ></el-input>
     </el-form-item>
   </template>
 </el-table-column>
methods:{
	checkTel(rule, value, callback) {
      let regTel = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/;
      let index = rule.field.split(".")[1];
      if (!value) {
        return callback(new Error("電話號碼不能為空"));
      } else if (!regTel.test(value)) {
        callback(new Error("請輸入正確的電話號碼"));
      } else if (this.telequalCheck(index, value)) {
        callback(new Error("電話號碼重復(fù),請重新填寫"));
      } else {
        callback();
      }
    },
    //相等判斷
    telequalCheck(index, value) {
      return this.form.tableData.some(({ tel }, i) => {
        console.log(i, index, i == index);
        return i == index ? false : tel === value;
      });
    },
}

9.14更新

必填*顯示

1. 輸入框前加*

正常來說,在el-form中,使用rules的required: true,就能顯示*了.
因為是在表格中,所以表單就不使用label了,所以這時必填選項的*沒有了.

注意:這里面label=' '必須有空格,否則還是沒有*

9.17更新

表頭加*

1.el-table>添加 header-cell-class-name='required'

參數(shù)說明類型可選值默認(rèn)值
header-cell-class-name表頭單元格的 className 的回調(diào)方法,也可以使用字符串為所有表頭單元格設(shè)置一個固定的 className。Function({row, column, rowIndex, columnIndex})/String

2. elment 有默認(rèn)的樣式,如果不想要,就換個class名字
header-cell-class-name='requiredclass'

<style>
.requiredclass>div.cell::before{
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>

3. 只加某幾個
:header-cell-class-name='addHeaderCellClassName'

addHeaderCellClassName({row, column, rowIndex, columnIndex}){
      if(columnIndex===0 || columnIndex===1){
        return 'requiredclass'
      }
    }

自定義表頭

在需要添加*的位置添加自定義表頭

.requiredclass::before{
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}

刪除某行,同時刪除這行的校驗結(jié)果

問題: 刪除某行時,這行的校驗結(jié)果不隨著刪除
觀察圖片,刪除第三行,第三行的校驗結(jié)果,還在.

解決方法

刪除時,對表單進行校驗

del(index) {
  this.form.tableData.splice(index, 1);
  this.$refs.form.validate()
},

row-key

綁定row-key,綁定的值要保證存在且唯一

 <el-form ref="form" :model="form" label-width="20px">
      <el-table
        ref="table"
        :data="form.tableData"
        empty-text="暫無數(shù)據(jù)"
        header-cell-class-name="requiredclass"
        row-key='id'
      >
      ...


 form: {
    tableData: [
      {
        name: "張一",
        age: 19,
        id:'1',
        tel: 13333333332
      },
    ]
  }

代碼及展示地址

到此這篇關(guān)于element中el-table與el-form同用并校驗的文章就介紹到這了,更多相關(guān)element el-table與el-form內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue使用xlsx模塊實現(xiàn)讀寫Excel文檔內(nèi)容

    Vue使用xlsx模塊實現(xiàn)讀寫Excel文檔內(nèi)容

    在前端項目開發(fā)中,讀寫Excel文件的需求變得越來越常見,本文將詳細(xì)介紹如何在Vue項目中利用FileReader對象和xlsx模塊來讀取Excel文件的內(nèi)容,需要的可以參考下
    2024-03-03
  • 詳解Unity webgl 嵌入Vue實現(xiàn)過程

    詳解Unity webgl 嵌入Vue實現(xiàn)過程

    Unity webgl嵌入到前端網(wǎng)頁中,前端通過調(diào)用Unity webgl內(nèi)方法實現(xiàn)需要展示的功能,前端點擊Unity webgl內(nèi)的交互點,Unity webgl返回給前端一些需要的數(shù)據(jù),這篇文章主要介紹了Unity webgl 嵌入Vue實現(xiàn)過程,需要的朋友可以參考下
    2024-01-01
  • vue項目實現(xiàn)添加圖片裁剪組件

    vue項目實現(xiàn)添加圖片裁剪組件

    這篇文章主要為大家詳細(xì)介紹了vue項目實現(xiàn)添加圖片裁剪組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 基于vue+canvas的excel-like組件實例詳解

    基于vue+canvas的excel-like組件實例詳解

    a vue component,基于vue的表格組件,主要解決大數(shù)據(jù)量的表格渲染性能問題,使用canvas繪制表格,同時支持類似excel的批量選中,復(fù)制黏貼刪除,實時編輯等功能.這篇文章主要介紹了基于vue+canvas的excel-like組件,需要的朋友可以參考下
    2017-11-11
  • vue中利用three.js實現(xiàn)全景圖的完整示例

    vue中利用three.js實現(xiàn)全景圖的完整示例

    這篇文章主要給大家介紹了關(guān)于vue中利用three.js實現(xiàn)全景圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式

    vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式

    最近的項目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點擊功能,本文主要介紹了vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • vue實現(xiàn)購物車的小練習(xí)

    vue實現(xiàn)購物車的小練習(xí)

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)購物車的小練習(xí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Vue中$set()的使用方法場景分析

    Vue中$set()的使用方法場景分析

    由于 Vue 會在初始化實例時進行雙向數(shù)據(jù)綁定,使用Object.defineProperty()對屬性遍歷添加 getter/setter 方法,所以屬性必須在 data 對象上存在時才能進行上述過程 ,這樣才能讓它是響應(yīng)的,這篇文章主要介紹了Vue中$set()的使用方法場景分析,需要的朋友可以參考下
    2023-02-02
  • vue2.x與vue3.x中自定義指令詳解(最新推薦)

    vue2.x與vue3.x中自定義指令詳解(最新推薦)

    vue自定義指令(2.x丨3.x)可以幫助我們實現(xiàn)需要操作,比如防抖、節(jié)流、懶加載、輸入框自動聚焦等等,使用起來非常方便,比如vue自帶的v-text、v-html、v-show、v-if等等,這篇文章主要介紹了vue2.x與vue3.x中自定義指令詳解,需要的朋友可以參考下
    2022-12-12
  • 在Uni中使用Vue的EventBus總線機制操作

    在Uni中使用Vue的EventBus總線機制操作

    這篇文章主要介紹了在Uni中使用Vue的EventBus總線機制操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07

最新評論