Datatable上从左到右滚动

时间:2016-06-28 12:49:59

标签: jquery datatables row columnsorting

我遇到需要使用Datatables渲染表格的情况。但是,我需要能够水平(从左到右)而不是垂直(从上到下)对其进行排序。我有什么方法可以做到这一点吗?

插图:列为A,B,C,D,E。行是R1,R2,R3,R4 ...... R30。我不想将A排序到E,但我确实希望将R1中的任何一个排序到R30,以便重新排列A-E。例如,对于R1,值的升序可以是A,E,D,B,C,对于R2的值,它可以是D,E,B,A,C。我应该能够点击行索引(该行的第一列)并查看我的 重新排序。 (默认是正在重新排序的行)

更新:我发现此示例用于水平排序Sorting Table Columns with jQuery Table Sorter,但如何使用数据表进行处理?

2 个答案:

答案 0 :(得分:2)

你可以使用:columns()。order()

var table = $('#example').DataTable();

table
    .columns( '.status' )
    .order( 'desc' )
    .draw();

更多信息: http://www.cso.com.au/article/577197/apple-tells-ios-9-developers-use-https-exclusively/

修改

您可以使用dataTables.colReorder.min.js插件实现此目的,您可能希望以任何形式禁用拖放...:

var table = $('#example').DataTable({
    colReorder: true
});

table.on('click', 'td:first-of-type', function() {
  var values = [];
  var row = $(this).parent();

  row.children('td').each(function(i){
    values.push($(this).text());
  });

  var colOrder = values.sortIndices;
  table.colReorder.order(colOrder);

});

//get index after sort
//credit to this post:
//http://stackoverflow.com/questions/3730510/javascript-sort-array-and-return-an-array-of-indicies-that-indicates-the-positi

function sortWithIndeces(toSort) {
  for (var i = 0; i < toSort.length; i++) {
    toSort[i] = [toSort[i], i];
  }
  toSort.sort(function(a, b) {
    return a[0] - b[0]
  });
  toSort.sortIndices = [];
  for (var j = 0; j < toSort.length; j++) {
    toSort.sortIndices.push(toSort[j][1]);
    toSort[j] = toSort[j][0];
  }
  return toSort;
}

这里有完整的工作示例: https://datatables.net/reference/api/columns().order()

答案 1 :(得分:0)

感谢Stryder上面的解决方案,没有它我就不会有这么远。

添加,处理列(小数和字符串)中的非整数数据切换排序保持第一列静态(类似于标题列),我已将代码细化为

var currRow = "";
var currOrd = "";

var table = $('#example').DataTable({
  colReorder: true
});

table.on('click', 'td:first-of-type', function() {
  var values = [];
  var row = $(this).parent();
  var valPos = [];
  if (currRow != row.index()) { //if this row has not been sorted yet
    currOrd = "desc";
    currRow = row.index();
  }
  currOrd = (currOrd == "asc") ? "desc" : "asc"; //toggle sorting order
  log("Sort row# " + (currRow + 1) + " in " + currOrd + " order");
  row.children('td').each(function() {
    values.push(this.innerHTML);
    valPos.push(values.length - 1);
  });

  //remove logs :)
  //    log("initial values: " + values);
  log("initial order: " + valPos);
  //    log("sorted values: " + sortWithIndeces(values));
  sortWithIndeces(values)
  log("new column order: " + values.sortIndices);

  var colOrder = values.sortIndices;
  table.colReorder.order(colOrder);

});

//get index after sort
//credit to:
//http://stackoverflow.com/questions/3730510/javascript-sort-array-and-return-an-array-of-indicies-that-indicates-the-positi
//and http://stackoverflow.com/questions/38076749/left-to-right-scrolling-on-datatable/38077921#38077921
function sortWithIndeces(toSort) {
  var firCol = toSort[0];
  for (var i = 1; i < toSort.length; i++) {
    toSort[i] = [toSort[i], i];
  }
  toSort.sort(function(a, b) {
    if ((a != firCol) && (b != firCol)) {
      if (currOrd == "asc") {
        if (($.isNumeric(a[0])) && ($.isNumeric(a[0]))) {
          return a[0] - b[0];
        } else {
          return ((a[0] > b[0]) ? 1 : ((a[0] < b[0]) ? -1 : 0));
        }
      } else //descending order
      {
        if (($.isNumeric(a[0])) && ($.isNumeric(a[0]))) {
          return b[0] - a[0];
        } else {
          return ((b[0] > a[0]) ? 1 : ((b[0] < a[0]) ? -1 : 0));
        }
      }
    } else
      return 0;
  });
  toSort.sortIndices = [0];
  for (var j = 1; j < toSort.length; j++) {
    toSort.sortIndices.push(toSort[j][1]);
    toSort[j] = toSort[j][0];
  }
  return toSort;
}

//throwaway :)
function log(s) {
  $('.console').append(s + "<br/>");
}

jsFiddle:https://jsfiddle.net/rsreeram84/tnozsp5s/