Vue+Element實現(xiàn)表格單元格編輯
前言
Element的表格組件并沒有給出明確的點擊單個單元格進行的編輯的方案,我仔細閱讀了官方的文檔后,發(fā)現(xiàn)這個操作還是可以實現(xiàn)的。
實現(xiàn)原理
1、利用Table組件的cell-click屬性,可以獲取當前點擊的單元格列對應的Dom元素。
2、清空所有的名為current-cell的class屬性。
3、為當前獲取的單元格Dom動態(tài)添加名為current-cell的class屬性。
4、控制單元格的input標簽的顯示隱藏就能實現(xiàn)表格的編輯功能。
代碼實現(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: '項目名稱', prop: 'itemName', width: '300', key: '100' }, ? ? { label: '項目編碼', prop: 'itemCode', width: '150', key: '200' }, ? ? { label: '單位', prop: 'compName', width: '150', key: '300', edit: true }, ? ? { ? ? ? ? label: '費用', prop: '', width: '450', align: 'center', key: '400', children: [ ? ? ? ? ? ? { label: '人工費', prop: 'staff', width: '150', key: '401', edit: true }, ? ? ? ? ? ? { label: '資料費', prop: 'material', width: '150', key: '402', edit: true }, ? ? ? ? ? ? { label: '場地費', 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 }
注意:注意相應的樣式不能少,非常重要?。?!
頁面效果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- vue+element-ui實現(xiàn)表格編輯的三種實現(xiàn)方式
- vue+Element中table表格實現(xiàn)可編輯(select下拉框)
- vue+iview 實現(xiàn)可編輯表格的示例代碼
- vuejs+element UI點擊編輯表格某一行時獲取內(nèi)容填入表單的示例
- Vue+Element實現(xiàn)表格編輯、刪除、以及新增行的最優(yōu)方法
- 對Vue table 動態(tài)表格td可編輯的方法詳解
- VUE+Element UI實現(xiàn)簡單的表格行內(nèi)編輯效果的示例的代碼
- vue+element實現(xiàn)表格新增、編輯、刪除功能
- VUE table表格動態(tài)添加一列數(shù)據(jù),新增的這些數(shù)據(jù)不可以編輯(v-model綁定的數(shù)據(jù)不能實時更新)
- Vue.js實現(xiàn)可編輯的表格
相關文章
vue中radio單選框如何實現(xiàn)取消選中狀態(tài)問題
這篇文章主要介紹了vue中radio單選框如何實現(xiàn)取消選中狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05在vue中使用防抖和節(jié)流,防止重復點擊或重復上拉加載實例
今天小編就為大家分享一篇在vue中使用防抖和節(jié)流,防止重復點擊或重復上拉加載實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue.directive 實現(xiàn)元素scroll邏輯復用
這篇文章主要介紹了Vue.directive 實現(xiàn)元素scroll邏輯復用功能,文中給大家提到元素實現(xiàn)滾動的條件有兩個,具體內(nèi)容詳情大家參考下本文2019-11-11vue-element-admin 全局loading加載等待
本文主要介紹了vue-element-admin 全局loading加載等待,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09基于vue-upload-component封裝一個圖片上傳組件的示例
這篇文章主要介紹了基于vue-upload-component封裝一個圖片上傳組件的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10