Converting DataTables column from string to float

Converting DataTables column from string to float

m127418927m127418927 Posts: 3Questions: 1Answers: 0

Link to test case: https://jsfiddle.net/j3210938120/1tcw0bqd/6/
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: Hi, I'm having a problem converting a column from string to float for sorting purposes. My idea was to use columnDefs and then set "type" to "num", "html-num" but it doesn't work, setting the type to "html" works but the column is still sorted as a string not as a float. Another idea I had was to use parseFloat on the data variable in the return of the render function, but it doesn't work as well. Please note the example I linked is just to get a general idea of the problem and will not work if you run the code. Is there a simple way of making this work?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Its hard to give a specific answer without seeing a running example with a sample of your data. Using the -option columns.type likely won't work as Datatables has builtin type detection.

    Depending on what the issue is you might need to use Orthogonal data to parse the cell data into a float for the sort and type operations.

    If you still need help please provide a running test case with a sample of your data.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • m127418927m127418927 Posts: 3Questions: 1Answers: 0

    This is the best I can do in terms of reproducing the data and code: https://jsfiddle.net/j3210938120/1tcw0bqd/27/ . I can't quite reproduce the error here, as the sorting works ok in this example. But that's not the case in my code. How would I parse the cell data into a float for the sort and type operations in this code example?

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    Why are you returning the data in a div?

                    "render": function ( data, type, row, meta ) {
    
                      return "<div>"+data+"</div>"
                      //return "<div>"+parseFloat(data)+"</div>"
                    }
    

    Normally you wouldn't need to do this. If all your data is numeric data like the test case it should just work.

    How would I parse the cell data into a float for the sort and type operations in this code example?

    Maybe this example will give you an idea. Although with the numeric data you have the result is the same as the default without using columns.render.
    https://jsfiddle.net/4psx08k3/

    I can't quite reproduce the error here, as the sorting works ok in this example.

    If you are using ajax data then use the browsers network inspector to get a sampling of the data from the JSON response to use for the test case.

    Again we will need to see the problem to help diagnose and offer suggestions. If you have other characters in the cell data then you might need to remove them to leave just the numeric value. If you have something other than numeric data in the column it will be typed as a string column and the data sorted as strings. If you are using server side processing then the sorting is handled at the server so you might need to adjust your query string.

    Kevin

  • m127418927m127418927 Posts: 3Questions: 1Answers: 0

    It seems like the example you provided that parses the cell data works. Thank you!

Sign In or Register to comment.