KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Compare two table column values #2220

Closed Ron opened this topic on on Jul 20, 2021 - 8 comments

Ron commented on Jul 20, 2021

Hi, I have a report that shows a table with three columns Columns 1 = name of person Column 2 = Total dept Column 3 = total paid

I need the following output to display the third column in color: Red in case total paid < total dept Green in case total paid >= total dept I guess I need to use a Map process and scan the value can you please guide me how to do that

Sebastian Morales commented on Jul 21, 2021

You can also use Table's "cssClass" property to add css classes for each cell:

https://www.koolreport.com/docs/koolphp/table/#table-settings-cssclass-options

Based on the $row values for your total paid and total debt columns you return certain css classes for your third column. Then add some color css rules in your page for those css classes. Rgds,

KoolReport commented on Jul 21, 2021

Another way is to use "formatValue"

Table::create([
    "columns"=>[
        "name",
        "total_dept",
        "total_paid"=>[
            "formatValue"=>function($value,$row) {
                $bgColor = ($row["total_dept"]>$row["total_paid"])?"background-color:red":"background-color:green";
                return "<span style='$bgColor'>$value</span>";
            }
        ]
    ]
]);
Ron commented on Jul 21, 2021

Tnx Sebastian. the second way works fine. the issue is that the value is now inside a span and the background color is only in the span itself. I want to mark in red the parent TD of the table that the value is in.

Sebastian Morales commented on Jul 21, 2021

Then use the "cssClass" property which would assign css classes to the td element. Rgds,

Ron commented on Jul 21, 2021

but can I use the ccClass with a formatValue=>function($value,$row) {}?

KoolReport commented on Jul 21, 2021

It is possible, we have example to use the function with "cssClass" that you have a look:

cssClass options

Ron commented on Jul 22, 2021

I see the example for the td but I can not access the value inside the td.

Ron commented on Jul 22, 2021

problem solved!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None