Color the row by the field value in the column

Color the row by the field value in the column

MazecMazec Posts: 48Questions: 2Answers: 0
edited February 2018 in Free community support

I'm using a function code, which unfortunately only works when loading a table, when using the coloring search disappears. Do not you have an idea how to solve it so that the coloring is also in search?

Im using this code:

"targets": [0],
        render: function(data, type, full, meta) {
          if (type === 'display' && data == "OK"  ) {//&& c < today
            var rowIndex = meta.row+1;
            $('#vypisDb tbody tr:nth-child('+rowIndex+')').addClass('green');
            return data;
          } else if (type === 'display' && data == "Before"  ) {//&& c < today
            var rowIndex = meta.row+1;
            $('#vypisDb tbody tr:nth-child('+rowIndex+')').addClass('orange');
            return data;
          } else if (type === 'display' && data == "After"  ) {//&& c < today
            var rowIndex = meta.row+1;
            $('#vypisDb tbody tr:nth-child('+rowIndex+')').addClass('red');
            return data;
          } else {
            return data;
          }
        }
      }],

When I use the search engine, the colored background disappears. As shows on screenshots:

Thank you very much for your advice.

EDIT: Updated code formatting using Markdown.

Replies

  • kthorngrenkthorngren Posts: 20,408Questions: 26Answers: 4,789

    I'm not sure why its failing but I would suggest using rowCallback, instead of columns.render, to apply attributes to the row based on the row data. You could do something like this:

      rowCallback: function( row, data, index ) {
        if ( data[0] == "OK" ) {
          $(row).addClass('green');
        } else if ( data[0] == "Before" ) {
          $(row).addClass('orange');
        } else if  ........      
      }
    

    If you are using objects instead of arrays change data[0] to data.<object>.

    Kevin

  • MazecMazec Posts: 48Questions: 2Answers: 0

    Thank you for your help, but unfortunately my tip does not work.
    Not even with a simple view without a search.
    Do not you know how it could be? What am I doing wrong?

    Thank you.

  • Johannes B.Johannes B. Posts: 7Questions: 1Answers: 0
    edited February 2018

    Maybe you could use this instead of the addClass (with rowCallback):

    if ( data.TABELNAME.ROWNAME == "VALUE_TO_COMPARE") {
    $('td:eq(1)', row).css('background-color', 'red');
    $('td:eq(2)', row).css('background-color', 'red');
    }

    This works for me when I want to color the background of the row 1 and 2 even after I search
    for a value.

    Else you could try what happens if you use this:

    "rowCallback": function( row, data, index ) {
          $(row).addClass('green');
    }
    

    THis should color all rows green every time. Is it still working after the search?

  • Johannes B.Johannes B. Posts: 7Questions: 1Answers: 0
    edited February 2018

    Please note that I made A mistake. I am using nested tables.

    You should propatly change this:

    if ( data.TABELNAME.ROWNAME == "VALUE_TO_COMPARE") {
    
    

    to this

    if ( data.NAME_OF_YOUR_COLMN == "VALUE_TO_COMPARE") {
    
  • kthorngrenkthorngren Posts: 20,408Questions: 26Answers: 4,789

    What am I doing wrong?

    Please either post your JS code and an example of your data structure or better yet a test case showing the issue:
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • MazecMazec Posts: 48Questions: 2Answers: 0

    Here is my code thankks for tips: http://live.datatables.net/setovovo/1/edit

  • kthorngrenkthorngren Posts: 20,408Questions: 26Answers: 4,789

    The rowCallback is configured as an option within the Datatables init code. I updated your example:
    http://live.datatables.net/setovovo/2/edit

    Kevin

  • MazecMazec Posts: 48Questions: 2Answers: 0

    Thanks you very much, working correct. <3 <3

  • culterculter Posts: 102Questions: 24Answers: 0

    Hi, I'm trying to solve similar problem and this helped, thanks. What do I need to change if I would like to change only cell color and also change the value e.g. from 1 to YES and from 0 to NO. Thank you

This discussion has been closed.