jqGrid row alternating background
Asked Answered
T

6

10

How can I insert alternating row background color in jqGrid?

Telephoto answered 22/11, 2009 at 7:46 Comment(0)
P
9

Look at the altRows and altclass options. Beware of the typically inconsistent capitalization! This does use the jQuery UI theme if you're using jqGrid 3.5 or higher.

Pommard answered 23/11, 2009 at 22:30 Comment(0)
K
2
function applyZebra(containerId) {
    $('#' + containerId + ' tr:nth-child(even)').addClass("jqgrow evenTableRow");
    $('#' + containerId + ' tr:nth-child(odd)').addClass("jqgrow oddTableRow");
}

ContainerId is your jqGrid ID. Call this method on the "gridComplete" event of your jqGrid.

Karriekarry answered 22/11, 2009 at 8:36 Comment(1)
Great solution, but it would be nice if the even/odd colors could be selected from the jQuery UI Theme.Quorum
I
2

To use the jQuery UI theme use this code:

$('#'+gridName+' tr:nth-child(even)').removeClass("ui-priority-secondary");
$('#'+gridName+' tr:nth-child(odd)').addClass("ui-priority-secondary");

I use this code when I perform manual sorting (drag-n-drop)

Idiotism answered 18/2, 2010 at 15:44 Comment(0)
R
0

Hello first defines the css:

<style type="text/css"> 
......
.Color_Red { background:red; }
.Color_Cyan { background:cyan; } 
......

Then in jqGrid ...

$("#list2").jqGrid({
........
loadComplete: function() {
  var rowIDs = jQuery("#list2").getDataIDs(); 
  for (var i=0;i<rowIDs.length;i=i+1){ 
    rowData=jQuery("#list2").getRowData(rowIDs[i]);
    var trElement = jQuery("#"+ rowIDs[i],jQuery('#list2'));

// Red       
    if (rowData.Estado == 0) {
        trElement.removeClass('ui-widget-content');
        trElement.addClass('Color_Red');
    }

// Cyan        
    if (rowData.Estado == 2) {
        trElement.removeClass('ui-widget-content');
        trElement.addClass('Color_Cyan');
    }
}
},

});

Thus we walk the rows and apply RED to fulfill the condition that == 0 and Cyan which satisfy the condition == 2.

You should change rowData.XXX == XX by column name and value to check.

I have it this way and it works perfectly.

Luck!

Raver answered 9/10, 2012 at 22:27 Comment(0)
G
0

Call loadComplete function to change background color of row in jqgrid.

loadComplete : function() {
    $("tr.jqgrow:odd").addClass('myAltRowClassEven');
    $("tr.jqgrow:even").addClass('myAltRowClassOdd');
},

for applying styles to background use below css.

<style type="text/css">
.myAltRowClassEven { background: #E0E0E0; border-color: #79B7E7; }
.myAltRowClassOdd { background: orange; }
</style>

Or

For changing row font in jqgrid see click below link

How can I change Background colour and Font of any row in JQGrid?

Gilemette answered 19/3, 2014 at 12:12 Comment(0)
F
-1

Here's how you do it:

$("#myGrid").jqGrid({
   ...
   gridComplete: function() {
       var _rows = $(".jqgrow");
       for (var i = 0; i < _rows.length; i += 2) {
           _rows[i].attributes["class"].value += " alt";
       }
   }
});
Fransiscafransisco answered 23/11, 2009 at 11:24 Comment(1)
Way too much work. This feature is already built into the grid. No need to reinvent it.Pommard

© 2022 - 2024 — McMap. All rights reserved.