Custom node sorting

Custom node sorting

Vilhelm PerssonVilhelm Persson Posts: 11Questions: 0Answers: 0
edited August 2010 in Plug-ins
Hi
I am evaluating DataTables and think it works really fine.
I am trying to create a custom sort that sort the data based on DOM node information but from what I can see the data that is passed to the oSort["my-sort-asc"] function(a,b) are only strings and not the table td nodes to compare.

Is there a option to get the node data instead of the String data for the compare?

What I am trying to do is to display status images in the table that are dynamically updated in the background when the status change and at the same time an dynamic DOM attribute (theImageNode.custom_sort=42) is updated.
If I can access the TD node data of the cell I will be able to write a recursive function that takes out the dynamic custom sort information.

I have manage to use the aoData object to update images not on the visible page similar as if I used the document.getElementById on the raw table and now I think the sorting is the final part before I can recommended it for the project and make some donation for your great work.

Best regards
Vilhelm Persson

Replies

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Hi Vilhelm,

    Yes indeed it is possible to do live DOM sorting. See this example: http://datatables.net/examples/plug-ins/dom_sort.html . Here is the documentation on it: http://datatables.net/development/sorting#data_source

    Regards,
    Allan
  • Vilhelm PerssonVilhelm Persson Posts: 11Questions: 0Answers: 0
    Hi
    Thanks for the response.
    I did not understand that this sample where what I needed since I thought that the sort data where updated when data where edited and not when the sorting take place.
    Now everything is working (I hope)

    [code]
    $.fn.dataTableExt.afnSortData['sort-key'] = function ( oSettings, iColumn )
    {
    var aData = [];
    $( 'td:eq('+iColumn+') ', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
    var customKey = RecursiveGetCustomKey(this);
    aData.push( (customKey==null?"":customKey) );
    } );
    return aData;
    }


    "aoColumns": [{ "sSortDataType": "sort-key", "sType": "string" },null,null,null,null,null,{ "sType": "image" },null,null]


    function RecursiveGetCustomKey(theNode)
    {
    if (theNode==null) return null;
    var custKey=null;
    try
    {
    if (typeof theNode!="undefined" && theNode != null ) custKey = theNode.getAttribute("sort_customkey");
    }
    catch(e)
    {}
    if (custKey != null) return custKey;
    if (typeof theNode.sort_customkey!="undefined") return theNode.sort_customkey;
    if (theNode.nodeType==1 || theNode.nodeType==11)
    {
    for (var i = 0; i < theNode.childNodes.length; i++)
    {
    custKey = RecursiveGetCustomKey(theNode.childNodes[i]);
    if (custKey != null) return custKey;
    }
    }
    return null;
    }
    [/code]
This discussion has been closed.