vue+element實(shí)現(xiàn)表格新增、編輯、刪除功能
需要做一個(gè)需求:新增一個(gè)xml文件時(shí),添加數(shù)量不確定、屬性相同的xml標(biāo)簽,想了想可以用表格做啊,屬性相同,使用統(tǒng)一的表頭,下面的屬性值只是進(jìn)行增刪改不就行了,就類(lèi)似于mysql給表里填數(shù)據(jù)一樣。
可是目前似乎還沒(méi)有表格的直接增刪改一行的操作,那要怎么實(shí)現(xiàn)呢?于是,通過(guò)上網(wǎng)以及自己的思考,終于實(shí)現(xiàn)了,代碼、思路以及效果圖如下:
1 html部分:
<el-button type="success" @click="addRow(tableData)">新增</el-button> <template> <el-table :data="tableData" style="width: 100%" max-height="250" @cell-dblclick="tableDbEdit"> <el-table-column prop="name" label="name" width="150"> </el-table-column> <el-table-column prop="xpath" label="xpath" width="120"> </el-table-column> <el-table-column prop="desc" label="desc" width="120"> </el-table-column> <el-table-column prop="value" label="value" width="120"> </el-table-column> <el-table-column prop="defVal" label="defVal" width="300"> </el-table-column> <el-table-column fixed="right" label="操作" width="120"> <template slot-scope="scope"> <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small"> 移除 </el-button> </template> </el-table-column> </el-table> </template>
2 樣式部分
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
.cell-edit-color{
color:#2db7f5;
font-weight: bold;
}
.cell-edit-input .el-input, .el-input__inner {
width:100px;
}
.cell-icon{
cursor:pointer;
color:#fff;
}
</style>
3.data定義:
rules: {
fileName: [
{ required: true, message: '請(qǐng)輸入文件路徑', trigger: 'blur' }
],
policyfileName: [
{ required: true, message: '請(qǐng)輸入文件路徑', trigger: 'blur' }
],
parmna: [
{ required: true, message: '請(qǐng)輸入數(shù)據(jù)字段名稱(chēng)', trigger: 'blur' }
],
remark: [
{ required: true, message: '請(qǐng)輸入分組類(lèi)型名稱(chēng)', trigger: 'blur' }
]
},
activeName: 'include',
tabPosition: 'left',
dialogFormVisible: false,
formQuery:[],
serverForm: {
fileName: '',
policyfileName: '',//policy下的include
scmType: '',
version: '',
address: ''
},
tableData: [{
name: 'aa',
xpath: 'bb',
desc: 'cc',
value: 'dd',
defVal: 'ee'
}, {
name: 'aa1',
xpath: 'bb1',
desc: 'cc1',
value: 'dd1',
defVal: 'ee1'
}]
4 具體方法:
deleteRow(index, rows) {//移除一行
rows.splice(index, 1);//刪掉該行
},
addRow(tableData,event){//新增一行
//之前一直想不到怎么新增一行空數(shù)據(jù),最后幸虧一位朋友提示:表格新增一行,其實(shí)就是源數(shù)據(jù)的新增,從源數(shù)據(jù)入手就可以實(shí)現(xiàn)了,于是 恍然大悟啊!
tableData.push({ name: '', xpath: '',desc:'',value:'',defVal:'' })
},
tableDbEdit(row, column, cell, event) {//編輯單元格數(shù)據(jù)
//當(dāng)鼠標(biāo)雙擊單元格里面具體單元格的時(shí)候,即可對(duì)數(shù)據(jù)進(jìn)行編輯操作,其實(shí)就是添加了一個(gè)輸入框,最終將輸入框中的數(shù)據(jù)保存下來(lái)就行了。
event.target.innerHTML = "";
let cellInput = document.createElement("input");
cellInput.value = "";
cellInput.setAttribute("type", "text");
cellInput.style.width = "60%";
cell.appendChild(cellInput);
cellInput.onblur = function() {
cell.removeChild(cellInput);
event.target.innerHTML = cellInput.value;
};
}
效果如下

