简体   繁体   English

基于data-attribute的订单表列

[英]Order table columns based on data-attribute

I have a table with 14 columns, I'll put just 4 of them for example purposes. 我有一个包含14列的表格,我只将它们中的4个用于示例目的。 All the columns (th's and td's) have a data-order attribute which determines the position I want them to be, like so: 所有列(th和td)都有一个数据顺序属性,用于确定我希望它们的位置,如下所示:

The original (dynamically generated) table looks like this: 原始(动态生成)表如下所示:

<table class="table" role="grid">
    <thead>
        <tr>
            <th data-order="0">Date</th>
            <th data-order="3">Clicks</th>
            <th data-order="1">Delivered</th>
            <th data-order="2">Opens</th>
        </tr>
    </thead>
    <tbody>
        <tr role="row">
            <td data-order="0">...</td>
            <td data-order="3">...</td>
            <td data-order="1">...</td>
            <td data-order="2">...</td>
        </tr>
        <tr role="row">
            <td data-order="0">...</td>
            <td data-order="3">...</td>
            <td data-order="1">...</td>
            <td data-order="2">...</td>
        </tr>
    </tfoot>
</table>

And the order I want them to be (based on the data-order attribute) is: 我希望它们的顺序(基于数据顺序属性)是:

<table class="table" role="grid">
    <thead>
        <tr>
            <th data-order="0">Date</th>
            <th data-order="1">Delivered</th>
            <th data-order="2">Opens</th>
            <th data-order="3">Clicks</th>
        </tr>
    </thead>
    <tbody>
        <tr role="row">
            <td data-order="0">...</td>
            <td data-order="1">...</td>
            <td data-order="2">...</td>
            <td data-order="3">...</td>
        </tr>
        <tr role="row">
            <td data-order="0">...</td>
            <td data-order="1">...</td>
            <td data-order="2">...</td>
            <td data-order="3">...</td>
        </tr>
    </tfoot>
</table>

I'm using the DataTables jQuery Plugin and I saw the column().order() functions and the colReorder property, but it doesn't work for me. 我正在使用DataTables jQuery插件 ,我看到了column()。order()函数和colReorder属性,但它对我不起作用。 My JavaScript looks like this (just to test that the colReorder is working): 我的JavaScript看起来像这样(只是为了测试colReorder是否正常工作):

var table = $('table').DataTable( {
    paginate: false,
    info : false,
    colReorder: {
        order: [ 0, 2, 3, 1 ]
    }
});

I also include the colReorder plugin 我还包括colReorder插件

But that keeps the original column order. 但这保留了原始列顺序。 What do I'm doing wrong? 我做错了什么? Is that even possible? 这甚至可能吗?

Very easy. 很容易。 Grab the data-order values from each <th> and build the desired order array out of that. 从每个<th>获取data-order值,然后构建所需的order数组。 You can even separate it into a function called by the order property : 您甚至可以将它分隔为order属性调用的函数:

//build array, order[data-order] = index
function getOrder() {
   var ths = $('.table th');
   var order = new Array(ths.length);
   for (var i=0, l=ths.length; i<l; i++) {
     order[$(ths[i]).data('order')] = i
   }
   return order;
}

var table = $('.table').DataTable({
   colReorder: {
     order: getOrder()
   }
})

demo -> http://jsfiddle.net/Lwdgnx1x/ 演示 - > http://jsfiddle.net/Lwdgnx1x/

Note : data-order only makes sense in the header. 注意data-order仅在标题中有意义。 You cannot have different ordering on row level anyway. 无论如何,你不能在行级别上有不同的排序。

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

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