admin管理员组文章数量:1406060
I found some similar questions like this and this. But my problem is somewhat different. I'm polling data for table every second from my REST Api with axios. I need is the user to freely manipulate (i.e. order, sort and select) the rows when data updates. In general, when there is data update, all selections are removed and orders are lost.
I've created a local store with my data which I pass as a reference to my table and update it like this:
setStrategies(newStrategies) {
if (this.state.strategies.length == 0) {
newStrategies.forEach(strategy => {
this.state.strategies.push(strategy)
});
} else {
this.state.strategies.forEach(strategy => {
var newStrategy = newStrategies.find(x => x.strategyName === strategy.strategyName);
strategy.status = newStrategy.status;
strategy.lastPingTime = newStrategy.lastPingTime;
});
}
It works but I think it's overplicated. So could you tell me what is the best approach or may be table-ponent that could do binding automaticaly?
I found some similar questions like this and this. But my problem is somewhat different. I'm polling data for table every second from my REST Api with axios. I need is the user to freely manipulate (i.e. order, sort and select) the rows when data updates. In general, when there is data update, all selections are removed and orders are lost.
I've created a local store with my data which I pass as a reference to my table and update it like this:
setStrategies(newStrategies) {
if (this.state.strategies.length == 0) {
newStrategies.forEach(strategy => {
this.state.strategies.push(strategy)
});
} else {
this.state.strategies.forEach(strategy => {
var newStrategy = newStrategies.find(x => x.strategyName === strategy.strategyName);
strategy.status = newStrategy.status;
strategy.lastPingTime = newStrategy.lastPingTime;
});
}
It works but I think it's overplicated. So could you tell me what is the best approach or may be table-ponent that could do binding automaticaly?
Share Improve this question asked Jun 13, 2018 at 9:14 xnegxneg 1,3681 gold badge17 silver badges28 bronze badges 03 Answers
Reset to default 1You can use keys in Vue to items in list. This will handle the refresh of your list and reuse and reorder existing elements.
<div v-for="item in items" :key="item.id">
<!-- content -->
</div>
As stated in the docs :
When Vue is updating a list of elements rendered with
v-for
, by default it uses an “in-place patch” strategy. If the order of the data items has changed, instead of moving the DOM elements to match the order of the items, Vue will patch each element in-place and make sure it reflects what should be rendered at that particular index.This default mode is efficient, but only suitable when your list render output does not rely on child ponent state or temporary DOM state (e.g. form input values).
To give Vue a hint so that it can track each node’s identity, and thus reuse and reorder existing elements, you need to provide a unique
key
attribute for each item. An ideal value forkey
would be the unique id of each item.
I don't understand the code you posted, but I would treat the source array and the filter and sort columns as page state, in your local store. Then, create yourself a puted property on your vue ponent that orders and sorts the source array. Every time the source array is replaced, the puted property will update. You may also want to look at vue-keep-scroll, so the user's position on the page is preserved across updates.
I am not diminishing the value other answers but I found that Vuetify table does exact what I want.
本文标签: javascriptVue table realtime updateStack Overflow
版权声明:本文标题:javascript - Vue table realtime update - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744958634a2634512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论