【问题标题】:Left-to-right scrolling on Datatable在 Datatable 上从左到右滚动
【发布时间】:2016-10-30 19:30:28
【问题描述】:

我遇到需要使用 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,但是如何让它与数据表一起使用?

【问题讨论】:

  • 是的,有办法做到这一点。但是您尝试过什么?
  • 没什么 - 我在 Datatables 文档中找不到任何方法。还是我错过了什么?
  • 我找到了这个用于水平排序的示例stackoverflow.com/questions/3127503/…,但是如何让它与数据表一起使用?

标签: jquery datatables row columnsorting


【解决方案1】:

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

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

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

更多信息: https://datatables.net/reference/api/columns().order()

编辑

您可以通过使用 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://jsfiddle.net/qjp8Lnam/6/

【讨论】:

  • 这有什么不同?我试过了,它仍然根据列值进行自上而下的排序。我需要根据行值进行左右排序。
  • 抱歉,我花了一段时间才回复您 - 感冒了。太感谢了!这是做我想做的事的一种非常清晰的方式!我正在尝试自己找到解决方案,但只是想知道您是否已经有一个解决方案:如何将第一列与排序隔离? :)
  • 此外,如果任何列包含文本,则排序失败。我怀疑它与 sortIndices 函数有关,但我不知道下一步该做什么。 (jsFiddle 更新)
  • 为以下内容分叉了 jsFiddle:静态第一列、切换排序和处理字符串/十进制/整数比较。我已经接受了 Stryder 的解决方案,因为它让我走到了这一步,而修改后的 jsFiddle 只是对它的修改。见jsfiddle.net/rsreeram84/tnozsp5s
【解决方案2】:

感谢 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多