使用javascript填充另一个表的内容表

时间:2013-07-30 11:49:06

标签: javascript arrays copy

我的页面上有一个可选div的网格,其中包含定义行和列的属性。当我选择其中一些时,会创建三维表 - 让我们将其命名为表格复制。

当我再次选择其他一些元素时,会创建另一个三维表 - 表格粘贴

第一次选择两列和两行后,它将

   x,y - positions
   at1,at2,at2 - attributes for later copy

                             Table Copy
                          1                2
                0:[x,y,at1,at2,at3],[x,y,at1,at2,at3]
                1:[x,y,at1,at2,at3],[x,y,at1,at2,at3]

选择三行三行后,它将如下所示

                             Table Copy
                          1                2
                0:[x,y,at1,at2,at3],[x,y,at1,at2,at3]
                1:[x,y,at1,at2,at3],[x,y,at1,at2,at3]

                             Table Paste
                  1                2                 3               
       0:[x,y,at1,at2,at3],[x,y,at1,at2,at3],[x,y,at1,at2,at3]
       1:[x,y,at1,at2,at3],[x,y,at1,at2,at3],[x,y,at1,at2,at3]
       2:[x,y,at1,at2,at3],[x,y,at1,at2,at3],[x,y,at1,at2,at3]

现在我需要一个只使用表格副本

填充表格的功能
                             Table Paste
                  1                2                 3               
            0:[tabCopy[0][1]],[tabCopy[0][2]],[tabCopy[0][1]]
            1:[tabCopy[1][1]],[tabCopy[1][1]],[tabCopy[1][1]]
            2:[tabCopy[0][1]],[tabCopy[0][2]],[tabCopy[0][1]]

当然,两种阵列都有很多可能性。

表副本可以有4行,表格只有3个。然后表格副本的第四行应该被“忽略”。

如果表格仅在1行1列中复制,则表格粘贴中的所有记录应该与此相同

如果表格粘贴只有1行1列,则只应该从表格复制中获取第一条记录。

我希望我能清楚地描述一切:)

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好吧,让我们看看我是否理解你......

function copyArrays(from, to) {
    for (var i = 0, j = 0; i < to.length;
         i++, j = (j + 1) % from.length) {
        if ((from[j] instanceof Array) &&
            (to[i] instanceof Array)) {
            copyArrays(from[j], to[i]);
        } else {
            to[i] = from[j];
        }
    }
}

var from = [["x", "y", "at1", "at2", "at3"],
            ["x", "y", "at1", "at2", "at3"],
            ["x", "y", "at1", "at2", "at3"],
            ["x", "y", "at1", "at2", "at3"]];

var to = [["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"],
          ["+", "-", "123", "456", "789"]];

然后:

copyArrays(from, to);

会给你:

[["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"], 
 ["x", "y", "at1", "at2", "at3"]]