4

「大きな」テーブル (多数の列) を、たとえば 2 列ごとに小さなテーブルに分割したいと考えています。

それを行う簡単な方法はありますか?

私はここにテーブルしかありませんhttp://jsfiddle.net/xy3UF/4/。たとえば、2列ごとに分割したいと思います。その結果、列を含む 3 つのテーブルがあり#、それぞれに大きなテーブルの 2 つの列が含まれている必要があります。

望ましい出力: http://jsfiddle.net/xy3UF/15/

4

4 に答える 4

6
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のサイズです。あなたの例では、それを. これが正しく機能するには、列数 (最初の列を除く) を均等に分割する必要があることに注意してください。たとえば、7 列のテーブルの場合、有効な値は 1、2、および 3 です。split($("table"), 2)chunkSizechunkSize

デモ

于 2012-08-22T21:00:50.317 に答える
1

固定列 (すべてのテーブルで繰り返す) をサポートするテーブル分割関数のより良いバージョンを作成しましたが、多くの時間を無駄にします。

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>

于 2016-10-18T07:37:49.080 に答える
0

XSLT を使用すると、問題を簡単に解決できます。

ここにアクセスしてください:大きなテーブルをいくつかの小さなテーブルに分割する

于 2012-08-22T19:53:22.097 に答える