之前已經(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 效果圖展示
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com