Conditional Formatting on some of the cells in my table

Conditional Formatting on some of the cells in my table

NevskiNevski Posts: 2Questions: 1Answers: 0

Hello All,

I am very new to DataTables and Editor, and I am trying to find any details I can on how to perform a specific task.

I am wanting to change the background colour of certain cells within my table, based on the data that is in that cell. This is both before or after an edit, and on multiple pages of data.

I am using the .NET libraries within MVC,

I want to do something like the below (code is for example and is not intended to be correct!)

$('#tablename').on("change", function () {
            if ($('#tablename').cellvalue.startsWith("P")) {
                $('#tablename').addClass('pass');
            }
        });

Can anyone tell me the correct way to achieve this please? I only want this condition to be checked on certain cells, not all cells within the table.

Many thanks.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,569Questions: 26Answers: 4,996
    Answer ✓

    rowCallback might be what you are looking for.

    Kevin

  • NevskiNevski Posts: 2Questions: 1Answers: 0

    @kthorngren thank you! This was just what I needed. I have included the code I used below in case it helps anyone in the future.

    "rowCallback": function (row, data, index) {
                        if (/^p/i.test(data.ColumnName)) {
                            $('td:eq(1)', row).css('background-color', 'green');
                            $('td:eq(1)', row).css('text-align', 'center');
                        }
    

    PS : the "i" flag in /RegExp/i is optional and stands for case insensitive (so it will also return true for "Passed", "pAssEd", etc.). and the td:eq(1) should reflect the number of the field you are formatting.

    Neil.

This discussion has been closed.