[英]How to stop a innerHtml table repeating itself over and over and over again?
I have made an array with objects that get there info from three different user variables, however on one of these there are many sub variables that i don't want it to repeat itself every time the user presses the select button(which updates the table) instead i want it to just add onto (or take away) from the sections that it already has in the table. 我用对象组成了一个数组,这些对象从三个不同的用户变量那里获取信息,但是在其中一个上有很多子变量,我不希望它每次用户按下选择按钮时都重复一次(更新表) ),我希望它只是添加到表中已有的部分(或从中删除)。 thanks(if you need the variable code let me know) please help!!
谢谢(如果您需要可变代码让我知道)请帮助! i really need a solution!!
我真的需要一个解决方案!
//creating array
var gProducts = new Array();
var gTotalCost = 0;
// Adding Products to array gProducts
function addProduct
{
var product = new Object();
product.name = name;
product.cost = cost;
gProducts.push(product);
gTotalCost += parseInt(cost)
}
//Getting products from array, use of for in loop setting new table rows in blank var for each array item
function renderProducts()
{
var HTMLadd = ""
for (var i in gProducts)
{
if( gProducts[i].cost > 0){
HTMLadd = HTMLadd +
"<tr>"+
"<td class='tableSettings00' id=tableRow2 >" + gProducts[i].name +
"</td>"+
"<td class='tableSettings'>€<span id=tableRow2part2>" + gProducts[i].cost +
"</span></td>"+
"</tr>";
}
else
{
}
}
document.getElementById('tableRow').innerHTML = HTMLadd;
}
In the loop you always iterate through the whole gProducts
, hence the values already placed to the table are doubled every time you execute the function. 在循环中,您始终会遍历整个
gProducts
,因此,每次执行该函数时,已放置在表中的值都会加倍。
This example uses some methods in HTMLTableElement to create new rows and cells. 本示例在HTMLTableElement中使用一些方法来创建新的行和单元格。 It also has a counter (
index
) holding the starting position of each iteration. 它还具有一个计数器(
index
),用于保存每次迭代的开始位置。
var index = 0; // Counter for holding the current position in gProducts
function renderProducts () {
var n, row, cell, span;
for (n = index; n < gProducts.length; n++) { // Start looping from the first new index in gProducts
if (gProducts[n].cost > 0) {
row = table.insertRow(-1); // Inserts a row to the end of table
cell = row.insertCell(-1); // Inserts a cell to the end of the newly-created row
cell.className = 'tableSettings00'; // Sets class for the cell
cell.id = 'tableRow' + index; // Sets an unique id for the cell
cell.innerHTML = gProducts[n].name; // Sets the content for the cell
cell = row.insertCell(-1); // Create another cell to row
cell.className = 'tableSettings';
cell.id = 'tableRowPart' + index;
cell.innerHTML = '€';
span = document.createElement('span'); // Creates an empty span element
span.id = 'tableRow2part' + index; // Sets an unique id for span
span.appendChild(document.createTextNode(gProducts[n].cost)); // Appends some text to the span
cell.appendChild(span); // Appends the span to the cell
}
}
index = n; // Sets counter equal to the length of gProducts
}
A live demo at jsFiddle . jsFiddle上的现场演示 。
As a sidenote, personally I prefer not to use id
s for elements within a table. 附带说明,就我个人而言,我不希望对表中的元素使用
id
。 If id
s are needed, you probably have constructed your table incorrectly, or the data is not suitable being shown in a table. 如果需要
id
,则您可能构造了错误的表,或者该数据不适合在表中显示。
You can use indices and table.rows
and rows.cells
collections to find cells, and if some elements has to be searched for from a cell, use firstElementChild
and nextElementSibling
properties of a cell to find them. 您可以使用索引以及
table.rows
和rows.cells
集合来查找单元格,如果必须从单元格中搜索某些元素,请使用单元格的firstElementChild
和nextElementSibling
属性来查找它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.