0
votes

(FYI, I'm using jqGrid 5.)

My question is about a column filter in jqGrid. I have a field that comes back as a boolean from the server, either "true" or "false". I may have the luxury of changing this to a 0/1, but I really don't want to if I can avoid it. So, it looks like this:

{"rows":[{"rowID":47568,"field1":"some text here","isOpen":true} ...]}

In my jqGrid, I am using the "beforeProcessing" function to show that boolean as something besides "true" or "false":

beforeProcessing: function(data) {
    for(var i=0, len=data.rows.length; i< len; i++) {
       data.rows[i].isOpenModified = openFormatter(data.rows[i].isOpen);
    }
...

function openFormatter(isOpen) {
    return isOpen ? '' : 'CLOSED';
}

This has the effect of leaving the cell blank if isOpen is true, and showing 'CLOSED' if it's false. Everything is good so far.

The problem I'm having is the select field for the toolbar filter field. Here is the jqGrid column model:

    colModel: [
                { name: 'field1', label: 'Stuff', width: 100},
                { name: 'isOpenModified', index:'isOpen', label: 'CLOSED',
                   searchoptions: { sopt:['eq'], value: ':All;" ":Open;"CLOSED":CLOSED'}, stype: 'select'},
            ],

This correctly shows the select box for the "CLOSED" field, but selecting anything besides "All" shows nothing.

I did try to change the isOpenModified to a 0 and 1, and then the filter select box worked, but of course I can't deliver that. My requirement is to display either empty (isOpen==true) or "CLOSED" (isOpen==false).

I feel like the solution might involve the name and index fields of the column model, but I can't pinpoint what I'm doing wrong. I appreciate your time....

1
Why you use index:'isOpen' property in the column name: 'isOpenModified'? I think that you should remove it first of all. Additionally I'd recommend you to change openFormatter to set not empty string as the value (like return isOpen ? ' ' : 'CLOSED'; or return isOpen ? '&nbsp;' : 'CLOSED';, ... instead of return isOpen ? '' : 'CLOSED';) and fix value` of searchoptions corresponds to your changes. - Oleg
Thanks Oleg, I did stumble across that solution right after I posted the question, and that did resolve the initial issue...but introduced another. (See answer below) And yes, I did remove the index specification - I've always been a bit fuzzy on the difference. - Stephanie
You are welcome! To tell the trust I didn't understand why you want to change original Boolean values of isOpen property with values true and false? Why you not just use { name: 'isOpen', label: 'CLOSED', searchoptions: { sopt: ['eq','ne'], value: ':All;true:Open;false:CLOSED'}? - Oleg
Because that's not what the user wants. User wants to see a blank cell when isOpen= true and 'CLOSED' when false. :-/ Is there a way to show that, but still have the searchoptions specify true/false? - Stephanie
OK, but what I mean is independent from formatting. You can define any custom formatter and unformat to display the original values in other way. I wanted to mention that jqGrid use the loaded data for filtering. I suppose that you have problems because you use trimable space as the value of false. I suppose that the usage of '&nbsp;' or '&#160;' instead of ' ' could solve your problem. - Oleg

1 Answers

0
votes

I discovered the solution to the original problem, however it presents another problem.

The searchoptions needed to be modified in order to make it work:

{ name: 'isOpenModified', label: 'CLOSED',
                   searchoptions: { sopt:['eq'], value: ':All; :Open;CLOSED:CLOSED'}, stype: 'select'} 

Notice the quotes are removed from around the space and from around CLOSED.

I also changed the format method to return a space instead of an empty string:

return isOpen ? ' ' : 'CLOSED';

This causes another issue: Now the "Open" option is selected by default (but it wasn't filtered by Open items). I attempted to solve that problem by reversing the order:

{ name: 'isOpenModified', label: 'CLOSED',
                   searchoptions: { sopt:['eq'], value: ' :Open;:All;CLOSED:CLOSED'}, stype: 'select'} 

This worked insomuch as "All" was selected by default, but it was 2nd in the list. I'm waiting for the "style" guys to throw a fit. Any advice?