如何将单元格颜色设置为具有冻结列和组的网格?

时间:2016-11-01 13:29:45

标签: jquery jqgrid

我有以下jqgrid 4.6网格

enter image description here

网格代码:

<!DOCTYPE html>
<html>
  <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/le-frog/jquery-ui.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/css/ui.jqgrid.css" />
  </head>
  <body>

    <table id="list47"></table>
    <div id="plist47"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/js/i18n/grid.locale-en.js"></script>
    <script type="text/javascript">
      $.jgrid.no_legacy_api = true;
      $.jgrid.useJSON = true;
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/js/jquery.jqGrid.min.js"></script>
    <script>
      $(function(){

        var mydata = [
          {id:"1",invdate:"2010-05-24",name:"test",note:"note",tax:"10.00",total:"2111.00"} ,
          {id:"2",invdate:"2010-05-25",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
          {id:"3",invdate:"2007-09-01",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
          {id:"4",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
          {id:"5",invdate:"2007-10-05",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
          {id:"6",invdate:"2007-09-06",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
          {id:"7",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
          {id:"8",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"21.00",total:"320.00"},
          {id:"9",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"11",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"12",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"13",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"14",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"15",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"16",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"17",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"18",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"19",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"21",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"22",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"23",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"24",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"25",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"26",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
          {id:"27",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
          {id:"28",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
          {id:"29",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}
        ];




        jQuery("#list47").jqGrid({
          data: mydata,
          datatype: "local",
          height: 'auto',
          rowNum: 30,
          rowList: [10,20,30],
          colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
          colModel:[
            {name:'id',index:'id', width:60, sorttype:"int"},
            {name:'invdate',index:'invdate', width:90, sorttype:"date", formatter:"date"},
            {name:'name',index:'name', width:100, editable:true},
            {name:'amount',index:'amount', width:80, align:"right",sorttype:"float", formatter:"number", editable:true},
            {name:'tax',index:'tax', width:80, align:"right",sorttype:"float", editable:true},
            {name:'total',index:'total', width:80,align:"right",sorttype:"float"},
            {name:'note',index:'note', width:150, sortable:false}
          ],
          pager: "#plist47",
          viewrecords: true,
          sortname: 'name',
          grouping:true,
          groupingView : {
            groupField : ['name'],
            groupColumnShow : [false],
            groupText : ['<b>{0} - {1} Item(s)</b>']
          },
          caption: "Configure group header row"
        });
      })

      jQuery("#list47").jqGrid('setGroupHeaders', {
        useColSpanStyle: false,
        groupHeaders:[
          {startColumnName: 'id', numberOfColumns: 2, titleText: 'Client Details'}
        ]
      });
      jQuery("#list47").jqGrid('setFrozenColumns');

    </script>

  </body>
</html>

我想对网格做以下事情:

  1. 冻结列InvoiceNo,Date并使其他列向右水平滚动
  2. 我需要将单个单元格颜色设置为与某些颜色匹配的颜色 条件
  3. (A。) 将值为0.00的“数量”列单元格设为“红色”。这意味着代码应该在Amount列中找到值为0.00的所有单元格,并将这些单元格设置为“红色”

    请注意,我不需要将整行用红色着色只有具有匹配值的单元格

    (B。) 如上所述,在总列中找到值大于300.00的单元格并将其着色为黄色

    我如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

首先,您应该移动setGroupHeaders块内的JavaScript代码的最后一部分(在setFrozenColumns$(function(){...})处调用)。

要根据amount的值设置行的颜色/背景颜色,您应该使用rowattr

rowattr: function (item) {
    if (item.amount == null || parseFloat(item.amount) === 0) {
        //return { "class": "ui-state-error" };
        //return { "style": "color: red" };
        return { "style": "background: red" };
    }
}

请参阅此处的演示https://jsfiddle.net/f7yopLr0/1/。它产生的结果如下图所示

enter image description here

更新:要更改颜色,只能使用amounttotal列的单元格

cellattr: function (rowid, cellValue, item) {
    if (cellValue == null || parseFloat(cellValue) === 0) {
        return ' style="color:red"';
    }
}
<{1>}列中的

amount
<{1>}列中的

。见https://jsfiddle.net/f7yopLr0/2/