1

I am following this DataTables example to add sorting on columns with an input field: http://datatables.net/examples/plug-ins/dom_sort.html

In my code below, the second targeted column (6), is a numeric field, and sorts fine, however the first column (5), a text column, doesn't sort at all. Using Chrome developer tools I can see it stepping into the function, but no sorting takes place. Both columns are input fields. I am using the latest DataTables version, 1.10.7.

$.fn.dataTable.ext.order['dom-text'] = function  ( settings, col )
{
   return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
      return $('input', td).val();
   } );
}


 var table = $("#example").DataTable({
   "scrollY": "500px",
   "scrollX": "675px",
   "scrollCollapse": true,
   "paging": false,
   "order": [],
   "deferRender": true,
   "orderClasses": false,
   "columnDefs": [
     { "orderDataType": "dom-text", "targets": [5,6] }
    ]
 });
2
  • Could you label your question with JQuery if in fact you are using this? Commented Jul 21, 2015 at 7:54
  • 1
    Notice in the example there is also type: 'string' option, which you're missing. Commented Jul 21, 2015 at 12:12

1 Answer 1

2

SOLUTION

As in the Live DOM Ordering example, you need to use dom-text for sorting <input> elements containing text and dom-text-numeric for sorting <input> elements containing numbers.

/* Create an array with the values of all the input boxes in a column */
$.fn.dataTable.ext.order['dom-text'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).val();
    } );
}

/* Create an array with the values of all the input boxes in a column, parsed as numbers */
$.fn.dataTable.ext.order['dom-text-numeric'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).val() * 1;
    } );
}

$(document).ready(function (){
    $('#example').dataTable( {
        /* ... other options ... */
        "columnDefs": [
            { "targets": 1, "orderDataType": "dom-text-numeric" },
            { "targets": 2, "orderDataType": "dom-text", "type": "string" }
        ]
    } );
});

DEMO

See this jsFiddle for code and demonstration.

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

1 Comment

ah, ok, I was missing the "type": "string" - thx. Will this method work with multi-column sorting?

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.