简体   繁体   English

DataTable按具有不同数据类型的两列排序

[英]DataTables sort by two columns with different data types

There is a table with statistics containing two columns. 有一个包含两列的统计信息的表。 The first column is - name (string) and the second one is number of actions (number) How can I make sure that the second column is the first one to be sorted? 第一列是-名称(字符串),第二列是操作数(数字)如何确定第二列是要排序的第一列? Given that the values have same names and also they are sorted alphabetically? 假设值具有相同的名称,并且它们也按字母顺序排序?

Here is my code: 这是我的代码:

$('#clickouts-table').DataTable({
  // eslint-disable-line new-cap
  processing: false,
  stateSave: true,
  responsive: true,
  iDisplayLength: 25,
  order: [[1, 'desc']],
  columnDefs: [{
    targets: [0],
    orderData: [1, 1]
  }, {
    targets: [1],
    orderData: [1, 1]
  }],
  sDom: `<'row top-row'<'col-sm-2 col-xs-2 col-xxs-12'f><'col-sm-2 col-xs-2 col-xxs-12'l>
        <'col-sm-8 col-xs-8 col-xxs-12 custom-button'>>
        <'row'<'col-sm-12'tr>>
        <'row'<'col-sm-12'<'table-bottom-toolbar clearfix'ip>>>`,
  columns: [
    { data: 'userName', name: 'userName' },
    { data: 'count', name: 'count' }
  ],
  data: data
});

Thanks! 谢谢!

I'm not exactly sure what your asking, but you can make column 0 sort as a String and 1 as a number like this: 我不确定您要问的是什么,但是您可以将列0排序为字符串,将列1排序为数字,如下所示:

columnDefs: [{
    targets: [0],
    orderData: [1, 0],
    type: "html"
  }, {
    targets: [1],
    orderData: [1, 0],
    type: "num"
  }],

See: https://datatables.net/reference/option/columns.type 参见: https : //datatables.net/reference/option/columns.type

Also, in case the column 1 numbers match, you should define column 0 as the secondary sorting option: 另外,如果第1列的数字匹配,则应将第0列定义为辅助排序选项:

order: [[1, 'desc'], [0, 'asc']]
...
orderData: [1, 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM