Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular datatables: sort non-textual data

Is there a way to create custom sorting for a column containing non-textual data?

Here is a snapshot:

enter image description here

I would like to sort by the icon symbol.

P.S. The both icons are shown using ng-if and a boolean value from the dataset.

Edit: I'am using the Angular way of displaying data.

<table datatable="ng" dt-options="dtOptionsLoginHistory" dt-column-defs="dtColumnDefsLoginHistory"
               class="table table-striped row-border hover"
               width="100%">
            <thead>
            <tr>
                <th>Success</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="entry in entries">
                <td style="width: 10%;">
                    <i ng-if="!entry.failed" class="fa fa-check" style="color: green;"></i>
                    <i ng-if="entry.failed" class="fa fa-times" style="color: red;"></i>
                </td>
            </tr>
            </tbody>
        </table>
like image 373
N.Zukowski Avatar asked Jun 30 '26 14:06

N.Zukowski


1 Answers

There is several different ways to achieve what you want. Considered your setup I believe the easiest would be to create a special orderType that return a value based on which fa-* classes the <i> elements is rendered with :

$.fn.dataTable.ext.order['fa-classes'] = function(settings, col)  {
  return this.api().column( col, {order:'index'} ).nodes().map(function(td, i) {
    return $('i', td).hasClass('fa-check') ? '1' : '0';
  })
} 

Will give all <i class="fa fa-check"> order 1, any other 0. This could also be a switch { .. } returning multiple different order values. Use it like this :

$scope.dtColumnDefsLoginHistory = [
   DTColumnDefBuilder.newColumnDef(0).withOption('orderDataType', 'fa-classes')
];  

small demo -> http://plnkr.co/edit/8S5f2MR331CiNBYYfDQf?p=preview

like image 96
davidkonrad Avatar answered Jul 07 '26 02:07

davidkonrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!