简体   繁体   English

使用 vue.js 提高 hover 对表列的反应性/性能

[英]Improve reactivity/performance of hover effect on table column with vue.js

I have a pretty big table in one of my Vue.js component.我的 Vue.js 组件之一中有一张相当大的桌子。 I want the current line and column to change color when the mouse is hover a cell.当鼠标是 hover 一个单元格时,我希望当前行和列改变颜色。

For the row hover effect, it's pretty easy in CSS.对于行 hover 效果,在 CSS 中非常容易。 But for the column, I have to do it in javascript (for various reason, I cannot use this trick in my current app).但是对于专栏,我必须在 javascript 中进行(由于各种原因,我不能在我当前的应用程序中使用这个技巧)。

Now, my problem is that using vue.js reactiveness to do this gets pretty slow when the table is too big.现在,我的问题是,当表太大时,使用 vue.js 反应性来执行此操作会变得非常慢。 See the included snippet (I recommend opening it in full page) where the row hover effect is pretty reactive (done in CSS) and the column hover effect is very sluggish (done with vue.js).请参阅包含的片段(我建议在整页中打开它),其中行 hover 效果非常被动(在 CSS 中完成),列 hover 效果非常缓慢(使用 vue.js 完成)。

I guess my solution is far from being the best so my question is: Is there a solution to get the column hover effect to be (almost) as reactive as the line hover effect?我想我的解决方案远不是最好的,所以我的问题是:有没有一种解决方案可以让 hover 列效应(几乎)与 hover 列效应一样反应?

 new Vue({ el: "#app", data: { hovered: { col: -1, }, columns: [...(new Array(50).keys())].map(c => ({ label: c, index: c })), rows: [...(new Array(200).keys())].map(r => ({ label: r, index: r })) }, methods: { hoverCol(colIndex) { this.hovered.col = colIndex } } })
 body { background: #20262E; padding: 20px; font-family: Helvetica; } #app { background: #fff; border-radius: 4px; padding: 20px; transition: all 0.2s; } table { width: 100%; } table tr:hover { background-color: green; }.bg-green { background-color: green; }.bg-grey { background-color: grey; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <table> <thead> <tr> <th v-for="col in columns":key="`header-col-${col.index}`":class="{ 'bg-grey': col.index === hovered.col }"> col-{{col.label}} </th> </tr> </thead> <tbody> <tr v-for="row in rows":key="`row-${row.index}`"> <td v-for="col in columns":key="`cell-${col.index}-${row.index}`" @mouseenter="hoverCol(col.index)":class="{ 'bg-green': col.index === hovered.col }"> cell-{{col.label}}-{{row.label}} </td> </tr> </tbody> </table> </div>

Okay I got my solution, instead of using vue reactivity to change the hover class, I can set it directly on the element using refs.好的,我得到了我的解决方案,而不是使用 vue 反应性来更改 hover class,我可以使用 refs 直接在元素上设置它。 Speed is way better.速度要好很多。

Snippet example:片段示例:

 new Vue({ el: "#app", data: { colHover: null, columns: [...(new Array(50).keys())].map(c => ({ label: c, index: c })), rows: [...(new Array(200).keys())].map(r => ({ label: r, index: r })) }, methods: { hoverCol(colIndex) { this.rows.forEach(r => { if (this.colHover.== null) { this.$refs[`cell_${this.colHover}_${r.index}`][0].classList = [] } this.$refs[`cell_${colIndex}_${r.index}`][0].classList.add('bg-green') }) this.colHover = colIndex } } })
 body { background: #20262E; padding: 20px; font-family: Helvetica; } #app { background: #fff; border-radius: 4px; padding: 20px; transition: all 0.2s; } table { width: 100%; } table tr:hover { background-color: green; }.bg-green { background-color: green; }.bg-grey { background-color: grey; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <table> <thead> <tr> <th v-for="col in columns":key="`header-col-${col.index}`"> col-{{col.label}} </th> </tr> </thead> <tbody> <tr v-for="row in rows":key="`row-${row.index}`"> <td v-for="col in columns":key="`cell-${col.index}-${row.index}`" @mouseenter="hoverCol(col.index)":ref="`cell_${col.index}_${row.index}`" > cell-{{col.label}}-{{row.label}} </td> </tr> </tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM