Coloring row if..

Coloring row if..

Alex2019Alex2019 Posts: 62Questions: 9Answers: 0

Hello,
i have this code for the table

$(document).ready(function() {
var oldStart = 0;
$('#tblFem').dataTable({
"bJQueryUI": true,
"order": [[ 1, "asc" ]],
"dom": '<lfi<t>ip>',
"sPaginationType": "full_numbers",
"fnDrawCallback": function (o) {
if ( o._iDisplayStart != oldStart ) {
var targetOffset = $('#tblFem').offset().top;
$('html,body').animate({scrollTop: targetOffset}, 500);
oldStart = o._iDisplayStart;
}
}
});
} );

I would like to display the colored lines only if there is a word
example:

if (data [5] == "Active") {
$ (row) .addClass ('green');
}
if (data [5] == "Retired") {
$ (row) .addClass ('red');

I tried to enter the code but it doesn't work

$(document).ready(function() {
var oldStart = 0;
$('#tblFem').dataTable({
"bJQueryUI": true,
"order": [[ 1, "asc" ]],
"dom": '<lfi<t>ip>',
"createdRow": function( row, data, dataIndex ) {
        if ( data[5] == "Active" ) {        
        $(row).addClass('green');
        }
        if ( data[5] == "Retired" ) {        
        $(row).addClass('red');
        
        }
"sPaginationType": "full_numbers",
"fnDrawCallback": function (o) {
if ( o._iDisplayStart != oldStart ) {
var targetOffset = $('#tblFem').offset().top;
$('html,body').animate({scrollTop: targetOffset}, 500);
oldStart = o._iDisplayStart;
}
}
});
} );

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I would like to display the colored lines only if there is a word

    Are you saying want those lines entirely removed? If so, you could either remove them before you initialise DataTables, or within initComplete call row().remove(),

    Colin

  • Alex2019Alex2019 Posts: 62Questions: 9Answers: 0

    Hi Colin
    do not remove them,
    color them with these words
    Active = Green
    Retired = Red
    the code I entered is probably wrong

    thanks

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    I tried to enter the code but it doesn't work

    What happens?

    You post this but it looks like you don't have the closing } for createdRow which should result in a syntax error in the browser's console:

    "createdRow": function( row, data, dataIndex ) {
            if ( data[5] == "Active" ) {       
            $(row).addClass('green');
            }
            if ( data[5] == "Retired" ) {       
            $(row).addClass('red');
             
            }
    },  // this looks to be missing
    "sPaginationType": "full_numbers",
    

    If this doesn't help then please post a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Alex2019Alex2019 Posts: 62Questions: 9Answers: 0

    I hadn't noticed the error :D
    thanks kthorngren ;)

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Sorry, I entirely misread that. Yep, Kevin's solution works, or you could do something like this: http://live.datatables.net/vejupiva/2/edit

    Colin

  • Alex2019Alex2019 Posts: 62Questions: 9Answers: 0

    Colin hi,

    there is an error in your code

    http://live.datatables.net/vejupiva/2/edit

    Line 36: Missing semicolon.

  • Alex2019Alex2019 Posts: 62Questions: 9Answers: 0
    edited December 2020

    Hi Colin,
    it also works with your code :)

    Thanks ;)

This discussion has been closed.