Vue+Element實(shí)現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法
之前已經(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" style="width: 100%" >
<el-table-column prop="fildna" label="名稱" style="width:6vw;" >
<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 效果圖展示

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 綁定對象,數(shù)組之?dāng)?shù)據(jù)無法動態(tài)渲染案例詳解
這篇文章主要介紹了vue 綁定對象,數(shù)組之?dāng)?shù)據(jù)無法動態(tài)渲染案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
vuex刷新頁面后如何解決丟失store的數(shù)據(jù)問題
這篇文章主要介紹了vuex刷新頁面后如何解決丟失store的數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue用ant design中table表格,點(diǎn)擊某行時(shí)觸發(fā)的事件操作
這篇文章主要介紹了vue用ant design中table表格,點(diǎn)擊某行時(shí)觸發(fā)的事件操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue?keep-alive的實(shí)現(xiàn)原理分析
這篇文章主要介紹了Vue?keep-alive的實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能
這篇文章主要介紹了Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

