Question

I'm using jqgrid with ruby on rails and as per my requiremnet in jqgrid listing page i need background color as dynamic for a particular column values.Screen shot for jqgrid listings

Here is my code for helper,

include JqgridsHelper
def colors_jqgrid
  options = {:on_document_ready => true, :html_tags => false}
grid = [{
  :sortable => true,        
  :url => '/colors',
  :datatype => 'json',
  :mtype => 'GET',
  :colNames => ['colors_id','ID','External ID','name','Color Swatch','Actions'],
  :colModel  => [
     { :name => 'colors_id',   :index => 'colors_id',:hidden => true},
    { :name => 'ID',   :index => 'ID',    :width => 180 ,:searchoptions => { 
        :sopt => ['eq','ne','bw','ew','cn'],
        }},
    { :name => 'External ID',   :index => 'color_id',:search=>false,    :width => 180 ,:searchoptions => { 
        :sopt => ['eq','ne','bw','ew','cn'],
        } },    
    { :name => 'name', :index => 'name',  :width => 180 ,:searchoptions => { 
        :sopt => ['eq','in'],
        } },
        { :name => 'color_id', :index => 'Color Swatch',:search=>false,  :width => 180},
        {name:'Actions',index:'action',align:'center',sortable:false,formatter: 'function(cellvalue, options, rowObject) {
              edit = "<span class=\"ui-icon ui-icon-pencil\" title=\"Edit\" style= \"cursor:pointer;float:left;\" onclick=\"window.location.href = '"'colors/edit?id="'"+options.rowId+"'"'"';\"></span>";
              edit += "<span class=\"ui-icon ui-icon-trash\" title=\"Delete\" style= \"cursor:pointer;\" onclick=\"jQuery.fn.fmatter.rowactions.call(this,'"'del'"');\"></span>";
             return edit;
         }'.to_json_var },
  ],
  :editurl => '/colors/grid_update',
  :pager => '#colors_pager',
  :rowNum => 10,
  :rowList => [10, 20, 30],
  :caption => 'Colors',
  :autowidth => true,
  :navigator=>true,
  :excel=>true,
  :viewrecords=>true,
  :ondblClickRow => "function(rowId, iRow, iCol, e) { window.location = 'colors/edit?id='+ rowId; }".to_json_var

}]
pager = [:navGrid, "#colors_pager", { :del => false,:search=>true,:edit => false,:add => false}, 
                                {:closeAfterEdit => true, :closeOnEscape => true}, {}, {}, {:multipleSearch => true}, {}]
   options = [:navButtonAdd, "#colors_pager", {caption: "Columns", title: "Reorder Columns", onClickButton: "function() { jQuery('#colors_list').jqGrid('columnChooser'); }".to_json_var }]
   jqgrid_api 'colors_list', grid, pager, options 
end 
Was it helpful?

Solution

Add following line anywhere in grid variable section.

:afterInsertRow => "function(rowId, data) { $(\"#colors_list\").setCell(rowId, 'color_id', '', {'background-color':'#'+data.color_id }); }".to_json_var

it requires 2 parameters. First is rowId which is identifier of row. second is data which is content of that row with all fields. so i used data.color_id for fetching color code which u want to use to set background color.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top