JqG​​rid-合并网格列模型javascript& JSON回应 [英] JqGrid - Merging the Grid Column Model javascript & JSON response

查看:56
本文介绍了JqG​​rid-合并网格列模型javascript& JSON回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Column Model javascript文件和原始数据JSON响应合并为1个文件?

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

Oleg-您来了:

JSON-codes.json

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

dataUrl和buildSelect -这是一个空的选择框

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>';
        }
    }},

推荐答案

您应将buildSelect的代码修改为以下内容

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>";
}

此外,如果jqGrid从服务器获取数据,则应该使用ajaxSelectOptions设置相应的$.ajax请求的任何选项.无论如何,您都应使用json/codes.json/json/codes.json之类的相对URL代替http://localhost/json/codes.json.

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.

ajaxSelectOptions参数的示例可能如下

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

如果确实需要contentType: "application/x-javascript; charset=utf-8",则可以将其添加为ajaxSelectOptions的附加属性.

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

如何从演示中看到通过上述buildSelect函数从您的JSON数据中正确生成.选择看起来像

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>

这篇关于JqG​​rid-合并网格列模型javascript&amp; JSON回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