溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue+Element實(shí)現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法

發(fā)布時(shí)間:2020-10-16 20:45:46 來源:腳本之家 閱讀:483 作者:face light~ 欄目:web開發(fā)

之前已經(jīng)實(shí)現(xiàn)了表格的新增、編輯和刪除,在我的上篇文章中寫的也比較詳細(xì)。但是總感覺有點(diǎn)不完美,首先新增了一行以后,必須要雙擊某一個(gè)單元格參能進(jìn)行內(nèi)容的輸入。從代碼上來說,代碼量也較大;而且使用的是原生的html標(biāo)簽,有點(diǎn)尷尬。

于是,進(jìn)一步研以后,進(jìn)行了一定的優(yōu)化,直接使用vue的代碼實(shí)現(xiàn),不僅大大減少了代碼量,還實(shí)現(xiàn)了操作的友好性。下面直接上代碼:

1 html部分

這次的優(yōu)化其實(shí)主要在于html部分,直接將vue的el-input標(biāo)簽或者el-select標(biāo)簽放入表格的每個(gè)單元格中。這樣就不用去考慮表格內(nèi)容的編輯問題了。

<el-form :model="inServForm" ref="inServForm" label-width="130px" size="small">
    <el-form-item label="輸入?yún)?shù)列表" prop="servin" >
    <el-button type="primary" @click="addRow(infiledList)">新增</el-button>
    <template>
     <el-table border :data="infiledList"  >
      <el-table-column prop="fildna" label="名稱"  >
      <template scope="scope">
       <el-input size="mini" v-model="scope.row.fildna" ></el-input>
      </template>
      </el-table-column>
      <el-table-column prop="fildtp" label="類型">
      <template scope="scope">
       <el-select v-model="scope.row.fildtp" clearable >
        <el-option
        v-for="item in fildtps"
        :key="item.value"
        :label="item.text"
        :value="item.value">
        </el-option>
       </el-select>
      </template>
      </el-table-column>
      <el-table-column prop="remark" label="備注">
      <template scope="scope">
         <el-input size="mini" v-model="scope.row.remark" ></el-input>
      </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
    <template slot-scope="scope">
    <el-button @click.native.prevent="deleteRow(scope.$index, infiledList)" size="small"> 移除 </el-button>
    </template>
    </el-table-column>
   </el-table>
   </template>
  </el-form-item>
</el-form>

2 數(shù)據(jù)定義部分

data () {
 return {
infiledList:[],
 fildtps:[{text:'字符',value:'1'},{text:'數(shù)字',value:'2'}],

}

3 方法部分

methods: {
 deleteRow(index, rows) {//刪除改行
    rows.splice(index, 1);
    },
  addRow(tableData,event){
  tableData.push({ fildna: '',fildtp:'',remark:''
  })
  },
}

4 效果圖展示

Vue+Element實(shí)現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI