conditional format

conditional format

sifuhallsifuhall Posts: 20Questions: 0Answers: 0
edited October 2009 in General
I've searched for the answer to this so I apologize if I have overlooked it.

Is is possible to have a conditional class to a row?

For example, if the value of a field is true make the row appear red?

Replies

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Hi sifuhall,

    Yes this is possible - and would be achieved in exactly the same way as you would do it with a static table (with this possible exception of using fnGetNodes() ). When you initialise the table, you can scan over it and assign the required classes, or if you are adding the data dynamically you can check the classes then, if using server-side data you could use fnDrawCallback() - there are plenty of ways to do it :-). Picking one depends on your exact scenario.

    Regards,
    Allan
  • sifuhallsifuhall Posts: 20Questions: 0Answers: 0
    Thanks, as always for the replies.

    Here is my scenario.
    [code]
    oTable = $('#equipment').dataTable( {
    "bProcessing": true,
    "sAjaxSource": '/ajax.php?action=get_myequipment',
    //"bLengthChange": false,
    "aoColumns": [ { "bSortable": false, "bSearchable": false},null,null,null,null,null,null, { "bSortable": false }]
    } )
    [/code]
    and the html is
    [code]




    +
    Worn
    Item
    Type
    Level
    Quality
    Bonuses
    $







    [/code]

    I'm wanting to apply a conditional class to any row where the field Worn == 1. I've tried fnDrawCallback() but I don't think I'm fully understanding that.

    Is that the best method in this case or should I try a different route?
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Hi sifuhall,

    I think the best option is probably fnInitComplete() which will fire when DataTables has loaded the data from the server and done it's initial display. At that point you can use fnGetNodes() and run through them to add the conditional classes as required.

    Regards,
    Allan
  • lucasnplucasnp Posts: 18Questions: 0Answers: 0
    Allan,

    I know the topic is quite old and I might overlooked something while searching forum. But I could not find a solution for this issue I am having (I am totally new to jQuery so please kind to me :D ):

    I want to remove the odd/even format on each row (because I must have them preformatted differently). But I couldn't get it running with fnGetNodes() inside fnInitComplete()

    [code]
    $(document).ready(function() {
    var oTable = $('#MainGrid').dataTable({
    "fnInitComplete": function() {
    var nNodes = oTable.fnGetNodes();
    $(nNodes).each(function(){
    $(this).removeClass('odd');
    $(this).removeClass('even');
    });
    }
    });
    });
    [/code]

    if the code that remove Class stay outside the initiation, it only applies to the first page.

    Again, I am sorry im new to jQuery and I know there must be a simple solution but I just could not find it
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    To remove the odd/even classes that DataTables adds by default you can set asStripClasses ( http://datatables.net/usage/options#asStripClasses ) to be an empty array.

    Allan
  • lucasnplucasnp Posts: 18Questions: 0Answers: 0
    Thank you Allan, works well. :) Still I am curious, as you mentioned in your post, how you use fnGetNodes() inside fnInitComplete() since the oTable variable is not available at the time?
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    That's an old post (2009) and somewhat out of date now. With DataTables 1.7+ (possibly 1.6 - I can't remember just now!) the callback functions are executed with the DataTables instance's scope - so you can do this.fnGetNodes() if you want to, which is much cleaner.

    Allan
  • lucasnplucasnp Posts: 18Questions: 0Answers: 0
    thumbs up for Allan. Your Datatables is awesome.
This discussion has been closed.