Vue+Element實現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法
之前已經(jīng)實現(xiàn)了表格的新增、編輯和刪除,在我的上篇文章中寫的也比較詳細。但是總感覺有點不完美,首先新增了一行以后,必須要雙擊某一個單元格參能進行內(nèi)容的輸入。從代碼上來說,代碼量也較大;而且使用的是原生的html標簽,有點尷尬。
于是,進一步研以后,進行了一定的優(yōu)化,直接使用vue的代碼實現(xiàn),不僅大大減少了代碼量,還實現(xiàn)了操作的友好性。下面直接上代碼:
1 html部分
這次的優(yōu)化其實主要在于html部分,直接將vue的el-input標簽或者el-select標簽放入表格的每個單元格中。這樣就不用去考慮表格內(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nuxt3嵌套路由,報錯Failed?to?resolve?component:?NuxtChild的解決
這篇文章主要介紹了Nuxt3嵌套路由,報錯Failed?to?resolve?component:?NuxtChild的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue.js與 ASP.NET Core 服務(wù)端渲染功能整合
本文通過案例給大家詳細分析了ASP.NET Core 與 Vue.js 服務(wù)端渲染,需要的朋友可以參考下2017-11-11
elementui之el-table-column日期格式顯示方式
文章介紹了如何使用formatter屬性對表格某一列的內(nèi)容進行日期格式化,通過綁定日期格式化的方法實現(xiàn),展示了前端代碼的模板和方法,并給出了前端效果的展示2024-12-12
echarts 使用formatter 修改鼠標懸浮事件信息操作
這篇文章主要介紹了echarts 使用formatter 修改鼠標懸浮事件信息操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
關(guān)于vue項目部署后刷新網(wǎng)頁報404錯誤解決
這篇文章主要介紹了關(guān)于vue項目部署后刷新網(wǎng)頁報404錯誤解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
vite.config配置alias Error: ENOTEMPTY: director
這篇文章主要為大家介紹了vite.config配置alias時報錯:Error: ENOTEMPTY: directory not empty, rmdir解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

