Very basic get-started question

Very basic get-started question

col.braziercol.brazier Posts: 26Questions: 2Answers: 0
edited February 2011 in General
Hi, I would love to use datatables but am having trouble getting it to work, right from the get-go. I intend to use it for dynamically-generated tables eventually but to simplify things I have pared my table down to a static html one as per here:

http://www.fobgfc.org/test.php

I cannot get the columns to sort. I have looked at the examples, FAQs and forums with no luck. I have obviously missed a simple basic ingredient. Can anyone help please?

Ta, Col

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi Col,

    What is happening here is that the sorting is being done on the static strings that are present when the table is initialised, rather than reading from the DOM for each sort would would be horribly slow. However, DataTables does provide a live DOM sorting option - have a look at this example which should get you up and running: http://datatables.net/examples/plug-ins/dom_sort.html .

    Regards,
    Allan
  • col.braziercol.brazier Posts: 26Questions: 2Answers: 0
    Allan,

    Many thanks. I am almost there (at least until I get to the dynamic bit), but I'm having trouble sorting the first column (surname). What am I missing here? Same URL.

    http://www.fobgfc.org/test.php

    Ta, Col
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi Col,

    This problem is this:

    [code]


    [/code]
    and this:

    [code]
    $( 'td:eq('+iColumn+') input',...
    [/code]
    The dom-text function is taking the values from both of your input elements.

    Changing the dom-text function to this:

    [code]
    $.fn.dataTableExt.afnSortData['dom-text'] = function ( oSettings, iColumn )
    {
    var aData = [];
    $( 'td:eq('+iColumn+') input[type=text]', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
    aData.push( this.value );
    } );
    return aData;
    }
    [/code]
    Fixes the problem :-)

    Regards,
    Allan
  • col.braziercol.brazier Posts: 26Questions: 2Answers: 0
    Perfect! Thank you.
This discussion has been closed.