————————分割線(xiàn)———————–
之前實(shí)現(xiàn)的都是input框的方式,昨天又做了個(gè)優(yōu)化,增加了下拉框的方式,并且新增了提交表單時(shí),能夠?qū)?shù)據(jù)傳到后端的功能。
首先我們知道,select標(biāo)簽的格式,比較特殊,沒(méi)有input那么直接,每次只需要修改,展示它本身的value屬性的值就OK了,select的標(biāo)簽格式如下:
<select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select>
那么, 我的修改之后的編輯方法如下:
tableDbEdit(row, column, cell, event) {
event.target.innerHTML = "";
let cellInput = '';
let name = column.property.trim();//拿到當(dāng)前的屬性值
//新建一個(gè)option元素
let option = document.createElement('option')
let option2 = document.createElement('option')
if(name==="fildtp"){
cellInput = document.createElement("select");
//為option賦值和內(nèi)容
option.value="1";
option.text="字符";
option2.value="2";
option2.text="數(shù)字";
//將option元素直接添加為子元素
cellInput.appendChild(option);
cellInput.appendChild(option2);
cell.appendChild(cellInput);
cellInput.onblur = function() {
cell.removeChild(cellInput);
//將單元格的內(nèi)容填充為所選中元素的內(nèi)容,而不是值
event.target.innerHTML = cellInput.selectedOptions[0].text;
//將所選中元素的值賦給該行的值,每個(gè)屬性都執(zhí)行一次賦值,那么在最終提交表單的時(shí)候,就能保證當(dāng)前表的值傳到后端
row.fildtp=cellInput.value;
}
}else if(name==="musttg"){
cellInput = document.createElement("select");
option.value="1";
option.text="是";
option2.value="0";
option2.text="否";
cellInput.appendChild(option);
cellInput.appendChild(option2);
cell.appendChild(cellInput);
cellInput.onblur = function() {
cell.removeChild(cellInput);
event.target.innerHTML = cellInput.selectedOptions[0].text;
row.musttg=cellInput.value;
}
}else if(name==="looptg"){
cellInput = document.createElement("select");
option.value="1";
option.text="循環(huán)";
option2.value="0";
option2.text="非循環(huán)";
cellInput.appendChild(option);
cellInput.appendChild(option2);
cell.appendChild(cellInput);
cellInput.onblur = function() {
cell.removeChild(cellInput);
event.target.innerHTML = cellInput.selectedOptions[0].text;
row.looptg=cellInput.value;
};
}else{
debugger
cellInput = document.createElement("input");
cellInput.value = "";
cellInput.setAttribute("type", "text");
cellInput.style.width = "75%";
cell.appendChild(cellInput);
cellInput.onblur = function() {
cell.removeChild(cellInput);
event.target.innerHTML = cellInput.value;
if(name==="fildcd"){
row.fildcd=cellInput.value;
}else if(name==="fildna"){
row.fildna=cellInput.value;
}else if(name==="fildln"){
row.fildln=cellInput.value;
}else if(name==="fildde"){
row.fildde=cellInput.value;
}else if(name==="defult"){
row.defult=cellInput.value;
}else if(name==="format"){
row.format=cellInput.value;
}else if(name==="enumcd"){
row.enumcd=cellInput.value;
}else if(name==="loophd"){
row.loophd=cellInput.value;
}else if(name==="remark"){
row.remark=cellInput.value;
}
};
}
},
這種方式,比較詳細(xì),也比較容易理解,但是總感覺(jué)有點(diǎn)不完美,首先新增了一行以后,必須要雙擊某一個(gè)單元格參能進(jìn)行內(nèi)容的輸入,不夠明顯。從代碼上來(lái)說(shuō),代碼量也較大;而且使用的是原生的html標(biāo)簽,因此,我在之后對(duì)此進(jìn)行了一個(gè)優(yōu)化,直接使用vue的代碼實(shí)現(xiàn),不僅大大減少了代碼量,還實(shí)現(xiàn)了操作的友好性。具體實(shí)現(xiàn)可以查看的我的文章:Vue+Element實(shí)現(xiàn)表格的編輯、刪除、以及新增行的最優(yōu)方法
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+element-ui實(shí)現(xiàn)表格編輯的三種實(shí)現(xiàn)方式
- vue+Element中table表格實(shí)現(xiàn)可編輯(select下拉框)
- vue+iview 實(shí)現(xiàn)可編輯表格的示例代碼
- vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例
- Vue+Element實(shí)現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法
- 對(duì)Vue table 動(dòng)態(tài)表格td可編輯的方法詳解
- VUE+Element UI實(shí)現(xiàn)簡(jiǎn)單的表格行內(nèi)編輯效果的示例的代碼
- VUE table表格動(dòng)態(tài)添加一列數(shù)據(jù),新增的這些數(shù)據(jù)不可以編輯(v-model綁定的數(shù)據(jù)不能實(shí)時(shí)更新)
- Vue.js實(shí)現(xiàn)可編輯的表格
- Vue+Element實(shí)現(xiàn)表格單元格編輯
相關(guān)文章
在Vue3項(xiàng)目中關(guān)閉ESLint的完整步驟
實(shí)際上在學(xué)習(xí)過(guò)程中,你會(huì)發(fā)現(xiàn)eslint檢查特別討厭,這個(gè)時(shí)候我們需要關(guān)閉掉eslint檢查,下面這篇文章主要給大家介紹了關(guān)于在Vue3項(xiàng)目中關(guān)閉ESLint的完整步驟,需要的朋友可以參考下2023-11-11
Vue2實(shí)現(xiàn)圖片的拖拽,縮放和旋轉(zhuǎn)效果的示例代碼
這篇文章主要為大家介紹了如何基于vue2?實(shí)現(xiàn)圖片的拖拽、旋轉(zhuǎn)、鼠標(biāo)滾動(dòng)放大縮小等功能。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11
關(guān)于vue.js中實(shí)現(xiàn)方法內(nèi)某些代碼延時(shí)執(zhí)行
今天小編就為大家分享一篇關(guān)于vue.js中實(shí)現(xiàn)方法內(nèi)某些代碼延時(shí)執(zhí)行,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Vue打包為相對(duì)路徑的具體實(shí)現(xiàn)方法
在Vue.js項(xiàng)目中,構(gòu)建后的資源文件(如CSS、JavaScript文件、圖片等)通常會(huì)被放置在指定的目錄下,為了確保這些文件能夠被正確加載,Vue CLI 提供了配置選項(xiàng)來(lái)指定這些文件的路徑,本文給大家介紹了Vue打包為相對(duì)路徑的具體實(shí)現(xiàn)方法,需要的朋友可以參考下2024-09-09
Vue實(shí)現(xiàn)類(lèi)似Spring官網(wǎng)圖片滑動(dòng)效果方法
這篇文章主要介紹了Vue實(shí)現(xiàn)類(lèi)似Spring官網(wǎng)圖片滑動(dòng)效果方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
在vue中使用inheritAttrs實(shí)現(xiàn)組件的擴(kuò)展性介紹
這篇文章主要介紹了在vue中使用inheritAttrs實(shí)現(xiàn)組件的擴(kuò)展性介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
vue中watch的實(shí)際開(kāi)發(fā)學(xué)習(xí)筆記
watch是Vue實(shí)例的一個(gè)屬性是用來(lái)響應(yīng)數(shù)據(jù)的變化,需要在數(shù)據(jù)變化時(shí)執(zhí)行異步或開(kāi)銷(xiāo)較大的操作時(shí),這個(gè)方式是最有用的,下面這篇文章主要給大家介紹了關(guān)于vue中watch的實(shí)際開(kāi)發(fā)筆記,需要的朋友可以參考下2022-11-11
vue依賴(lài)包報(bào)錯(cuò)問(wèn)題eslint\lib\cli-engine\cli-engine.js:421
這篇文章主要介紹了vue依賴(lài)包報(bào)錯(cuò)問(wèn)題eslint\lib\cli-engine\cli-engine.js:421,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

