【问题标题】:Rails jqgrid column background color dynamicRails jqgrid 列背景颜色动态
【发布时间】:2014-05-05 08:57:01
【问题描述】:

我正在使用 jqgrid 和 ruby​​ on rails,根据我在 jqgrid 列表页面中的要求,我需要将背景颜色作为特定列值的动态。

这是我的助手代码,

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 

【问题讨论】:

    标签: ruby-on-rails ruby jqgrid


    【解决方案1】:

    在网格变量部分的任意位置添加以下行。

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

    它需要 2 个参数。首先是 rowId,它是行的标识符。第二个是数据,它是该行的所有字段的内容。所以我使用 data.color_id 来获取你想用来设置背景颜色的颜色代码。

    【讨论】:

    • 你的意思是在 :colModel 之后?
    • 是的,你可以放在那里。我在回答中提到过。
    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 2017-12-10
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2014-12-12
    相关资源
    最近更新 更多