admin管理员组文章数量:1355748
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).
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).
Share Improve this question edited Mar 5, 2015 at 17:55 John 1689 bronze badges asked Oct 21, 2013 at 14:10 J - C SharperJ - C Sharper 1,7077 gold badges25 silver badges37 bronze badges2 Answers
Reset to default 7Assuming 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.
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);
本文标签: javascriptSlickGrid How to loop through each row and set color based on the conditionStack Overflow
版权声明:本文标题:javascript - SlickGrid: How to loop through each row and set color based on the condition? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743937528a2564955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论