1

Is it possible to merge the Column Model javascript file and the raw data JSON response into 1 file?

Oleg - here you go:

JSON - codes.json

{
    "codes":[
        {
            "code" : "WFH - Work From Home"
        },  
        {
            "code" : "OST - Onsite"
        }
]}

dataUrl and buildSelect - this is drawing up an empty select box

    editoptions: {
        dataUrl: 'http://localhost/json/codes.json',
        type: "GET",
        dataType: "json",
        contentType: "application/x-javascript; charset=utf-8",
        async: "true",
        buildSelect: function(response){
            var s = '<select>';
            $.each(response.codes, function(index, codes){
                s.append("<option>"+codes.code+"</option>");
            });
            return s + '</select>';
        }
    }},
techlead
  • 779
  • 7
  • 24
  • 44
  • probably you want to have the features like I described in [the feature request]. This features are not yet implemented in jqGrid. If you make the requirements not so general it could be found probably a ways to implement there in the existing jqGrid. – Oleg Nov 10 '11 at 17:52
  • I am looking for a way to populate a select field with options in the column model. I browsed the related questions - it seems it's possible to do this with dataUrl and buildSelect. I understood the dataUrl but don't know how to use buildSelect as I'm getting the response in JSON format. Can you help? – techlead Nov 10 '11 at 17:59
  • If you would include in your question an example of JSON data from `dataUrl` I could help you with `buildSelect`. It can be for example array of strings if you don't need to use different `value` and the text inside of ` – Oleg Nov 10 '11 at 18:08
  • Oleg, I added it in, pls see above in the original post. – techlead Nov 10 '11 at 18:25
  • Hi Oleg, it's not working for me. Can you pls look into it and let me know what I'm doing wrong? – techlead Nov 10 '11 at 18:47

1 Answers1

1

You should modify the code of buildSelect to about the following

buildSelect: function (data) {
    var s = '<select>', codes, i, l, code, prop;
    if (data && data.codes) {
        codes = data.codes;
        for (i = 0, l = codes.length; i < l; i++) {
            code = codes[i];
            // enumerate properties of code object
            for (prop in code) {
                if (code.hasOwnProperty(prop)) {
                    s += '<option value="' + prop + '">' + code[prop] + '</option>';
                    break; // we need only the first property
                }
            }
        }
    }
    return s + "</select>";
}

Additionally you should use ajaxSelectOptions to set any options of the corresponding $.ajax request which you jqGrid if it get data from from the server. In any way you should use relative URLs like json/codes.json or /json/codes.json instead of http://localhost/json/codes.json.

An example of ajaxSelectOptions parameter could be the following

ajaxSelectOptions: {
    dataType: 'json',
    cache: false
}

If contentType: "application/x-javascript; charset=utf-8" is really required you can add it as additional property of ajaxSelectOptions.

How you can see from the demo the selects will be produced correct from your JSON data by above buildSelect function. The select looks like

<select role="select" id="2_code" name="code">
    <option value="code1" role="option">WFH - Work From Home</option>
    <option value="code2" role="option">OST - Onsite</option>
</select>
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @SK11: You are welcome! Absolute another thing: I see that you don't use voting of any answers or questions of other. Voting is *very important* because it helps other people to find better answer. [Here](http://stackoverflow.com/faq#howtoask) you can read: "As you see new answers to your question, vote up the helpful ones by clicking the upward pointing arrow to the left of the answer." You have right to make about 30 votes per day. I recommend to use the right if you want help other people. If would be good it you vote up all good answers on your old questions which helped you before. – Oleg Nov 10 '11 at 20:37
  • Another interesting question ... http://stackoverflow.com/questions/8086381/jqgrid-inline-editing-logic-in-json-response – techlead Nov 10 '11 at 21:15