Vue+Element實(shí)現(xiàn)表格單元格編輯
前言
Element的表格組件并沒有給出明確的點(diǎn)擊單個(gè)單元格進(jìn)行的編輯的方案,我仔細(xì)閱讀了官方的文檔后,發(fā)現(xiàn)這個(gè)操作還是可以實(shí)現(xiàn)的。
實(shí)現(xiàn)原理
1、利用Table組件的cell-click屬性,可以獲取當(dāng)前點(diǎn)擊的單元格列對(duì)應(yīng)的Dom元素。
2、清空所有的名為current-cell的class屬性。
3、為當(dāng)前獲取的單元格Dom動(dòng)態(tài)添加名為current-cell的class屬性。
4、控制單元格的input標(biāo)簽的顯示隱藏就能實(shí)現(xiàn)表格的編輯功能。
代碼實(shí)現(xiàn)
<template>
? <div class="tableDiv">
? ? <el-table :data="tableData" highlight-current-row @cell-click="cellClick">
? ? ? <el-table-column
? ? ? ? v-for="(item, index) in tableColumn"
? ? ? ? :key="index"
? ? ? ? :prop="item.prop"
? ? ? ? :label="item.label"
? ? ? ? :width="item.width"
? ? ? >
? ? ? ? <template slot-scope="scope">
? ? ? ? ? <el-input
? ? ? ? ? ? v-if="item.edit"
? ? ? ? ? ? size="small"
? ? ? ? ? ? ref="tableInput"
? ? ? ? ? ? v-model="scope.row[item.prop]"
? ? ? ? ? ? @blur="removeClass"
? ? ? ? ? ? @change="handleEdit(item.prop, scope.$index, scope.row)"
? ? ? ? ? ></el-input>
? ? ? ? ? <span>{{ scope.row[item.prop] }}</span>
? ? ? ? </template>
? ? ? ? <el-table-column
? ? ? ? ? v-for="(itemchild, indexchild) in item.children"
? ? ? ? ? :key="indexchild"
? ? ? ? ? :prop="itemchild.prop"
? ? ? ? ? :label="itemchild.label"
? ? ? ? ? :width="itemchild.width"
? ? ? ? >
? ? ? ? ? <template slot-scope="scope">
? ? ? ? ? ? <el-input
? ? ? ? ? ? ? v-if="itemchild.edit"
? ? ? ? ? ? ? size="small"
? ? ? ? ? ? ? ref="tableInput"
? ? ? ? ? ? ? v-model="scope.row[itemchild.prop]"
? ? ? ? ? ? ? @blur="removeClass"
? ? ? ? ? ? ? @change="handleEdit(itemchild.prop, scope.$index, scope.row)"
? ? ? ? ? ? ></el-input>
? ? ? ? ? ? <span>{{ scope.row[itemchild.prop] }}</span>
? ? ? ? ? </template>
? ? ? ? </el-table-column>
? ? ? </el-table-column>
? ? </el-table>
? </div>
</template>
<script>
import { Column, tableData } from "./tableColumn";
export default {
? data() {
? ? return {
? ? ? tableData: tableData,
? ? ? tableColumn: Column
? ? };
? },
? methods: {
? ? handleEdit() {},
? ? cellClick(row, column, cell, event) {
? ? ? for(let i=0;i<document.getElementsByClassName('current-cell').length;i++){
? ? ? ? document.getElementsByClassName('current-cell')[i].classList.remove('current-cell');
? ? ? }
? ? ? cell.classList.add("current-cell");
? ? },
? ? removeClass(){
? ? ? document.getElementsByClassName('current-cell')[0].classList.remove('current-cell');
? ? }
? }
};
</script>
<style scoped>
.tableDiv .el-input {
? display: none;
}
.current-cell .el-input {
? display: block;
}
.current-cell .el-input + span {
? display: none;
}
</style>tableColumn.js文件
const Column = [
? ? { label: '項(xiàng)目名稱', prop: 'itemName', width: '300', key: '100' },
? ? { label: '項(xiàng)目編碼', prop: 'itemCode', width: '150', key: '200' },
? ? { label: '單位', prop: 'compName', width: '150', key: '300', edit: true },
? ? {
? ? ? ? label: '費(fèi)用', prop: '', width: '450', align: 'center', key: '400', children: [
? ? ? ? ? ? { label: '人工費(fèi)', prop: 'staff', width: '150', key: '401', edit: true },
? ? ? ? ? ? { label: '資料費(fèi)', prop: 'material', width: '150', key: '402', edit: true },
? ? ? ? ? ? { label: '場(chǎng)地費(fèi)', prop: 'site', width: '150', key: '403' }
? ? ? ? ]
? ? }
];
const tableData = [
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 1 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 2 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 3 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 4 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 5 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 6 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 7 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 8 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 9 },
? ? { itemName: "北京水立方", itemCode: "1351157698", compName: "北京", staff: "100", material: "84547", site: "6544", sort: 10 }
]
export {
? ? Column, tableData
}注意:注意相應(yīng)的樣式不能少,非常重要?。。?/p>
頁(yè)面效果:

以上就是本文的全部?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+element實(shí)現(xiàn)表格新增、編輯、刪除功能
- VUE table表格動(dòng)態(tài)添加一列數(shù)據(jù),新增的這些數(shù)據(jù)不可以編輯(v-model綁定的數(shù)據(jù)不能實(shí)時(shí)更新)
- Vue.js實(shí)現(xiàn)可編輯的表格
相關(guān)文章
關(guān)于element同時(shí)使用Drawer和Dialog出現(xiàn)多個(gè)遮罩問(wèn)題
這篇文章主要介紹了關(guān)于element同時(shí)使用Drawer和Dialog出現(xiàn)多個(gè)遮罩問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue項(xiàng)目實(shí)現(xiàn)局部全屏完整代碼
最近需要做一個(gè)全屏功能,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目實(shí)現(xiàn)局部全屏的相關(guān)資料,需要的朋友可以參考下2023-09-09
Ant Design Upload 文件上傳功能的實(shí)現(xiàn)
這篇文章主要介紹了Ant Design Upload 文件上傳功能的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題
今天小編就為大家分享一篇解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue中集成省市區(qū)街四級(jí)地址組件的實(shí)現(xiàn)過(guò)程
我們?cè)陂_發(fā)中常會(huì)遇到選擇地址的需求,有時(shí)候只需要選擇省就可以,有時(shí)候則需要選擇到市、縣,以至于鄉(xiāng)鎮(zhèn),甚至哪個(gè)村都有可能,下面這篇文章主要給大家介紹了關(guān)于vue中集成省市區(qū)街四級(jí)地址組件的相關(guān)資料,需要的朋友可以參考下2022-12-12

