8

I know it's been asked before, but I cant get it to run and I'm out of things to try.

I want to colorize a row in a Grid if its value is not 1 - I use a custom formatter for this. The formatter itself works, that's not the problem.

I've tried multiple ways I've found so far on the web - adding a class, directly adding CSS code, using setRowData, using setCell....

Here are my examples - none of them worked for me (Linux, ff363) - any pointer would be greatly appreciated.

27.05.2010_00:00:00-27.05.2010_00:00:00 is my row id

<style>
.state_inactive {
            background-color: red !important;
        }
.state_active {
    background-color: green !important;
}
</style>

function format_state (cellvalue, options, rowObject)
{
    var elem='#'+options.gid;
    if (cellvalue != 1) {

        jQuery('#list2').setRowData(options.rowID,'',
                                    {'background-color':'#FF6F6F'});

        jQuery('#list2').setRowData('27.05.2010_00:00:00-27.05.2010_00:00:00',
                                    '',{'background-color':'#FF6F6F'});

        for (var cnt=0;cnt<rowObject.length;cnt=cnt+1) {
            jQuery(elem).setCell(options.rowId,cnt,'','state_inactive','');

            jQuery(elem).setCell('"'+options.rowId+'"',cnt,'','state_inactive');

            jQuery(elem).setCell('"'+options.rowId+'"',cnt,'5',
                                 {'background-color':'#FF6F6F'},'');
        }
    } else {
        for (var cnt=0;cnt<rowObject.length;cnt=cnt+1) {
            jQuery(elem).setCell(options.rowId,cnt,'','state_active','');
        }
    }
    <!-- dont modify, we simply added the class above-->
    return cellvalue;
}
msrd0
  • 7,816
  • 9
  • 47
  • 82
Thomas
  • 83
  • 1
  • 1
  • 3

8 Answers8

13

It seems to me that your main problem is you're not setting a 'background-color' style. You should remove 'ui-widget-content' class from the row (from <tr> element)

jQuery("#"+ options.rowId,jQuery('#list2')).removeClass('ui-widget-content');

before adding the class state_activ or state_inactive, because jQuery UI class 'ui-widget-content' is define .ui-widget-content like

{
border: 1px solid #fad42e;
background: #fbec88 url(images/ui-bg_flat_55_fbec88_40x100.png) 50% 50% repeat-x;
color: #363636;
}

and only with setting of CSS 'background-color' your not really change the background color. So try to use something like

