首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可处理无限滚动附加数据并应用合并单元格格式

问可处理无限滚动附加数据并应用合并单元格格式
EN

Stack Overflow用户
提问于 2016-11-02 11:35:40
回答 1查看 1.5K关注 0票数 2

我正在使用手柄,并希望用无限滚动来附加数据。我可以使用以下简化示例来实现这一点:

Array.prototype.push.apply

我希望能找到一种手持式的方法,让我可以追加数据,但是我已经搜索了,没有发现任何东西。也许我错过了什么,如果是这样的话,我真的很感激你能提供的任何指导。

下面是我简化的无限滚动示例:

​

代码语言:javascript
复制
$(document).ready(function () {
  var data = [
    ["", "Maserati", "Mazda", "Mercedes", "Mini", "Mitsubishi"],
    ["2000", 0, 2941, 4303, 354, 5814],
    ["2001", 5, 2905, 2867, 412, 5284],
    ["2002", 0, 2941, 4303, 354, 5814],
    ["2003", 5, 2905, 2867, 412, 5284],
    ["2004", 4, 2517, 4822, 552, 6127],
    ["2005", 0, 2941, 4303, 354, 5814],
    ["2006", 5, 2905, 2867, 412, 5284],
    ["2007", 4, 2517, 4822, 552, 6127],
    ["2008", 0, 2941, 4303, 354, 5814],
    ["2009", 5, 2905, 2867, 412, 5284],
    ["2010", 4, 2517, 4822, 552, 6127],
    ["2011", 2, 2422, 5399, 776, 4151],
    ["2012", 0, 2941, 4303, 354, 5814],
    ["2013", 5, 2905, 2867, 412, 5284],
    ["2014", 4, 2517, 4822, 552, 6127],
    ["2015", 2, 2422, 5399, 776, 4151],
  ];
  var moreData = [
    ["2016", 10, 11, 12, 13, 14],
    ["2017", 20, 11, 14, 14, 10],
    ["2018", 30, 15, 12, 13, 25],
    ["2019", 20, 11, 14, 14, 10],
    ["2020", 30, 15, 12, 13, 25]
  ];
  var $container2 = $("#HoT2");

  $container2.handsontable({
      data: data,
      colHeaders: true,
      contextMenu: true,
  });
  
  $(window).scroll(function () {
    if($(document).height() <= $(window).scrollTop() + $(window).height()) {
      appendHOT2();
    }
  }); 

  function appendHOT2() {
//    rowCount = $container2.handsontable('countRows'); console.log("Row Count: "+rowCount);
//    $container2.handsontable('alter', 'insert_row', rowCount, 3); // Add set number of empty rows

    var hotData = $container2.handsontable('getData');
    Array.prototype.push.apply(hotData, moreData);
    console.log("Array: "+ hotData);
    $container2.handsontable('loadData', hotData); //Replaces current data doesn't append
  }
});
代码语言:javascript
复制
</style><!-- Ugly Hack due to jsFiddle issue --><script src="http://docs.handsontable.com/scripts/jquery.min.js"></script> <script src="http://docs.handsontable.com/bower_components/handsontable/dist/handsontable.full.js"></script> <link type="text/css" rel="stylesheet" href="http://docs.handsontable.com//bower_components/handsontable/dist/handsontable.full.min.css"> <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css">
代码语言:javascript
复制
<h2>Infinite Scroll - Simple Load More Rows</h2>
<div id="HoT2" class="handsontable"></div>

​

实际上,我想对对象数据(从ajax调用中)做一些更复杂的事情,但是我不知道如何用数据和格式更新手柄表。

下面是我的例子,就我所知:

​

