Vue封裝的可編輯表格插件方法
可任意合并表頭,實(shí)現(xiàn)單元格編輯。
頁(yè)面效果如圖:

頁(yè)面使用如下:
<template>
<div>
<v-table
:datat = "tableData"
:thlabel="thlabel"
:isEdit="true">
</v-table>
</div>
</template>
<script>
export default{
data(){
return{
tableData:[{'a':'1','b':'2','c':'3','d':'8'},{'a':'4','b':'5',c:'6',d:'9'}],
thlabel:[[{label:'測(cè)試1',prop:'a',rowspan:'2'},{label:'測(cè)試2'},{label:'測(cè)試3',colspan:'2'}],
[{prop:'c',label:'表頭2'},{prop:'b',label:'表頭3'},{prop:'d',label:'表頭4'}]]
}
}
}
</script>
目錄結(jié)構(gòu)如下:

vtable.vue代碼如下:
<template id="vtable">
<table>
<thead>
<tr v-for="(i,index) in rownum">
<th v-for="label in thlabel[index]">{{label.label}}</th>
</tr>
</thead>
<tbody>
<tr v-for="data in datat">
<td v-for="key in labelprop" @click="tdEdit($event)"><input type="text" v-model="data[key]"/></td>
</tr>
</tbody>
</table>
</template>
<script>
export default{
props:{
datat:{
type:Array,
required:true
},
thlabel:{//表頭數(shù)組
type:Array,
required:true
},
isEdit:{
type:Boolean,
required:true
}
},
data(){
return{
datata:''
}
},
computed:{
rownum:function(){//表頭行數(shù)
return this.thlabel.length;
},
labelprop:function(){//取出表頭數(shù)據(jù)依次對(duì)應(yīng)的字段key
let thlab = this.thlabel;
var ar = [];
for(let i=0;i<thlab.length;i++)
for(let j=0;j<thlab[i].length;j++){
for(var key in thlab[i][j]){
if(key == 'prop'){
ar.push(thlab[i][j][key])
}
}
}
return ar;
},
},
mounted:function(){
this.$nextTick(function(){
$('td').attr('isEdit',this.isEdit);
var a = this.thlabel;
for(let i=0;i<a.length;i++)
for(let j=0;j<a[i].length;j++){
for(var key in a[i][j]){
if(key == 'rowspan'){
$($('tr').eq(i).find('th').eq(j)).attr('rowspan',a[i][j][key]);
}else if(key == 'colspan'){
$($('tr').eq(i).find('th').eq(j)).attr('colspan',a[i][j][key]);
}
}
}
}
)
},
methods:{
tdEdit:function(event){
var h = event.currentTarget;
if($(h).attr('isEdit')){
$(h).find('input').attr("readOnly",false);
$(h).addClass('tdclick').siblings().removeClass('tdclick');
$(h).addClass('tdclick').parent('tr').siblings().find('td').removeClass('tdclick');
}else{
$(h).find('input').attr("readOnly",true);
}
}
}
}
</script>
<style>
@import './scss/vtable.css';
</style>
index.js代碼如下:
import Vue from 'vue'
import vtable from './vtable/vtable.vue'
import vpagination from './vpagination/vpagination.vue'
const common = {//全局安裝
install:function(Vue){
Vue.component('vTable',vtable);
Vue.component('vPag',vpagination);
}
}
export default common
main.js中引入
import common from './components/common/index.js' Vue.use(common)
css樣式代碼:
table {
border: 1px solid #EBEEF5;
height: 200px;
width: 300px;
text-align: center;
margin-left: 40px; }
table td {
border: 1px solid #EBEEF5;
position: relative;
color: #606266; }
table th {
text-align: center;
border: 1px solid #EBEEF5;
background-color: #F5F7FA;
color: #909D8F;
line-height: 60px; }
tr:hover {
background-color: #F6F8FB; }
.tdclick:after{
content: ' ';
position: absolute;
display: block;
width: 100%;
height: 100%;
top: 0;
left: 0;
border: 1px solid blue;
pointer-events: none;
}
input{
display: block;
width: 100%;
height: 100%;
text-align: center;
border: none;
outline: none;
}
/*# sourceMappingURL=vtable.css.map */
如有錯(cuò)誤或可改進(jìn)的地方,請(qǐng)指正!
以上這篇Vue封裝的可編輯表格插件方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目本地開(kāi)發(fā)完成后部署到服務(wù)器后報(bào)404錯(cuò)誤解決方案
很多時(shí)候我們發(fā)現(xiàn)辛辛苦苦寫(xiě)的VueJs應(yīng)用經(jīng)過(guò)打包后在自己本地搭建的服務(wù)器上測(cè)試沒(méi)有什么問(wèn)題,但真正放在服務(wù)器上后會(huì)發(fā)現(xiàn)或多或少的問(wèn)題,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目本地開(kāi)發(fā)完成后部署到服務(wù)器后報(bào)404錯(cuò)誤的解決方案,需要的朋友可以參考下2024-01-01
在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法
今天小編就為大家分享一篇在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列
這篇文章主要為大家詳細(xì)介紹了vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue el-checkbox實(shí)現(xiàn)全選單選方式
這篇文章主要介紹了vue el-checkbox實(shí)現(xiàn)全選單選方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
本篇文章主要介紹了Vue2 利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
vscode中vue-cli項(xiàng)目es-lint的配置方法
本文主要介紹vscode中 vue項(xiàng)目es-lint的配置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的的朋友參考下吧2018-07-07

