I have to do some custom sorts with Jquery Datatables. I do not want to write custom sort functions for every custom sort.
I want to define a value to sort on and have Datatables ignore the original column values, if value is defined.
For example:
<td data-sort="111123">E 1.111,23</td>
I want Jquery Datatables to sort this column numerically on 111123
.
<td data-sort="19801220">20-12-1980</td>
I want Jquery Datatables to sort this column numerically on 19801220
.
<td>a string</td>
I want Jquery Datatables to sort this column by its original value a string
.
http://www.datatables.net/plug-ins/sorting has "Hidden title numeric sorting" which is close to what I want, but requires me to specify for every datatable on which column this custom sorting applies. I have too many datatables of differing sizes to do this in a reasonable time. I just want to make Datatables always sort this hidden value / data-* attribute if it is present. No need for custom sort definitions on specific columns.
Related: jQuery DataTables: how to sort by custom parameter value rather than the content of the cell? but unfortunately no answer as to how to sort simply by custom parameter, instead pointers to custom sorting scripts.
You can use data-order attr, for example
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Date</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach($users as $user) {?>
<tr>
<td data-order="<?php echo $count ?>">
<?php echo $user['createdDate']; ?>
</td>
<td>
<?php echo $user['count']; ?>
</td>
</tr>
<?php
$count++;
}?>
<tr>
<td data-order="999999999999999999999999999"> <!--always last-->
Total
</td>
<td>
<?php echo count($users); ?>
</td>
</tr>
more information HTML5 data-* attributes