Highlight rows in GridField based on proximity to date

SS Version 4.4

How can I highlight (change font or background color) the rows in GridField results based on a Date Field?

I have a date field which is in the future. The closer I get to the date the more the row should get red.

Any ideas, anyone?

Best

Off the top of my head I’m not sure the easiest way to highlight the whole row (without javascript), but if you would settle for having one field that is highlighted that should be fairly easy. Something like this would make dates closer than 7 days (or in the past) bold, and more red the closer the date is (untested):

$summary_fields = [
    'Title' => 'Title',
    'HighlightedDate' => 'Date',
];

public function HighlightedDate()
{
    $date = $this->dbObject('Date');
    $defaultColor = ['102', '102', '102']; // RGB grey
    $highlightColor = ['255', '000', '000']; // RGB red
    // Start warning at 7 days
    $warningPeriod = 7;
    $daysAway = max(min(floor((strtotime($date->getValue()) - time())/60/60/24), $warningPeriod), 0);
    $highlightFactor = 1 - ($daysAway / $warningPeriod);
    // Blend the colors
    $blendedColor = [];
    foreach ($defaultColor as $i => $color) {
        $defaultValue = (1 - $highlightFactor) * $color;
        $highlightValue = $highlightFactor * $highlightColor[$i];
        $blendedColor[] = round($defaultValue + $highlightValue);
    }
    // Create HTML
    $niceDate = $date->Nice();
    $color = implode(',', $blendedColor);
    $tag = $daysAway < $warningPeriod ? 'strong' : 'span';
    return DBField::create_field('HTMLText', 
        "<$tag style=\"color: rgb($color)\">$niceDate</$tag>"
    );
}

this works out of the box :slight_smile: thank you.

the only negative thing here is that the column is not sortable because it contains HTML I think.

See .Nice formating kills column sorting