4

我是 SlickGrid 的初学者。我想知道如何遍历网格中的每一行并根据条件设置行背景颜色(例如:如果年龄在 20 - 40 之间,则该行将具有蓝色,否则,它将具有红色)。

4

2 回答 2

7

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.

于 2013-10-25T21:22:33.273 回答
1

您需要使用格式化程序,以便您的列定义看起来像这样

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

像这样将格式化程序添加到 slickgrid

(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);
于 2013-10-25T20:24:47.660 回答