Want to add a class to conditionally

Want to add a class to conditionally

xinxin Posts: 4Questions: 0Answers: 0
edited August 2013 in General
I'm populating my table dynamically using fnAddData. What I need to do is add a class to the tag if one of the row's field's has a certain value (in this case, the field is 'status', and I need to add a class to the row if the status is 'inactive'). I've tried digging around the forums and the API but couldn't find a function that does this (fnCreatedRow doesn't seem particularly suited to this case).

Hoping someone can point me in the right direction. Thank you!

Replies

  • xinxin Posts: 4Questions: 0Answers: 0
    Did some more digging on the forums and found Organicspider's post here: http://datatables.net/forums/discussion/4641/change-tr-class-based-on-data/p1. This would certainly do the trick, but this depends on the fact that the data that the class is based on is being displayed on the table as well. The problem is I won't be displaying the 'status' field in the table.

    For now, I think I can go with organicspider's solution, it works really well with only some minor drawbacks I can definitely work with (maybe I'll just hide that particular column?). But if anyone has a solution that can add a class to the row without being dependent on the data being displayed on the table, please let me know! Many thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Why doesn't fnCreatedRow sound like what your want? It. should be ideal for conditionally adding classes based on data.

    Allan
  • xinxin Posts: 4Questions: 0Answers: 0
    From what I understand, fnCreatedRow uses aData to check for the data on which I'll be basing the condition. But I won't be adding that data onto the table (it's in the array that I use to populate the table, but it won't be displayed on the table), so I'm not sure how else I can access that data. My apologies if the explanation is a little confusing, let me know if I can clear anything up for you.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Right - I hadn't got that before - sorry. However, as long as the data source / variable that you are based your condition upon is scoped so that the mRender function can see it, that shouldn't be an issue.

    Allan
  • xinxin Posts: 4Questions: 0Answers: 0
    Hi Allan, thanks for all your help! But would you mind clarifying what you mean about using mRender?
  • sdinesh21sdinesh21 Posts: 37Questions: 0Answers: 0
    Hi xin,

    I had implemented conditional formatting of a cell by using fnCreatedCell as given below. I believe you have to similarly add class to tr element (nTr).
    [code]
    "aoColumns": [
    { "sTitle": "Type", "mData" : "10"},
    { "sTitle": "Severity", "mData" : "5",
    "fnCreatedCell": function(nTd, sData, oData, iRow, iCol)
    {
    switch(sData)
    {
    case 1:
    $(nTd).addClass('alertSeverityLow');
    $(nTd).text("Low");
    break;
    case 2:
    $(nTd).addClass('alertSeverityMedium');
    $(nTd).text("Medium");
    break;
    case 3:
    $(nTd).addClass('alertSeverityHigh');
    $(nTd).text("High");
    break;
    case 4:
    $(nTd).addClass('alertSeverityCritical');
    $(nTd).text("Critical");
    break;
    }
    }
    }
    ]
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    @xin - There is an example in the documentation of mRender about how to use it as a function.

    Allan
This discussion has been closed.