Hidden table sort

Hidden table sort

dazzafactdazzafact Posts: 1Questions: 0Answers: 0
edited July 2011 in General
Hello, how i can sort a table by hidden fields?
I like to use it for Bible Chapters.
[code]
Gensise 2, 5
Revalation 13,5
[/code]

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited July 2011
    I can think of a few ways you can do this. The easiest is to have a row for those "hidden" values. You can tell datatables to hide any column. (but to sort by it, you may need to use an external button)

    [code]



    number
    Verse




    01.02005
    Genesis 2,5


    66.13005
    Revelations 13,5



    [/code]

    [code]

    // <!--
    $(document).ready( function() {
    $oTable = $('#bible_verses').dataTables({
    aoColumnDefs: [
    // column 0, numeric id, but treat as strings for sorting so the leading 0 remains
    { "aTargets": [ 0 ] , "sType": "string", "bVisible": false}
    ]
    });
    });
    // -->

    [/code]


    another way would be to use the fnRender function so that your table has the verse number string but shows to the user the bible verse string

    [code]



    Verse




    01.02005


    66.13005



    [/code]

    [code]

    // <!--
    $(document).ready( function() {
    $oTable = $('#bible_verses').dataTables({
    aoColumnDefs: [
    // column 0, numeric id, but treat as strings for sorting so the leading 0 remains
    { "aTargets": [ 0 ], "sType": "string", "bUseRendered": false, "fnRender": make_verse_string(oObj) }
    ]
    });
    });

    // convert verse number (like "01.02005") to a verse string (like "Genesis 2,5")
    function make_verse_string(oObj) {
    // ... add code here
    }
    // -->

    [/code]

    by using bUseRendered, the column should sort by the original value (verse number) though the user will still be shown the verse string.
  • extemplextempl Posts: 1Questions: 0Answers: 0
    I'm find the easiest way:
    [code]

    01.02005Genesis 2,5
    66.13005Revelations 13

    [/code]
    Minus of this way is situation when the first values are equal - then table sorting by second value. But for me it's ideal.
  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    As extempl suggests the way to do this is with an inner element - the hidden sorting plug-in makes use of this for example: http://datatables.net/plug-ins/sorting#hidden_title . The reason for this is that the sorting function doesn't have access to the TD element and thus the attribute. While it would be possible to add this in (and it is actually possible already with a pre-sorting callback function ( http://datatables.net/development/sorting#data_source ) it can be a bit faster to do it this way, since DataTables is internally working with just strings for this.

    Allan
This discussion has been closed.