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

大城市里の小女人 提交于 2021-02-07 19:38:13

问题


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).


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/19496846/slickgrid-how-to-loop-through-each-row-and-set-color-based-on-the-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!