【问题标题】:Split a "big" table to smaller tables将“大”表拆分为较小的表
【发布时间】:2012-08-18 07:05:09
【问题描述】:

我想将一个“大”表(很多列)拆分为较小的表,例如每 2 列。

有没有简单的方法来做到这一点?

我这里只有http://jsfiddle.net/xy3UF/4/ 的桌子。例如,我想将它每 2 列拆分一次。因此,我应该有一个包含 # 列的三个表,每个表包含大表中的 2 个列。

所需输出:http://jsfiddle.net/xy3UF/15/

【问题讨论】:

  • 你想要的输出有点混乱。请为您想要的输出创建 HTML 代码。
  • 这里是:jsfiddle.net/xy3UF/15
  • 您想要一个特定于此示例的答案还是通用答案?
  • AI 相信一个通用的答案也会对其他人更有帮助..
  • 嗯,你不能给列分配类,克隆表,然后根据表/列类隐藏类吗?

标签: javascript jquery html html-table


【解决方案1】:
function split($table, chunkSize) {
  var cols = $("th", $table).length - 1;
  var n = cols / chunkSize;

  for (var i = 1; i <= n; i++) {
     $("<br/>").appendTo("body");
     var $newTable = $table.clone().appendTo("body");
     for (var j = cols + 1; j > 1; j--) {
         if (j + chunkSize - 1 <= chunkSize * i || j > chunkSize * i + 1) {
             $('td:nth-child(' + j + '),th:nth-child(' + j + ')', $newTable).remove();
         }
     }
  }  
}

其中$table 是表格jQuery 对象,chunkSize 是每个split 的大小。在您的示例中,将其称为split($("table"), 2)。请注意,chunkSize 必须将列数(不包括第一列)均分才能正常工作,例如,对于有 7 列的表,chunkSizevalid 值为 1 、2 和 3。

DEMO.

【讨论】:

  • 您可以使用var n = Math.ceil(cols / chunkSize);,如果您使用split($("table"), 4),它会将您的6列表(不包括第一个)拆分为4列和2列表
  • 您将如何使用 colspan 处理表数据,我正在尝试使用此示例,但是我的前 2 行所有 colspan 并且由于列的数量不匹配,它弄乱了它下面的所有行
【解决方案2】:

我制作了更好的表格拆分功能版本,支持修复列(在所有表格中重复),它浪费了很多时间:

function range(start, end) {
  var array = new Array();
  for (var i = start; i <= end; i++) {
    array.push(i);
  }
  return array;
}

function splitTables($tables, chunkSize, fixCols) {
  $table = $($tables);
  console.log('fixCols :', fixCols);
  fixCols = fixCols || [];
  console.log('fixCols :', fixCols);
  fixCols = fixCols.sort();
  console.log('fixCols :', fixCols);
  //chunkSize -= fixCols.length;
  var rowLength = $('tr:first>*', $table).length;
  console.log('total length:', rowLength);
  var n = Math.ceil(rowLength / chunkSize);
  var bufferTables = [];
  var numberList = range(1, rowLength);

  for (var i = 1; i <= n; i++) {
    var colList = fixCols.slice(0);
    //console.log('fixCols :',fixCols);
    //console.log('colList :',colList);
    console.log('numberList :', numberList);
    while (colList.length < chunkSize && numberList.length > 0) {
      var index = numberList.shift();
      console.log(index, colList);
      if (colList.indexOf(index) == -1) {
        colList.push(index);
      }
    }

    console.log('col list:', colList);
    var $newTable = $table.clone(true)
    for (var index = 1;
      (index <= rowLength); index++) {
      if (colList.indexOf(index) == -1) {
        console.log('removing:', index);
        $('tr>:nth-child(' + index + ')', $newTable).hide();
      }
    }
    bufferTables.push($newTable)
  }

  $(bufferTables.reverse()).each(function(i, $el) {
    $('<br/>').insertAfter($table);
    $el.insertAfter($table);
  });
  $table.remove();

}

$(function() {
  $('#sbtn').click(function() {
    splitTables($("#tb"), 3, [1]);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="sbtn">Split Table !</button>

<table border="1" id="tb">
  <thead>
    <tr>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td>Sum</td>
      <td>$180</td>
      <td>$500</td>
      <td>$300</td>
      <td>$700</td>
      <td>$600</td>
      <td>$1000</td>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>Home</td>
      <td>$100</td>
      <td>$200</td>
      <td>$200</td>
      <td>$300</td>
      <td>$400</td>
      <td>$500</td>
    </tr>
    <tr>
      <td>Work</td>
      <td>$80</td>
      <td>$300</td>
      <td>$100</td>
      <td>$400</td>
      <td>$200</td>
      <td>$500</td>
    </tr>
  </tbody>
</table>

【讨论】:

【解决方案3】:

这要简单得多。

1) 标记 TR:

<tr><td>some</td><!-- SPLITHERE -->

2) 拆分表格:

document.body.innerHTML = document.body.innerHTML.replace(/<!-- SPLITHERE --\>\s*<\/tr>/i, '</tr></table></div><div class="eachPage"><table class="items">');

注意:您必须对该表的父元素(在我的示例中为正文)进行 innerHTML 更改,而不是表本身,这是行不通的。

【讨论】:

    【解决方案4】:

    使用 XSLT 可以轻松解决问题。

    请访问:Split large table into several smaller tables

    【讨论】:

    • 有这样的例子吗?
    • XSLT 是一个不错的建议,但需要先将这些数据转换为格式良好的 XML 文档。
    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多