JS DataTables ColReorder API重新排序Bug?

时间:2014-12-05 00:24:13

标签: javascript jquery datatable

使用API​​重新排序列时,JS DataTables ColReorder会导致意外行为。 https://github.com/DataTables/ColReorder/

第一次重新订购工作正常,例如 tableColReorder.fnOrder([2,1,0]);

但是这个后续的重新订购应该将列恢复到原始订单,但它不会。为什么不? tableColReorder.fnOrder([0,1,2]);

这里简单的小提琴示例: http://jsfiddle.net/h7wdt72k/

$(document).ready(function () {

    // Initialize data table extension.
    var table = $('table')
      .DataTable({
        paging: false,
        searching: false,
        ordering: false,
        bInfo: false
    });

    // Initialize column re-order extension.
    tableColReorder = new $.fn.dataTable.ColReorder(table);

    // Re-order columns.  Switch first/last columns.
    tableColReorder.fnOrder([2, 1, 0]);

    // Re-order columns to original order 0, 1, 2.  Does not work!?
    tableColReorder.fnOrder([0, 1, 2]);
    // Get current column order. Did not apply re-order directly above.  Why not!?
    alert(tableColReorder.fnOrder());

    // This statement returns columns to original order 1, 2, 3.  Works but why!?
    //tableColReorder.fnOrder([2, 1, 0]);   
});

HTML:

<table border="1">
  <thead>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

行为是设计而非错误。

//To switch position of two columns, always reorder based on array of consecutive integers (array length = number of columns).
//Even if columns already moved. Start with array of consecutive integers.
newColOrder = [0,1,2];

// Set new order of columns.
newColOrder[colTo] = colFrom; // colFrom = index to move column from.
newColOrder[colFrom] = colTo; // colTo = index to move column to.
e.g. to switch first and last column.
newColOrder[0] = 2;
newColOrder[2] = 0;
// [2,1,0];

// Reorder columns. Switch position of first and last column.
tableColReorder.fnOrder(newColOrder);

// Switch position of first and last column again.  Returns columns to original position.
tableColReorder.fnOrder(newColOrder);