post by mopac on Oct 6

Changing the text colour of data in a cell, in a row, in a datagrid is possible by using $crud->callbackColumn() and adding html to the cell (e.g. “FF-”.$value.“”).

But how can I change the background colour of an entire row?

Unless something already exists, I think I would like a new function that allows me to add a css class atrribute to the tag of a row defined by the row ID number.

I’m open to any ideas to acheive the same thing

Ray

1 month later

post by johnny 17 hours ago

Hello @mopac , and sorry for the delay.

Yes this should be a new functionality (which honestly, first time that someone is asking for it). Till then you can add a small work-around which I have tested that it works. First you need to add in the callbackColumn a class (e.g. “test-red”) so we recognize that this row will need to be colored. For example:

$crud->callbackColumn('my_date', function ($value) {
    return "<div class='test-red'>" . $value."</div>";
});

And then in JavaScript add something like this:

(function () {
  // marker class inside a cell  ->  class added to the row
  const ROW_MARKERS = {
    'test-red': 'gc-row-red'
  };

  function paint(root) {
    for (const [marker, rowClass] of Object.entries(ROW_MARKERS)) {
      const rows = new Set();
      root.querySelectorAll('tbody .' + marker).forEach(el => {
        const tr = el.closest('tr');
        if (tr) rows.add(tr);
      });

      // remove from rows that no longer match (React reuses the <tr> elements)
      root.querySelectorAll('tr.' + rowClass).forEach(tr => {
        if (!rows.has(tr)) tr.classList.remove(rowClass);
      });
      rows.forEach(tr => tr.classList.add(rowClass));
    }
  }

  function init() {
    document.querySelectorAll('.grocery-crud').forEach(root => {
      let scheduled = false;
      const schedule = () => {
        if (scheduled) return;
        scheduled = true;
        requestAnimationFrame(() => {
          scheduled = false;
          paint(root);
        });
      };

      new MutationObserver(schedule).observe(root, { childList: true, subtree: true });
      schedule();
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();

were initially you are adding the logic:

  const ROW_MARKERS = {
    'test-red': 'gc-row-red'
  };

which test-red is the name of the class and gc-row-red will be the class of the tr and you will have something like this:

tr.gc-row-red {
    background-color: red !important;
}

I hope it helped. Since the above is a bit more complicated solution, what is the functionality that you are trying to achieve? from your question I see that you have the color directly as a color which can make the grid too colorful (e.g. usually we add only few row colors)

Regards
Johnny

1 / 2
Oct 6