2

I try to generate a string for the editoptions value using a function. The string should look like this: '1:Active;2:Inactive;3:Pending;4:Suspended'. If I try this string as the value the select on the grid works properly but when I generate it with the function exactly the same and it doesn't work. Can you please tell me what is wrong?

jqGrid code:

{
    label:'Status',
    name:'status',
    editable:true,
    edittype: "select",
    stype:'select',
    formatter:'select',
    editoptions: { value: generate}
}

And the function

function generate(){
    var s="";
    $.ajax({
        type:"GET",
        url:"rst/crud/selectStatus",
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            response = $.parseJSON(JSON.stringify(data));

            $.each(response, function(i, item) {
                s+=response[i].id+":"+response[i].status+";";
            });

            s = s.substring(0, s.length-1);
            //  alert(s);
        }
    });
    return s;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vlad
  • 48
  • 5

1 Answers1

0

The $.ajax call which you use currently will be executed asynchronously. You should use another feature of editoptions in the case: dataUrl and buildSelect. The problem only that the feature works good for editing of the grid, but not with formatter:'select', which you use. The formatter will be used for filling the grid (for processing the server response).

I descried the solution of such scenario in the answer. One need just extend the standard response of the server (the main url) with the data provided via "rst/crud/selectStatus". You can process the data inside of beforeProcessing and to use setColProp to modify the editoptions.value dynamicaly. By the way you can set the value event based on real data in the column of the grid. The scenario is especially practical if you use loadonce: true scenario. I mean the following: you can reduce the set of values from '1:Active;2:Inactive;3:Pending;4:Suspended' till the values which really used in some values in the column. Such value is very practical for searchoptions.value, but you still can use full set of allowed values for editoptions.value.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you for the fast answer.I already tried dataUrl and buildSelect but I have a problem with this when I try to add to database. When I use $('#jqGrid').getRowData(rowKey); on the status column I get the text from the field but I need the id from behind. Using value and formatter:'select' getRowData gives me the id for the status field which I can use. Is there any way I can get the "value" of the selected option and not the text using dataUrl? – Vlad Feb 26 '16 at 09:01
  • @Vlad: The answer on your question can depend on version of jqGrid which you use and from the fork of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or and old jqGrid in version <=4.7). Free jqGrid is the fork which I develop starting with the end 2014, after changing the license agreement of the main fork and renaming the product to Guriddo jqGrid JS. Free jqGrid uses `unformat` for the data returned by `getRowData` one can use `{includeId: true}` option additionally. – Oleg Feb 26 '16 at 09:08
  • @Vlad: I'm not sure in which context you use `getRowData`. Moreover it has `additionalProperties` which extend the local data to additional properties without requirement to use hidden columns. Thus you can use for example both name and id in the data returned from the server. You can fill only the names in the grid, but you can get id from *another* property, which is associated with the row of data. The exact solution depends on other options which you can use in jqGrid. You can post new question if you would have implementation problems. – Oleg Feb 26 '16 at 09:11
  • I solved the problem by adding async:false to my AJAX call, now the function works. Thank you for your support. – Vlad Feb 26 '16 at 13:56
  • @Vlad: You are welcome! `async:false` option should work of cause, but it blocks the web browser till the response is received. Moreover your code send just now another asynchronous request to fill the grid. The request was to *the same server*. What I suggested you was to include the response from `dataUrl` in the main data for filling the grid. It's typically minimal modification of the server code. Then you get the new property with the data inside of `beforeProcessing` and use `setColProp` to modify the `editoptions.value` in the column. In the way all will work asynchronously. – Oleg Feb 26 '16 at 16:32
  • I will take it into consideration in future projects. Thanks for your help Oleg. ( you are some sort of office Hero here at my work place :)) ). – Vlad Feb 29 '16 at 09:09
  • @Vlad: You are welcome! I'm glad that I could help you. – Oleg Feb 29 '16 at 09:23