Home >
Web Front-end >
JS Tutorial >
v-for implements the method of generating a table and adding a serial number to the table
v-for implements the method of generating a table and adding a serial number to the table
小云云
Release: 2018-01-20 13:47:56
Original
2082 people have browsed it
This article mainly introduces the relevant information about using vue's v-for to generate a table and add serial numbers to the table. Friends who need it can refer to it. I hope it can help everyone.
Now there is a table generated using the mybatis paging plug-in. The data in the table is obtained through vue. The front-end display uses
The background vue gets the data, uses the paging plug-in to query and then uses the callback to return the result to a model of vue
The requirement is: add a serial number to the generated tableJust started using the js function
function rownum(){
//首先拿到table中tr的数量 得到一共多少条数据
var len = $("#tableId table tbody tr").length;
//使用循环给每条数据加上序号
for(var i = 1;i<len+1;i++){
$('#tableId table tr:eq('+i+') span:first').text(i);
}
}
Copy after login
Put the above method on the event of clicking to generate the table, you can display the serial number, and then click the next page of the page Page or page number, when jumping to the next page, the serial number disappeared. It is natural to think that there should be an operation to add the serial number after clicking the next page, so I found the next page data display method, plus the above js method, the result did not take effect. I personally think that after the data was found, the rownum method was added before the dom was refreshed, and then after the dom was updated, the serial number disappearedThe problem was finally solved by searching for information as follows, adding the Vue.nextTick(function(){}) method in every place where paging queries appear
When do you need to use Vue. nextTick()The DOM operations you perform in the created() hook function of the Vue life cycle must be placed in the callback function of Vue.nextTick(). What is the reason? The reason is that the DOM is not rendered at all when the create() hook function is executed, and DOM operations at this time are in vain, so the js code for DOM operations must be put into Vue. In the callback function of nextTick(). Corresponding to this is the mounted hook function, because all DOM mounting and rendering have been completed when this hook function is executed, there will be no problem in performing any DOM operations in this hook function. When there is an operation to be performed after the data changes, and this operation requires the use of a DOM structure that changes as the data changes, this operation should be put into the callback function of Vue.nextTick(). Vue performs DOM updates asynchronously. Once data changes are observed, Vue will open a queue and then push the watcher that observes data changes in the same event loop into this queue. If this watcher is triggered multiple times, it will only be pushed to the queue once. This buffering behavior can effectively eliminate unnecessary calculations and DOm operations caused by duplicate data. In the next event loop, Vue will clear the queue and perform the necessary DOM updates. When you set
vm.someData = 'new value',DOM
Copy after login
it will not be updated immediately, but when the asynchronous queue is cleared, that is, when the update is performed at the beginning of the next event loop Only then will the necessary DOM updates be performed. Problems will arise if you want to do something based on the updated DOM state at this time. . In order to wait for Vue to finish updating the DOM after the data changes, you can use Vue.nextTick(callback) immediately after the data changes. This callback function will be called after the DOM update is completed.
Summary:
* `Vue.nextTick(callback)`, when the data changes, the callback is executed after the update.
* `Vue.$nextTick(callback)`, when the dom changes, the callback is executed after updating.
Related recommendations:
Examples to explain vue v-for data processing
Introduction and use of the v for instruction in the vue component Analysis of alarm issues in v-for
Circular use of v-for instruction example code
The above is the detailed content of v-for implements the method of generating a table and adding a serial number to the table. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn