0

I need to add dynamics(conditionals) values to an attribute on GridJs table, but i haven't found a way to do it on render time.

I have a column named "Type" and, for a distinct "Type" i have to fill the background color. Suppose I have 3 types: INFO(blue), WARNING(yellow), ERROR(red).

I've been trying to do it with formatter: property, but it only paints the nested html element.

The goal is to paint the whole cell, not only the background div.

My JS code:

var eventGrid = new gridjs.Grid({
        columns: [
            { name: "ID", width: '2%' },
            { name: "DateTimeString", width: '11%' },
            { name: "Username", width: '8%' },
            { name: "Professional ID", width: '2%' },
            { name: "Bio ID", width: '2%' },
            { name: "Type", width: '8%', attributes: {'data-field' : 'HERE THE DYNAMIC ATTRIBUTE VALUE'} formatter: (cell, row, attributes) => setCellColor(cell, row, attributes) },
            { name: "Navigation Type", width: '8%' },
            { name: "Message", width: '8%' },
            { name: "Exception Message" }],
        server: {
            url: '/EventsLogs/Details',
            then: data => data.map(event => [
                event.eventLogId,
                event.eventLogDateTimeString,
                event.username,
                event.professionalId,
                event.bioId,
                event.eventLogTypeName,
                event.navigationTypeName,
                event.message,
                event.exceptionMessage])
        }
    }).render(document.getElementById("eventsLogs-table"));

And the formatter function does the following:

function setCellColor(cell, row, att){
        switch(cell) {
            case 'INFO':
                    cell = `<div style='background-color: #3393FF; width:100%; color:white;'>${cell}</div>`;
            break;
            case 'WARNING':
                    cell = `<div style='background-color: #FFCE33; color:white;'>${cell}</div>`;
            break;        
            case 'ERROR':
                    cell = `<div style='background-color: #FC1235; color:white;'>${cell}</div>`;
            break;
        }
        return gridjs.html(cell);
    }

Result:

enter image description here

As you can see everything is working fine but not as expected. The expected result is to paint the whole cell.

Inspecting the HTML there is a span element as parent and then the td element. That's why I'm trying as a workaround a do it by an attribute.

Thanks!

enter image description here

  • Can you try to expand the div to fill the entire cell? https://stackoverflow.com/questions/3215553/make-a-div-fill-an-entire-table-cell – This Guy Dec 14 '22 at 21:44
  • @ThisGuy I tried it with a width and height of 100% but it doesn't work...so, checking the native theme, i can modify it, the problem is that if I alter the native theme i will alter ALL the columns, so, it sounds easy, but it's complicated at the same time, because obviously the Grid is automatically rendered with its own html elements. – David Lerma Developer Dec 14 '22 at 22:59
  • Would you be willing and able to set up a snip / sample? It would really help to get this dialed in for you by allowing others to work with your sample. – This Guy Dec 15 '22 at 15:19

0 Answers0