[SOLVED] Searchable Columns

[SOLVED] Searchable Columns

organicspiderorganicspider Posts: 12Questions: 0Answers: 0
edited April 2011 in General
Making great headway with DataTables and its awesome :D

I have generated a table that includes two telephone numbers; inbound and outbound. Based on a separate column I decide which number to be show[code]var sDirectionClass; var sSrc = aData[3]; var sDst = aData[4];
if ( aData[1] == "0" ) {
sDirectionClass = "inbound"; $('td:eq(2)', nRow).html( sSrc );
$('td:eq(1)', nRow).html( '' );
} else {
sDirectionClass = "outbound"; $('td:eq(2)', nRow).html( sDst );
$('td:eq(1)', nRow).html( '' );
}[/code] Is it possible to exclude the sSrc and sDst columns from the search but allow the resulting column to be included. The rationale is that if you search for a specific number it could be the source when what is being displayed is the destination; confusing :)

Replies

  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Agreed - showing data when there isn't anything visibly matching can be a bit confusing! I presume that the code you have above is in an fnRowCallback? The advantage of fnRowCallback is that it runs on every row for the displayed rows - the disadvantage is that it's just rendered information, it's not actually in the DataTables data store, and thus not part of the filtering.

    So what I would suggest is a little change of plan and to use fnRender instead, which will allow your data to be filtered (you can remove the two source columns from the filter using bSearchable). Now the disadvantage in this case is the fnRender will run only once during the life time of a row (at creation), unless the row is updated. So if you have something which is toggled by the user to select which is being shown, then you'll need to update each row, forcing the render again. If however the decision for what to display is made before the table is initialised and won't change, then fnRender is most certainly the easiest way to go.

    Please shout if anything needs clarified - hope this helps!

    Regards,
    Allan
  • organicspiderorganicspider Posts: 12Questions: 0Answers: 0
    edited April 2011
    Thanks allan,

    I must be doing something wrong as it still does not work for me :( I have moved the code from fnRowCallback into fnRender[code]"aoColumnDefs": [ { "sClass": "center", "aTargets": [ 0, 1, 2, 3, 4, 5, 6, 7] } ],
    "aoColumns": [
    null,
    { "bSearchable": false },
    { "fnRender": function ( oObj ) { if ( oObj.aData[1] == "0" ) { return oObj.aData[3]; } else { return oObj.aData[4]; } } },
    { "bVisible": false, "bSearchable": false, },
    { "bVisible": false, "bSearchable": false },
    null,
    null,
    null
    ],[/code]Any ideas ?
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hmmm - the code looks okay to me, as far as I can tell. At least there is nothing obviously wrong. What part exactly isn't working? Does your third column get the intended data and the filter doesn't work, or is it something else?

    Allan
  • organicspiderorganicspider Posts: 12Questions: 0Answers: 0
    Hi Allan,

    Yep the third column is being populated correctly. An example of what I am seeing is say with the following data:

    src dst Number (3rd column)
    --- ---- -------------------------
    01234 789 01234
    02345 456 456
    01234 234 01234

    If I enter 789 in the search field it filters the first record; which is confusing to the user as they only see 01234 on the screen.
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    I see. I presume that the order of the src and dst in your above example is not the same as the actual table (otherwise the aoColumns would be slightly incorrect). Can you possibly give me a link to your example? It would be very useful to be able to check that DataTables is adhering to the bSearchable: false (I'd be very surprised if it isn't!). Also you've got a trailing comma in "{ "bVisible": false, "bSearchable": false, }," which IE won't like.

    Allan
  • organicspiderorganicspider Posts: 12Questions: 0Answers: 0
    Yep, I removed that comma. Let me update my DNS to make the URL available as its internal with sensitive data.
This discussion has been closed.