Excluding Words from Filter Processing

Excluding Words from Filter Processing

MDRMDR Posts: 2Questions: 1Answers: 0

A bit surprised its taking so long to find this as it seems common. Here it goes...

So I have data in a table that looks like....

1.72 miles
8.56 miles
12.34 miles

etc.

Problem is the filter treats it like a string when it includes miles (counts the decimal), so its not ordering numericly. So I'm either trying to find a way to remove " miles" before the sorting filter tries to process it, or I'm also fine with having the data initially just be numbers and then add miles after processing. In the end I want the user to still see miles, just the sorter not to take it into account.

I came across the render option, but anything there that gets displayed also ends up in the filter.

Any help would be greatly appreciated.

This question has an accepted answers - jump to answer

Answers

  • HPBHPB Posts: 73Questions: 2Answers: 18
    Answer ✓

    columns.render is exactly what you need for this.
    The function will also give the type, and you can use that to only add miles for display purposes.

    render: function ( data, type, row, meta ) {
       if(type === 'display')
          return data + ' miles';
       return data;
    }
    
  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Yup - orthogonal data is great for this sort of thing.

    The other option is to use a sorting plug-in like this one which will strip everything but numeric data.

    Allan

  • MDRMDR Posts: 2Questions: 1Answers: 0

    Thanks that worked. Here is the final code for anyone looking. In this case the column in question is the 6th one.

    $('#datatable').dataTable({
                                    aoColumns: [
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        { "bSortable": false }
                                    ],
                                    "columnDefs": [ {
                                        "targets": 5,
                                        "data": "distance",
                                        "render": function ( data, type, row, meta ) {
                                          if(type === 'display'){
                                              return data + ' miles';
                                              }
                                           return data;
                                        }
                                      } ]
                                });
    
This discussion has been closed.