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>
3
  • please show exactly how you render the table. Commented Jun 18, 2016 at 5:52
  • 1
    @davidkonrad I updated my question. Commented Jun 18, 2016 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: stackoverflow.com/questions/38032004/… Commented Jun 25, 2016 at 20:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.