Can't get numbers sorted properly

Can't get numbers sorted properly

waolwaol Posts: 37Questions: 0Answers: 0
edited August 2009 in General
Numbers will not sort properly but considering the first chiper only.

Tried also to specify the column type as below but no difference:
The escape characters are needed in my conf.

$(document).ready(function() {
$('#example').dataTable( {\"aaSorting\": [[ 0, \"asc\" ]],
\"aoColumns\": [
{ \"sType\": \"html\" },
{ \"sClass\": \"center\"},
{ \"sClass\": \"left\"},
{ \"sClass\": \"left\"},
{ \"sClass\": \"left\"},
{ \"sType\": \"numeric\", \"sClass\": \"center\"},
{ \"sType\": \"numeric\", \"sClass\": \"center\"},
{ \"sType\": \"numeric\", \"sClass\": \"center\"},
],
\"bPaginate\": true,
\"bLengthChange\": true,
\"bFilter\": true,
\"bSort\": true,
\"bInfo\": false,
\"bAutoWidth\": false } );
} );

I've also checked that there ais nothing else but number in those columns and this seems to be the case, see from code:



00228507
AAAAA
NI-400
CPU Usage too different between devices handling the same traffic
Await Eng
3 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<
79 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<
24 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<





00228807
BBBBBB
SI-GT-EGX2
"HW buf double free" messages
Aw Bug Fix
3 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<
109 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<
19 <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<

Replies

  • fliesflies Posts: 35Questions: 0Answers: 0
    If i see correctly you have tbody in every row... It should be only before first row with data, and closing after the last row...
  • waolwaol Posts: 37Questions: 0Answers: 0
    Yes Flies.

    I have just removed it. Have to learn to look at the HTML file first.

    It was there because I was looping that too in the "while".

    Removed, this issue is now gone. One less.......:)

    Thanks man.
  • waolwaol Posts: 37Questions: 0Answers: 0
    Flies,

    If I wasn't clear the issue I have solved is the one where the pagination wasn't working.

    This post is for sorting. Do you see anything obvious in the code I pasted?

    Also now that the tbody is fixed I can't sort the 3 numbers columns properly.

    Thanks in advance.
    Salvo
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Good stuff - and for the remaining question, there is a plug-in for it :-). The problem you are running into is that the information in that column isn't simply a number, but your also for HTML data in there - hence DataTables is sorting it as a string.

    Therefore you need to use a different sorting function, and this one is perfect for it: http://datatables.net/plug-ins#sorting_numbers_html . You'll need to set the sType for that column, and that should be that!

    Regards,
    Allan
  • waolwaol Posts: 37Questions: 0Answers: 0
    Hi Allan,

    I've tested and can confirm that removing all the html tags the numbers will be actually sorted properly.

    I'm not quite sure where the plugins has to be inserted in the code.
    Into the function? before?
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Take a peak at the sorting plug-in example: http://datatables.net/examples/example_sorting_plugin.html

    Anywhere before you initialise DataTables, but after you import DataTables itself.

    Allan
  • waolwaol Posts: 37Questions: 0Answers: 0
    Hi Allan,

    No luck at all. Tried tons of times, also 1.4 and 1.5
    I think I'm missing something. Here my code, would you mind have a look?

    root@ubuntu:/var/www/salvo/dataTables-1.5# more global.html
    <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>




    DataTables example

    @import "media/css/demo_table.css";
    @import "media/css/demo_page.css";






    jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };


    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumns": [
    { "sType": "html" },
    null,
    null,
    null,
    null,
    { "sType": "html" },
    { "sType": "html" },
    null,
    ],
    "bStateSave": false,
    "bProcessing": true,
    "iDisplayLength": 25,
    "sPaginationType": "full_numbers"
    } );
    } );
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Okay, two things,

    1. While you have included the plug-in for the type 'num-html', none of your columns are actually using it! You need to give an sType of 'num-html' for the appropriate column.

    2. (This won't make any difference to this specific problem, but it's good practice), you are including both jquery.datatables.js and jquery.datatables.min.js - they do the same thing - you only need one of them (ideally the min file of course).

    Allan
  • waolwaol Posts: 37Questions: 0Answers: 0
    1, Now works like a charm. Thanks

    2, Corrected. Thanks.

    Still have to deal wit the colors, but haven;t had time to go through your suggestions in other thread. will let you know.

    Allan,

    the way I'm now creating the html pages is with a script that reloads the php page every minute in background (quite silly I know but I needed just one page till today).
    I understand there is a big limit here, as for every different page I want I should have something in background reloading the php page to refresh the html one.
    Ideally I'd like to call the html page and create everything on fly, I understand this is possible with the server_side. Before I start I'd like to know if this is going to work with MS-SQL. I see all the examples are with mysql.

    Anyone else has a working example?

    Thanks Again,
    Salvo
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Hi Salvo,

    What exactly is it that you are looking to do with the reload? Do you just want to refresh the whole table, or just the specific view.

    If you want to reload the whole table, and there aren't too many records (say less than 500), then using an Ajax source ( http://datatables.net/examples/example_ajax_source.html ) and the fnReloadAjax() plug-in could be perfect for you. Otherwise then the server-side processing might be better.

    I think someone has ported it to MSSQL, but I can't find any evidence to back this up... Either way, it shouldn't be too difficult to do. Perhaps just a few changes. Probably best to ask in a MSSQL forum.

    Allan
  • waolwaol Posts: 37Questions: 0Answers: 0
    Hi Allan,

    Thanks for your feedback.

    Let me explain better: I need to reload (read recreate) the html page by reading content in the database as it changes whenever a user does something. so I guess I need the server-side as it's a proper recreation of the table with possible different content.

    I'll see if I can figure out something other (probably) will start a new thread.



    Thanks,
    salvo
This discussion has been closed.