var trElement = jQuery("#"+ options.rowId,jQuery('#list2'));
trElement.removeClass('ui-widget-content');
trElement.addClass('state_active');
msanjay
  • 2,255
  • 2
  • 19
  • 19
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Sounds logical but unfortunately it doesnt work - the ui-widget-content class is not removed. I put it in the formatter call and in a gridComplete function - nothing :( – Thomas May 29 '10 at 22:38
  • @Thomas. In `loadComplete` are rows filled and in the `gridComplete` not. At me it works inside of `loadComplete`. I recommend you always use `gridview: true` wnd no time use `afterInsertRow` event, which break quick execution behavior of `gridview: true` parameter. You can call `jQuery('#list2').getDataIDs();` inside or `loadComplete`, test cell value with `getCell()` and then remove and add the class. – Oleg May 30 '10 at 03:57
  • It still doesnt work :( Aparantly its b/c the jquery selector cant find the row id - the object is null although the id is there ... id=25.05.2010_00:00:00-26.05.2010_00:00:00 alerts from var l2=jQuery('#list2'); alert ("id="+ cl +" "+l2.html()); var trElement = jQuery('"#'+cl+'"',jQuery('#list2')); trElement.removeClass('ui-widget-content'); trElement.addClass('state_active'); trElement is null with .html()... – Thomas May 30 '10 at 21:17
  • 2
    It can be that you have such problem because of special symbols inside of `id`. You use ':', '-' and '.'. jqGrid had problems with some symbols inside of `id`. To verify this try to use more easy form of ids. Moreover some characters have a spatial meaning for jQuery. So the constructs like `jQuery("#"+ options.rowId)` can be wrong interpret by jQuery. If all will be work with the simple ids, then you can implement some form of encoding/decoding of id values. – Oleg May 30 '10 at 21:24
  • yeah i thought so already as well - maybe jqgrid.jqid might help here, i'll try that later. In the meanwhile, the following code is working (and fast enough so while its not pretty i might use it) loadComplete: function(xhr) { var mids = jQuery("#list2").getDataIDs(); for (var i=0;i – Thomas May 30 '10 at 22:55
  • JavaScript has no private properties. So you can use any jqGrid internal properties. See http://stackoverflow.com/questions/2917902/get-the-jqgrid-colmodel for example discussion about getting of `colModel`. So you can use construct like `jQuery("#list2")[0].p.colModel` to receive a `colModel` or `jQuery("#list2")[0].p.colNames` to receive `colNames`. Both of properties are arrays so you can use `jQuery("#list2")[0].p.colModel.length` to get a number of columns. Don't forget, that if you use `rownumbers: true` then `colModel` has an additional `rm` column on the first place (index 0). – Oleg May 31 '10 at 00:02
  • Finally that did it - escaping the ids... Thanks a lot for your help:) loadComplete: function(xhr) { var mids = jQuery("#list2").getDataIDs(); for (var i = 0; i < mids.length; i=i+1){ var cl2=jQuery.jgrid.jqID(mids[i]); var trElement = jQuery("#"+ cl2,jQuery('#list2')); trElement.removeClass('ui-widget-content'); trElement.addClass('state_active'); } – Thomas May 31 '10 at 11:34
  • I should correct my a little about the best way to find out the number of columns. `jQuery("#list2").jqGrid ('getGridParam', 'colModel')` is a documented way to receive `colModel` array. So if you want to get the number of VIABLE columns you can do following: `var cm = jQuery("#list2").jqGrid('getGridParam', 'colModel'); var length = cm.length, columnCount = 0; for (var i=0; i – Oleg May 31 '10 at 20:23
  • Thanks, but actually i dont need that any more since i can modify the entire row now and have no need to loop over each cell - but its good to know anyway :) – Thomas May 31 '10 at 21:19
  • I dont want to see the lines of the JQGrid which is displayed at the border then how can I do that ? – Bhavik Ambani Aug 01 '12 at 06:15
  • 1
    @BhavikAmbani: In general it's new question. It's better for other users if you ask the question not in the comments because such answers will be not found during searching. To your problem: You can use `transparent` color for the border. For example `.ui-jqgrid tr.ui-row-ltr td {border-right-color: transparent;}` removes the vertical borders between cells. In the same way `.ui-jqgrid tr.ui-row-ltr td {border-bottom-color: transparent;}` will remove the borders between the rows. [Another answer](http://stackoverflow.com/a/6994678/315935) shows how to remove the borders in the column headers. – Oleg Aug 01 '12 at 11:02
13

for anyone that looking for a real answer at this topic..

this works !

afterInsertRow : function(rowid, rowdata)
{
    if (rowdata.colmodelnamefield == "something")
    {
        $(this).jqGrid('setRowData', rowid, false, 'StyleCss');
    }

}

In another file stylesheet the custom CSS


.StyleCss{ background-color:red !important; }

dont forget the !important to overwrites the theme ui roller

Sk.
  • 460
  • 7
  • 15
5

I was trying solution for a long time and finally from all examples and suggestions combine someting that worked for me. Of course you need to define custom css styles for this to work. Hope that this helps, although it could produce potential speed issue.

...

loadComplete: function() {

      var rowIDs = jQuery("#grid").getDataIDs(); 
      for (var i=0;i<rowIDs.length;i=i+1){ 
        rowData=jQuery("#grid").getRowData(rowIDs[i]);
        var trElement = jQuery("#"+ rowIDs[i],jQuery('#grid'));
        if (rowData.statusID > 5) { 
            trElement.removeClass('ui-widget-content');
            trElement.addClass('rowColorRED');
        }else{ 
          if (rowData.statusID == 1){
            trElement.removeClass('ui-widget-content');
            trElement.addClass('rowColorGREEN');
          }
        }
      }
  },

...

marbo
  • 51
  • 1
  • 1
4

I've tried solutions above, but I think that one is the correct:

afterInsertRow : function(rowid, rowdata)
{
    if (parseFloat(rowdata.amount) > 500)
    {
        $(this).jqGrid('setRowData', rowid, false, {color:'red'});
    }
}

If css class is between apostrophes, then it is overwritten by to the original one (you can see that easily using firebug):

<tr class="ui-widget-content jqgrow ui-row-ltr RedStyle" ...>  

correct with {color:'red'}:

<tr class="ui-widget-content jqgrow ui-row-ltr" style="background: none repeat scroll 0pt 0pt red;" ...>

According to documentation of setRowData:

If the cssprop parameter is string we use addClass to add classes to the row. If the parameter is object we use css to add css properties.

christof
  • 421
  • 4
  • 15
1
 afterInsertRow: function (rowid, rowdata) {                                                     
    $(grid).jqGrid('setRowData', rowid, false, { background: 'red' });
}

Very Simple and works

Ricardo Vieira
  • 730
  • 7
  • 13
1

I suggest that you try someing like this. This will actualy give you access to the whole row.

afterInsertRow: function(rowid, aData, rowelem) 
     {  
        if (aData.field =='value'){    
            jQuery("#list1").setCell(rowid,'message','',{color:'red'});   
        }   
     } 
David
  • 41,209
  • 3
  • 17
  • 14
  • Well this at least works :) I'll try to use setRowData instead of setCell, i hope thats cheaper from an execution point of view - this take quite long on my test vm... – Thomas May 29 '10 at 23:03
0

Assumed other cell value is Y/N.

below code goes in loadComplete event

 var rowIDs = jQuery("#GRID").getDataIDs();   //Get all grid row IDs 
 for (var i = 0; i < rowIDs.length; i++) {     //enumerate rows in the grid
     var rowData = $("#GRID").jqGrid('getRowData', rowIDs[i]);   
     //If condition is met (update condition as req)
     if (rowData["COLNAME_CHECKED"] == "N") {          

         //set cell color if other cell value is matching condition
         $("#GRID").jqGrid('setCell', rowIDs[i], "COLNAMEModified", "", { color: 'red' });
         //for row color, update style. The code is given above by **Ricardo Vieira**
     }
 }
  • Can you describe your code? Why does it helps the OP? – msrd0 Aug 29 '14 at 15:27
  • @user3755692: Already added comments. don't know what is not clear to you. This code colors cells/rows at load time for all rows once the grid is created. If you already know this code, it may be helpful to others. If you think its useless, delete it. – user3990701 Sep 03 '14 at 13:58
  • It's always better to describe your code as good as you can. I don't think any answer that answers the question is useless, but there are always some answers more usefull than others - and you decide which answer is usefull by upvoting it – msrd0 Sep 03 '14 at 15:03
  • yes, u are right, detailed explanation is always better. Sometimes, its little subjective too. hope the updated code better now. BTW, how your user name changed to msrd07 from user3755692? – user3990701 Sep 03 '14 at 15:15
  • yes, your updates version is better. You can change your username by going to your profile, click "edit" and then change your display name – msrd0 Sep 03 '14 at 15:16
0

May be it's too late for this answer but you can use rowattr to change row color like so :

rowattr : function(rd) {
            if(Difference_In_Days>=0&&rd.active==true){
                return {"class" : "online"};
            }else if(rd.active==false){
                return {"class" : "notActive"};
            }
            
        }
Mohammad
  • 739
  • 7
  • 22