Sorting Not working when a new row is added?

Sorting Not working when a new row is added?

chobo2chobo2 Posts: 23Questions: 2Answers: 0
edited July 2010 in General
Hi

I have a datatable that has sorting enabled.

It seems though if I try to delete a row then add the new version of that row. Or just try to update that row no sorting will be done on this new/updated row. It is like it is not part of the sort anymore. If I click on the column that row will either be the first or last row. It is like it does not even try to sort it.

var position = dbTable.fnGetPosition(row);
dbTable.fnUpdate([my columns], position);

So I get the position and then do an update. The data gets updated an a success msg is returned yet when I try to sort it does not sort.

It seems to happen with the add method too. Is it something to do with my dates?

I have 2 columns that use a hidden column to sort. I am using iDataSort and the dates look like this

07/19/2010 4:04 PM
07/16/2010 4:04 PM
07/18/2010 2:01 PM
07/22/2010 4:04 PM
07/23/2010 4:04 PM
07/30/2010 4:04 PM

So the top one(19th) is the one I added it add's it on the top. If I hit the sort column my hidden column should be sorted and it seems to be using that column

07/30/2010 4:04 PM
07/23/2010 4:04 PM
07/22/2010 4:04 PM
07/18/2010 2:01 PM
07/16/2010 4:04 PM
07/19/2010 4:04 PM

That is the result. See how the 19th just goes right to the bottom. If I refresh the page then it is all sorted in correct order

07/16/2010 4:04 PM
07/18/2010 2:01 PM
07/19/2010 4:04 PM
07/22/2010 4:04 PM
07/23/2010 4:04 PM
07/30/2010 4:04 PM

Replies

  • hans_jameshans_james Posts: 10Questions: 0Answers: 0
    Hi,

    I have the same Problem, it work not correct after new row. Can someone help us.

    Hans
  • chobo2chobo2 Posts: 23Questions: 2Answers: 0
    @Hans_james

    Is it with dates too? I found out that because I was html encoding the dates this was the problem

    http://datatables.net/forums/comments.php?DiscussionID=2310&page=1#Item_1
  • hans_jameshans_james Posts: 10Questions: 0Answers: 0
    HI chobo2,

    yes with dates. Thx

    Hans
  • dahepedahepe Posts: 7Questions: 0Answers: 0
    Hi,

    do you have bUseRendered enabled on the datatable. From my point of view it seems DataTables uses the raw column value as added using fnAddData for sorting an not the typed value as one would expect when specifying sType as date. That is, if you add dates to your table that is formatted like 'October 13, 1975 11:13:00' and afterwards add rows using f.e. unix like time (milliseconds since 1970-01-01), sorting messes up. I solved this by ensuring all data is added to the table in the same format. And provided a renderer for user friendly date formatting.

    Regards.
  • hans_jameshans_james Posts: 10Questions: 0Answers: 0
    edited July 2010
    Hi,

    my Datatable look like:

    test



    On Start display is everything fine. The Datatable sort it Correct. After update it sort the first colum right, but the Date-TD not Correct.

    if u have a idia how i can solve this problem.

    If u have example Code??

    Best Regards

    Hans
  • dahepedahepe Posts: 7Questions: 0Answers: 0
    Perhaps you could post your complete page, including the used javascript code?!

    Is there any reason to use the hidden input field instead of using the bVisible column flag of DataTables?
  • hans_jameshans_james Posts: 10Questions: 0Answers: 0
    Hi dahepe,

    here my html code:

    [code]
    Live example
    Click to add a new row



    Column 1
    Column 2
    Column 3
    Column 4




    100
    1.2
    1.3



    200
    1.2
    1.3



    300
    1.2
    1.3



    100
    1.2
    1.3




    [/code]

    JavaScript Code:
    [code]
    var oTable;

    /* Global var for counter */
    var giCount = 2;

    $(document).ready(function()
    {
    oTable = $('#example').dataTable(
    {
    "iDisplayLength": 50,
    "aaSorting": [[ 3, "asc" ], [0,'desc'] ]
    } );
    })

    function fnClickAddRow()
    {
    oTable.fnAddData( [
    4000,
    giCount+".2",
    giCount+".3",
    ""
    ] );

    giCount++;
    }
    [/code]

    On Start display is everything fine. The Datatable sort it Correct. After Add new Row, it sort the first second and third colum right, but last colum not.

    Best Regards

    Hans
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Hans,

    The problem that I think you are having is that the sorting on column 4 is currently being done on the HTML image string (or if you are using the 1.7 betas an empty string since it strips HTML automatically!). I'd say the fact that it works before your do an fnAddData is "lucky" based on the data you have already available, and if you put the data you are adding into the HTML before initialisation, then that would probably be "broken" as well.

    What you need to do is to have a custom sorting function which will sort the strings you are looking at in an appropriate manner. There are a number of plug-ins for this already available, although none which will do exactly what you are looking for - having said that you can cannibalise the plug-ins which are available to do what you want (specifically the hidden title and the date sorting plug-ins): http://datatables.net/plug-ins/sorting

    Allan
This discussion has been closed.