Sort by doc_nr + year. how to

Sort by doc_nr + year. how to

itajackassitajackass Posts: 155Questions: 47Answers: 3
edited February 2020 in Free community support

I don't know how to implement a simple way to sort a column that show me number of doc with trailing year like:

COL 1    COL 2     COL 3    DOC. NR
------------------------------------
  x        x          x       2/2020
------------------------------------
  x        x          x       3/2020
------------------------------------
  x        x          x       4/2021
------------------------------------
  x        x          x       1/2022
------------------------------------

In my example i'd like to sort doc. nr col by asc or desc, based on number of doc (grouped by year).

I tried to set data-order with formula: numberdoc + year but it doesn't work cause, for example, 3 + 2020 is equal to 1 + 2022.... so this way is not correct. Any idea?

<td data-order="2020">1/2019</td>

Script:

$(document).ready( function () {
  var table = $('#example').DataTable({
     "aaSorting": [[ 1, "desc" ]],
    "columnDefs": [
            {
                targets: [1],
                data: {
                    _: "1.display",
                    sort: "1.@data-order",
                    type: "1.@data-order"
                }
            }
        ]
  });
} );

My fiddle: http://live.datatables.net/jivekefo/1/edit

Expected: (ASC)

1/2018
2/2018
3/2018
1/2019
2/2019
3/2019
4/2019
1/2020
...

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    You would need to create your own sort-plugin for that. There's several here that you could use as a template.

    Colin

  • itajackassitajackass Posts: 155Questions: 47Answers: 3

    mh i'm not an expert on create plugin.
    I solved with this rule:

    <td data-order="<?php echo (new Datetime($dateDoc))->format("Y") . str_pad(ltrim($numberDoc, "0"), 5, "0", STR_PAD_LEFT); /* es. 201900001*/ ?>">
    
This discussion has been closed.