tags:

views:

866

answers:

4

Hi all,

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

So 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, thats not the problem.

I've tried multple 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 gratly 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;
}

Thanks, Thomas

+1  A: 

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
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
A: 

It seems to me that your main problem is 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.raddClass('state_active');
Oleg
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
@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
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 <tbody><tr id="25.05.2010_00:00:00-26.05.2010_00:00:00" role="row" class="ui-widget-content jqgrow ui-row-ltr"><td ...</tr>alerts fromvar 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
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
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<mids.length;i=i+1){ var cl=mids[i]; var state=jQuery("#list2").getCell(cl,'state'); if (state !=1) { for (var l=0;l<7;l=l+1) { jQuery("#list2").setCell(cl,l,'',{'background-color':'red'}); }}Any way to find out the # of cols dynamically? I tried getRowData).length but that didnt work out...
Thomas
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
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
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<length; i++) { if (!cm[i].hidden } }`. The value `columnCount` is the number of visible columns of the jqGrid #list2. Column "rn" (has index 0) exist only if `rownumbers: true`.
Oleg
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