3

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>
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
N.Zukowski
  • 600
  • 1
  • 12
  • 31
  • please show exactly how you render the table. – davidkonrad Jun 18 '16 at 05:52
  • 1
    @davidkonrad I updated my question. – N.Zukowski Jun 18 '16 at 12:44
  • @davidkonrad Would you be so kind and look at my another datatables question, since you probably know this library very well? Here is the link: http://stackoverflow.com/questions/38032004/angular-datatables-searching-for-a-specific-html-class-value – N.Zukowski Jun 25 '16 at 20:14

2 Answers2

2

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

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
0

Add an orderBy filter:

  <tr ng-repeat="entry in entries orderBy : 'failed'  "> 
    <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>
Mike
  • 1,645
  • 3
  • 13
  • 21
  • `orderBy` along with dataTables is not a good idea. At best you will end up in rows visually rendered different than the underlying internal row structure. It would be the correct answer on a DOM table without dataTables. – davidkonrad Jun 18 '16 at 13:49