Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlickGrid: How to loop through each row and set color based on the condition?

I'm beginner to SlickGrid. I would like to know on how to loop through each row in a grid and set row back-color based on the condition (for ex: if Age between 20 - 40, the row will have blue color, otherwise, it will have red color).

like image 961
J - C Sharper Avatar asked Feb 15 '23 22:02

J - C Sharper


2 Answers

Assuming you're using Slick.Data.DataView, you can modify the getItemMetadata method to dynamically add classes to the containing row element. I am going to write this as if your Slick.Data.DataView instance is called dataView, here you go:

dataView.getItemMetadata = metadata(dataView.getItemMetadata);

function metadata(old_metadata_provider) {
  return function(row) {
    var item = this.getItem(row);
    var ret  = (old_metadata_provider(row) || {});

    if (item) {
      ret.cssClasses = (ret.cssClasses || '');
      if (item.age >= 20 && item.age <= 40) {
        ret.cssClasses += ' blue';
      } else {
        ret.cssClasses += ' red';
      }
    }

    return ret;
  }
}

This will add a class of 'blue' or 'red' to the row's element.

like image 94
idbehold Avatar answered Feb 17 '23 12:02

idbehold


You'll want to use a formatter so your column definition would look something like this

{id: "delete", name: "Del", field: "del", formatter: Slick.Formatters.Delete, width: 15},

Add your formatters to slickgrid like this

(function ($) {
// register namespace
$.extend(true, window, {
    "Slick": {
        "Formatters": {                
            "Delete": DeleteLink
        }
    }
});


function DeleteLink(row, cell, value, columnDef, dataContext) {
    //Logic to present whatever you want based on the cell data
    return "<a href=\"javascript:removeId('contact', '" + dataContext.folderId + "', '" + dataContext.id + "')\"><img width=\"16\" height=\"16\" border=\"0\" src=\"/images/delete.png\"/></a>";
}

})(jQuery);
like image 22
Damian Avatar answered Feb 17 '23 10:02

Damian