1

I have string Data in my ProviderQualificationTime field.So it doesn't sort this column properly since the data is a string. How can I make the datatable to treat it as Int Column and sort according to that? Possible? Sample Data in ProviderQualificationTime field:

"1","10","11","NotAccpeted"

Code:

$('#tblProviders').dataTable({
                            "autoWidth": false,
                            "searching": false,
                            "pageLength": 6,
                            "lengthMenu": [6, 10, 25, 50, 75, 100],
                            "bDestroy": true,
                            data: obj.data.ProviderQualificationDetails,
                            columns: [
                                       { "data": "ProviderName" },
                                       { "data": "ProviderQualificationTime" },
                                       { "data": "TotalServiceableOffers" }
                            ]
                        });
4
  • data : obj.data.ProviderQualificationDetails.map(Number), Commented Nov 16, 2016 at 13:46
  • @adeneo will it not convert the whole ProviderQualificationDetails object into number? I just want ProviderQualificationTime to be of type number. Commented Nov 16, 2016 at 13:49
  • Then I guess you'd have to figure out how to map it and convert only the index you want to a number Commented Nov 16, 2016 at 13:51
  • Also ProviderQualificationDetails is a array Commented Nov 16, 2016 at 13:52

5 Answers 5

1

I would use a columnDef object to specify a render function like so:

$('#tblProviders').dataTable({
 "autoWidth": false,
 "searching": false,
 "pageLength": 6,
 "lengthMenu": [6, 10, 25, 50, 75, 100],
 "bDestroy": true,
 data: obj.data.ProviderQualificationDetails,
 columns: [
   { "data": "ProviderName" },
   { "data": "ProviderQualificationTime" },
   { "data": "TotalServiceableOffers" }
 ],
 "columnDefs": [ {
    "targets": 1,
    "render": function ( data, type, full, meta ) {
       // If this is a display render request ...
       if(type === 'display') {
         // don't modify it.
         return data;
       }

       // Either convert it to a number if it is a numeric value or set it to 0
       // This will return a numeric value for sorting and filtering.
       return (isNaN(data)) ? 0 : +data;
    }
 }]
});

So with the custom renderer for your column you can have a unmodified display value and a converted sort/filter value.

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

Comments

0

With HTML5 data attributes you can specify data-order. This will allow the datatable to order the content differently from the data that is actually displayed:
https://datatables.net/examples/advanced_init/html5-data-attributes.html

For example:

<td data-order='0'>NotAccepted</td>

Comments

0

This is ugly but works:

$(document).ready(function() {
  jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "numNonStandard-asc": function (a, b) {
      var x = isNaN(parseInt(a, 10)) ? 0 : parseInt(a, 10);
      var y = isNaN(parseInt(b, 10)) ? 0 : parseInt(b, 10);
      return (x == y) ? 0 : (x < y) ? 1 : -1;
    },
    "numNonStandard-desc": function (a, b) {
      var x = isNaN(parseInt(a, 10)) ? 0 : parseInt(a, 10);
      var y = isNaN(parseInt(b, 10)) ? 0 : parseInt(b, 10);
      return (x == y) ? 0 : (x < y) ? -1 : 1;
    }
  });
  $('.table-data').DataTable({
    columnDefs: [
      { type: 'numNonStandard', targets: -1 }
    ]
  });
});

Working example.

Comments

0

Just add the given below parameter:

"sType": "string"

For more details visit the link [https://datatables.net/forums/discussion/24284/column-sort-wrong-data-always-string]

Comments

0

Please refer https://datatables.net/plug-ins/sorting/signed-num

and in first IF condition that is return (a=="-" || a==="") ? 0 : a.replace('+','')*1; add one more or condition like (a == "-" || a === "" || a == "NotAccpeted") ? 0 : a.replace('+', '') * 1;

Comments

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.