生成合并的单元格数组

时间:2016-10-13 16:10:00

标签: javascript arrays merge handsontable

例如我有一个行数组:

{ "name1", "value1", "other1", "another1" },
{ "name1", "value2", "other2", "another2" },
{ "name2", "value3", "other3", "another3" },
{ "name2", "value4", "other4", "another4" }

我无法生成具有合并单元结构的数组,如:

{ row: 0, col: 0, colspan: 1, rowspan: 2 }, 
{ row: 2, col: 0, colspan: 1, rowspan: 2 }

其中是行索引, col 列索引, colspan 是列的范围, rowspan 是桌子上的行数。

到目前为止,我有代码:

var prev;
var entry = {};
var repeat = 1;
var result = [];
for(a in data){ 
  if(a>0){
    prev = data[a-1];
    for(b in data[a]){
      if(prev[b]===data[a][b]){
         if(repeat==1){
            entry["row"] = a-1;
            entry["col"] = b;
            entry["colspan"] = 1; 
         }
         repeat++;
      } 
    }
  }
}

但是我无法理解何时可以再次rowspan=repeat然后再repeat=1 entry,同时我需要将result推到var cols = data[0].length; var prev; var entry = {}; var repeat = []; for(var i=0;i<cols;i++){ repeat.push(1); } var result = []; for(a in data){ if(a>0){ prev = data[a-1]; for(b in data[a]){ if(prev[b]!=null&&prev[b]===data[a][b]){ if(repeat[b]==1){ entry["row"] = a-1; entry["col"] = parseInt(b); entry["colspan"] = 1; } repeat[b]++; }else{ if(repeat[b]>1){ entry["rowspan"] = repeat[b]; result.push(entry); repeat[b] = 1; entry = {}; } } } } }

谢谢大家的帮助!

编辑:我已经设法做了几乎工作的例子,但是没有什么错误,它没有推动最后一次合并...任何想法让它推动最后一个条目?!

 Declare @YourTable table ([Event] varchar(100),[Start] DateTime,[End] DateTime, [Tag] varchar(25))
 Insert Into @YourTable values
 ('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:44','9/9/16 10:49','Big'),
 ('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:50','9/9/16 10:51','Small'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 12:51','4/4/16 13:58','Big'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:04','4/4/16 14:29','Small'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:51','4/4/16 14:58','Big'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 15:04','4/4/16 15:29','Small'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4-4-16 15:45','4-4-16 15:55','Big'),
 ('11PIC41010.PV 11-PSV-401002W 11-PSV-401002D','4/4/16 16:04','4/4/16 16:45','Big')
很明显,我的结构有一个减号,因为它使用了prev变量并从第二行开始......但我没有想出任何其他更好的方法。

1 个答案:

答案 0 :(得分:0)

所以我想出了我该怎么做:)这是我的代码,以防万一有人需要它。不是最好的方式......但是它有效!

var cols = data[0].length;
var prev;
var entry = {};
var repeat = [];
for(var i=0;i<cols;i++){
  repeat.push(1);
}
var result = [];
for(a in data){
  if(a>0){
    prev = data[a-1];
    for(b in data[a]){
      if(prev[b]!=null&&prev[b]===data[a][b]){
        if(repeat[b]==1){
          entry["row"] = a-1;
          entry["col"] = parseInt(b);
          entry["colspan"] = 1;
        }
        repeat[b]++;
      }else{
        if(repeat[b]>1){
          entry["rowspan"] = repeat[b];
          result.push(entry);
          repeat[b] = 1;
          entry = {};
        }
      }
    }
    if(a==data.length-1){
      for(var i=0;i<repeat.length;i++){
        if(repeat[i]>1){
          entry["rowspan"] = repeat[i];
          result.push(entry);
          repeat[i] = 1;
          entry = {};
        }
      }  
    }
  }
}