简体   繁体   English

如何阻止表再次构建

[英]How to stop the table from being built again

I've got a lovely table function from josh.trow @ link that builds a 4x4 table which can be filled with color.我有一张来自 josh.trow @ 链接的可爱表格 function,它构建了一个可以填充颜色的 4x4 表格。 The only problem is is that it rebuilds itself every time I call the function (hence the first time I call the file I see a 4x4 table, the second time I see a 8x4 table, and so on.) It doesn't multiply the rows because they are already limited to 4.唯一的问题是每次我调用 function 时它都会自行重建(因此我第一次调用文件时看到的是 4x4 表,第二次看到的是 8x4 表,依此类推。)它不会乘以行,因为它们已经限制为 4。

I can't see what check I need to put in that limits the table's columns to only being built once.我看不出我需要进行什么检查来限制表的列只能构建一次。 (4) (4)

Here's the JS code:这是JS代码:

function repeatedFunction(L, G, P, D) {
jQuery.fn.reverse = [].reverse;

var completeData = [L, G, P, D];
var max = 4;
$.each(completeData, function(intIndex, objValue) {
    if (objValue > max) max = objValue;
});
for (var i = 0; i < max; i++) 
{
    $('#statSheetTable').append('<tr id="resultRow' + i + '"></tr>');
    $.each(completeData, function(intIndex, objValue) {

    $('#resultRow' + i).append('<td name="column' + intIndex + '" ></td>');
    });
}

$.each(completeData, function(intIndex, objValue) {
    $('td[name=column' + intIndex + ']').reverse().each(function(idx, val) {
        if (idx < objValue) $(this).addClass('complete' + intIndex);
    });
});
}

I tried using a global variable, which stopped the table from being duplicated, but then my color filling code was not functioning (as it can't be refreshed with the new instructions to fill with color).我尝试使用一个全局变量,它阻止了表被复制,但是我的颜色填充代码不起作用(因为它不能用新的指令刷新来填充颜色)。

Essentially what I added to the above function was:基本上我添加到上面的 function 是:

var firstTime = true;
.....
function repeatedFunction(L, G, P, D, 4) {
if(firstTime == true)
{
    .....
    // code from above block
    .....
    firstTime = false;
}

Do your experienced JS eyes see where I can limit the table from building more columns?您经验丰富的 JS 眼睛是否看到我可以限制表格在哪里构建更多列?

Here you go chief, updated as you asked.在这里,您是 go 首席,按照您的要求进行了更新。

EDIT: Whoops, now it works:)编辑:哎呀,现在可以了:)

http://jsfiddle.net/X83ya/2/ http://jsfiddle.net/X83ya/2/

First, always use identical comparison operators when comparing boolean values:首先,在比较 boolean 值时,始终使用相同的比较运算符:

if(firstTime === true)

That being said, you don't need it if you just want to rebuild your table.话虽如此,如果您只想重建表,则不需要它。 $('#statSheetTable').append(... will just "add to" whatever was in there. Make sure you clear it out in the beginning like such: $('#statSheetTable').append(...只会“添加”其中的任何内容。确保在开始时将其清除,如下所示:

...
var max = 0;
$('#statSheetTable').html('');
...

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

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