views:

79

answers:

1

Hi,

is there a way to modify the values of a status field in the colModel dynamically? Lets say we have a col Model with a field like:

... field ... name: "state",type: "select",
editoptions: {value: "0:state0;1:state1;2:state2;3:state3;4:state4"}

So I get a select field for my states with this values. But I need to dynamicaly decide which selectfields should be possible. If the state of the current row is state0, only state0 and state1 should be displayed. If state is state1, display should be state0, state1 and state2 and so on up to state 4 which should display only stae3 and state4.

Am I able to solve this with a formatter, or is there any other way to do such a thing.

To make it more difficult, lets say the states which are displayed generally are dependend on a user who is logged in, in my application. In a way the user only can see state0, state2 and state4. This can be made even more complicated, cause the transistion between state3 and state4 is not allowed to the current user.

Nevertheless, the states themself are dynamicaly either. Would it be helpful to dynamically generate the javascript for an object in my application, which represents a general state-class and use this object to generate my needed output in the formatter? So I can encapsulate the logic within this object, how my output is generated and additionaly I get only the states the user is able to see.

Should get me to kill two birds with one stone.

After rereading I hope its clear what I want to do, if not tell me and I will explain it with more details.

A Solution for the concrete Problem, thx to oleg:

    editoptions : {

    value : function(){
        //a function can be called here:
        currentRow=$("#order_items").getGridParam('selrow');
        currentState=$("#order_items").getCell(currentRow,"state");
                    nastyGeneratedThings=function(){
                                     ... do some nasty things with currentState
                                     ... and generate what you want
                                     }
                    return nastyGeneratedThings
    }

I ran into some trouble, cause the function was only called once. So I have to set the recreateForm option in Navgrid.

navGrid("#pager", {
            edit : true,
            add : true,
            del : true
        }, {
            height : 500,
            width : 500,
            // recreate the form every time when edit button is clicked.
            // Default is false.
            recreateForm : true
        }
        });

After that my function fires each time I click edit. Hope this helps someone somehow.

+1  A: 

The value property from the editoptions can be not only a string but also a function. The function can return either a string like "0:state0;1:state1;2:state2;3:state3;4:state4" or a object like {"0":"state0", "1":"state1", "2":"state2", "3":"state3", "4":"state4"}. The last format by the way has some advantages: you can for example use ':', ';' inside of the values.

The function has no parameters, but you can get the current selected row with a coll like getGridParam('selrow') method and with getCell(rowid,iCol) or getCell(rowid,"state") the current value of the "state" column.

See more in the description of the value property on http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions). Probably this can solve your problem?

Oleg
Thx, that helped me. I overrated the problem, cause I was thinking I have to do all stuff when creating the grid. But thats not true. I just need to implement my logic before the editForm is created. I will do some test tommorrow, infact I think I cannot use it this way. Are you sure I could call getCell() or something like that in the function itself, cause at the call-time the row itself is not existent. I ran into a similar problem few days ago in a formatter, I was not able to get any value out of the grid. Than I realised, the row was actually not existent.You know what I mean?
evildead
Comment field was too short, damn :)So if I rethink the problem it doesnt matter what I give in the colModel. I can inject the concrete states directly into the form before I do my Edit. Or is there an elegant way to modify the fields in the editform? e.g. override the colModel before the editForm is rendered. Or do I have to do this by hand, by deleting the original states in the form and insert only the possible states?
evildead
You can in very simple way to define whether the loading is completed of not (setting a variable inside of `loadCompleted` for example and reset it in `beforeRequest` or `loadBeforeSend`). During table loading your `value` function can returns full list of values. Only in editing more can be important to return reduced list of values from your `value` function. Isn't it so?
Oleg
Changing of `colModel` dynamically is also possible. On http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options you can read, that it is not possible to change `colModel` with respect of `setGridParam`. Yes, you can not replace one `colModel` with another one with respect of `setGridParam`, but it is do possible to change `colModel` with the following trick. First you get reference to the `colModel object with `var cm = $("#list").getGridParam("colModel");`. Then you modify a column with `jQuery.extend(cm[iCol], { /* some new properities: editoptions: { value: newV }*/});`. This way works.
Oleg
Hi Oleg, big thx again. I was able to do what I want. I edited my main post, so there is a sconcrete solution for the problem.
evildead