代码语言:javascript
复制
$(document).ready(function () {
  var colHeaders = [
  'ID','Name','Address'
  ];
  var mergeCells = [
    {row:0, col:0, rowspan:2, colspan:1},
    {row:0, col:1, rowspan:2, colspan:1},
    {row:0, col:2, rowspan:1, colspan:1},
    {row:2, col:0, rowspan:1, colspan:1},
    {row:2, col:1, rowspan:1, colspan:1},
    {row:2, col:2, rowspan:1, colspan:1},
    {row:3, col:0, rowspan:1, colspan:1},
    {row:3, col:1, rowspan:1, colspan:1},
    {row:3, col:2, rowspan:1, colspan:1},
    {row:4, col:0, rowspan:2, colspan:1},
    {row:4, col:1, rowspan:2, colspan:1},
    {row:4, col:2, rowspan:1, colspan:1},
    {row:5, col:0, rowspan:1, colspan:1},
    {row:5, col:1, rowspan:1, colspan:1},
    {row:5, col:2, rowspan:1, colspan:1},
    {row:6, col:0, rowspan:1, colspan:1},
    {row:6, col:1, rowspan:1, colspan:1},
    {row:6, col:2, rowspan:1, colspan:1},
  ];
  var data = [
    {id: 1, name: 'Ted Right', address: 'A1'},
    {id: 1, name: 'Ted Right', address: 'A2'},
    {id: 2, name: 'Frank Honest', address: 'B'},
    {id: 3, name: 'Joan Well', address: 'C'},
    {id: 4, name: 'Gail Polite', address: 'D1'},
    {id: 4, name: 'Gail Polite', address: 'D2'},
    {id: 5, name: 'Michael Fair', address: 'E'},
  ];
  var moreData = [
      {id: 6, name: 'Ted 2Right', address: 'F'},
      {id: 7, name: 'Frank 2Honest', address: 'G1'},
      {id: 7, name: 'Frank 2Honest', address: 'G2'},
      {id: 8, name: 'Joan 2Well', address: 'H'},
      {id: 9, name: 'Gail 2Polite', address: 'I1'},
      {id: 9, name: 'Gail 2Polite', address: 'I2'},
      {id: 9, name: 'Gail 2Polite', address: 'I3'},
      {id: 10, name: 'Michael 2Fair', address: 'J'},
  ];
  var moreMergeCells = [
    {row:7, col:0, rowspan:1, colspan:1},
    {row:7, col:1, rowspan:1, colspan:1},
    {row:7, col:2, rowspan:1, colspan:1},
    {row:8, col:0, rowspan:2, colspan:1},
    {row:8, col:1, rowspan:2, colspan:1},
    {row:8, col:2, rowspan:1, colspan:1},
    {row:9, col:0, rowspan:1, colspan:1},
    {row:9, col:1, rowspan:1, colspan:1},
    {row:9, col:2, rowspan:1, colspan:1},
    {row:10, col:0, rowspan:3, colspan:1},
    {row:10, col:1, rowspan:3, colspan:1},
    {row:10, col:2, rowspan:1, colspan:1},
    {row:11, col:0, rowspan:1, colspan:1},
    {row:11, col:1, rowspan:1, colspan:1},
    {row:11, col:2, rowspan:1, colspan:1},
  ];
  var $container = $("#HoT2");

  $container.handsontable({
      data: data,
      colHeaders: colHeaders,
      mergeCells: mergeCells,
      contextMenu: false,
  });
  
  $(window).scroll(function () {
    if($(document).height() <= $(window).scrollTop() + $(window).height()) {
      appendHOT2();
    }
  }); 

  function appendHOT2() {
    console.log("Append Data and MergeCells...");
    
// Append moreData and moreMergeCells data to the handsontable display 
// How can this be achieved?
  }
});
代码语言:javascript
复制
</style><!-- Ugly Hack due to jsFiddle issue --> <script src="http://docs.handsontable.com/scripts/jquery.min.js"></script> <script src="http://docs.handsontable.com/bower_components/handsontable/dist/handsontable.full.js"></script> <link type="text/css" rel="stylesheet" href="http://docs.handsontable.com//bower_components/handsontable/dist/handsontable.full.min.css"> <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css">
代码语言:javascript
复制
<h2>Infinite Scroll - Load More Formatted Rows</h2>
<div id="HoT2" class="handsontable"></div>

​

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-04 09:42:35

正如我在评论中提到的,如果您想在滚动时更新表的设置(参考文献:更新mergeCells设置),更一般的函数是,IMO,强制性的.至少要简单得多)

这一行是您每次滚动时需要做的:

代码语言:javascript
复制
$container.handsontable('updateSettings', ({mergeCells: merge()}));

(您将注意到,在表初始化之后,我使用了一次)。

请参见下面的函数,根据示例中数据的结构,如果列1的值重复,则合并列1和2:

代码语言:javascript
复制
function merge() {
  var mergeCells=[], rowspan=1, tempName = '';

  for(var i=0; i<$container.handsontable('getData').length;i++) {
    if ($container.handsontable('getDataAtCell', i,1) === tempName) {
      rowspan+=1;
    } 
    else  {
      mergeCells.push({row:i-rowspan, col:0, rowspan:rowspan, colspan:1})
      mergeCells.push({row:i-rowspan, col:1, rowspan:rowspan, colspan:1})
      rowspan=1
    }
    tempName = $container.handsontable('getDataAtCell', i,1);
  }
  return mergeCells;
}

您可以在这个JSFiddle中找到完整和工作的示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40378960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档