自己嘗試著封裝一個(gè)通用的table組件,里面有一個(gè)顯示、隱藏列的功能,如下圖:
可以通過點(diǎn)擊“顯示/隱藏列”下面的列名稱來顯示隱藏表格對(duì)應(yīng)的列,但是當(dāng)我多次(反復(fù))點(diǎn)擊時(shí),發(fā)現(xiàn)table里面的列顯示、隱藏的動(dòng)作非常非常慢(好像有延遲),有時(shí)候點(diǎn)擊后沒反應(yīng),必須鼠標(biāo)移開(點(diǎn)擊的列名稱)后,table的顯示或隱藏才能完成,這是什么問題呢?
代碼如下:
注:不想復(fù)制的朋友可以直接 下載源碼 ,里面只有這個(gè)demo,npm install 后就可以查看了,謝謝!
myTable.vue組件
<template>
<div class="tab-wrapper">
<div class="tab-title-wrapper">
<div class="tab-title">table測(cè)試組件</div>
<div class="tab-actions" @mouseover="showTableAction = true" @mouseout="showTableAction = false">
顯示/隱藏列
<div class="tab-action-item-wrapper" v-show="showTableAction">
<div v-for="head in heades" :key="head.key" @click="showColumn(head)" :class="{'active': head.isShow != false}">{{ head.label }}</div>
</div>
</div>
</div>
<table>
<thead>
<tr>
<th v-for="head in heades" v-show="head.isShow != false" :key="head.key">{{ head.label }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, idx) in tabDatas" :key="idx">
<td v-for="(head, idx2) in heades" v-show="head.isShow != false" :key="head.key + idx2">
{{ item[head.key] }}
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
name: 'table-component',
props: {
tabHeades: Array,
tabDatas: Array
},
data() {
return {
showTableAction: false,
heades: []
}
},
methods: {
showColumn(head) {
head.isShow = !head.isShow
console.log(JSON.stringify(head))
}
},
mounted() {
this.heades = this.tabHeades;
}
}
</script>
<style scoped>
.tab-wrapper, table{
width: 100%;
}
thead th{
cursor: pointer;
}
.tab-title-wrapper {
display: flex;
}
.tab-title-wrapper .tab-title{
flex: 1;
text-align: left;
}
.tab-title-wrapper .tab-actions{
position: relative;
cursor: pointer;
}
.tab-title-wrapper .tab-actions .tab-action-item-wrapper{
position: absolute;
text-align: left;
}
.tab-title-wrapper .tab-actions .tab-action-item-wrapper .active{
background: #ccc
}
</style>
父組件:
<template>
<div>
<my-table :tabHeades='tabHeades' :tabDatas='tabDatas'></my-table>
</div>
</template>
<script>
import MyTable from './myTable'
export default {
name: 'table-use',
data() {
return {
tabHeades: [
{label: '編號(hào)', key: 'id'},
{label: '訂單號(hào)', key: 'orderId', isShow: false},
{label: '用戶名', key: 'username'}
],
tabDatas: [
{'id': 1, 'orderId': '10001', 'username': '小張'},
{'id': 3, 'orderId': '10003', 'username': '小王'},
{'id': 4, 'orderId': '10004', 'username': 'aaa'},
{'id': 5, 'orderId': '10005', 'username': 'bbb'},
]
}
},
components: {
MyTable
}
}
</script>
看到標(biāo)題說卡,以為是列表數(shù)據(jù)量巨大,進(jìn)來看,發(fā)現(xiàn)才四條數(shù)據(jù)。。。
如你所說:
“隱藏的動(dòng)作非常非常慢(好像有延遲),有時(shí)候點(diǎn)擊后沒反應(yīng),必須鼠標(biāo)移開(點(diǎn)擊的列名稱)后,table的顯示或隱藏才能完成”,問題在這里,那么到底為什么呢???
鼠標(biāo)離開操作是一個(gè)很好的啟發(fā),與mouseout有關(guān)的你只在一個(gè)地方使用了。仔細(xì)想想 發(fā)生了什么,點(diǎn)擊click并沒有反應(yīng),然后鼠標(biāo)移開生效了,發(fā)生了什么。
鼠標(biāo)移開發(fā)生了模板重繪,順便導(dǎo)致你的 table表格更新。
其實(shí)你的根源不在于 數(shù)組變動(dòng),而在于你的 數(shù)據(jù)初始化不包含 isShow屬性,后來新增加的導(dǎo)致無法監(jiān)聽。
tabHeades: [
{label: '編號(hào)', key: 'id'},
{label: '訂單號(hào)', key: 'orderId', isShow: false},
{label: '用戶名', key: 'username'}
],
把其他兩條數(shù)據(jù)的isShow補(bǔ)全。
還有一個(gè)需要注意的地方
你在子組件mounted接受的 props的數(shù)據(jù)賦值給本地data
this.heades = this.tabHeades;
由于這是一個(gè)引用類型的數(shù)據(jù),傳遞的僅僅是地址引用。所以在click的時(shí)候,你做了數(shù)據(jù)更改,實(shí)際上父組件的數(shù)據(jù)一樣發(fā)生了更改。如果你想保持單純,請(qǐng)?jiān)谧咏M件使用 深拷貝操作。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
北大課工場(chǎng)是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國(guó)家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國(guó)制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國(guó)職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。