高效的连续 .append() 用法
开始:
HTML
<table id='my-table' width='960' height='500'></table>
JS
var data = [
{ type: "Name", content: "John Doe" },
{ type: "Birthdate", content: "01/01/1970" },
{ type: "Salary", content: "$40,000,000" },
// ...300 more rows...
{ type: "Favorite Flavour", content: "Sour" }
];
附加在循环内
你刚收到大量数据。现在是时候循环并在页面上呈现它了。
你的第一个想法可能是做这样的事情:
var i; // <- the current item number
var count = data.length; // <- the total
var row; // <- for holding a reference to our row object
// Loop over the array
for ( i = 0; i < count; ++i ) {
row = data[ i ];
// Put the whole row into your table
$('#my-table').append(
$('<tr></tr>').append(
$('<td></td>').html(row.type),
$('<td></td>').html(row.content)
)
);
}
这是完全有效的,将完全呈现你的期望,但……
不要这样做
还记得 300 多行数据吗?
每一个都会强制浏览器重新计算每个元素的宽度,高度和定位值,以及任何其他样式 - 除非它们被布局边界分开,不幸的是,这个例子(因为它们是 <table>
元素的后代),他们不能。
在少量和少量列的情况下,这种性能损失肯定可以忽略不计。但我们希望每毫秒都能计算出来。
更好的选择
-
添加到单独的数组,在循环完成后追加
/**
* Repeated DOM traversal (following the tree of elements down until you reach
* what you're looking for - like our <table>) should also be avoided wherever possible.
*/
// Keep the table cached in a variable then use it until you think it's been removed
var $myTable = $('#my-table');
// To hold our new <tr> jQuery objects
var rowElements = [];
var count = data.length;
var i;
var row;
// Loop over the array
for ( i = 0; i < count; ++i ) {
rowElements.push(
$('<tr></tr>').append(
$('<td></td>').html(row.type),
$('<td></td>').html(row.content)
)
);
}
// Finally, insert ALL rows at once
$myTable.append(rowElements);
在这些选项中,这个选项最依赖于 jQuery。
-
使用现代 Array。*方法
var $myTable = $('#my-table');
// Looping with the .map() method
// - This will give us a brand new array based on the result of our callback function
var rowElements = data.map(function ( row ) {
// Create a row
var $row = $('<tr></tr>');
// Create the columns
var $type = $('<td></td>').html(row.type);
var $content = $('<td></td>').html(row.content);
// Add the columns to the row
$row.append($type, $content);
// Add to the newly-generated array
return $row;
});
// Finally, put ALL of the rows into your table
$myTable.append(rowElements);
功能上与之前的相同,只是更容易阅读。
-
使用 HTML 字符串(而不是 jQuery 内置方法)
// ...
var rowElements = data.map(function ( row ) {
var rowHTML = '<tr><td>';
rowHTML += row.type;
rowHTML += '</td><td>';
rowHTML += row.content;
rowHTML += '</td></tr>';
return rowHTML;
});
// Using .join('') here combines all the separate strings into one
$myTable.append(rowElements.join(''));
*完全有效,*但再次,不推荐。这迫使 jQuery 一次解析大量文本而不是必需的。jQuery 非常擅长正确使用时的功能。
-
手动创建元素,附加到文档片段
var $myTable = $(document.getElementById('my-table'));
/**
* Create a document fragment to hold our columns
* - after appending this to each row, it empties itself
* so we can re-use it in the next iteration.
*/
var colFragment = document.createDocumentFragment();
/**
* Loop over the array using .reduce() this time.
* We get a nice, tidy output without any side-effects.
* - In this example, the result will be a
* document fragment holding all the <tr> elements.
*/
var rowFragment = data.reduce(function ( fragment, row ) {
// Create a row
var rowEl = document.createElement('tr');
// Create the columns and the inner text nodes
var typeEl = document.createElement('td');
var typeText = document.createTextNode(row.type);
typeEl.appendChild(typeText);
var contentEl = document.createElement('td');
var contentText = document.createTextNode(row.content);
contentEl.appendChild(contentText);
// Add the columns to the column fragment
// - this would be useful if columns were iterated over separately
// but in this example it's just for show and tell.
colFragment.appendChild(typeEl);
colFragment.appendChild(contentEl);
rowEl.appendChild(colFragment);
// Add rowEl to fragment - this acts as a temporary buffer to
// accumulate multiple DOM nodes before bulk insertion
fragment.appendChild(rowEl);
return fragment;
}, document.createDocumentFragment());
// Now dump the whole fragment into your table
$myTable.append(rowFragment);
我个人的最爱。这说明了 jQuery 在较低级别执行的操作的一般概念。