【问题标题】:Not able to bind data to JQGrid with JSON无法使用 JSON 将数据绑定到 JQGrid
【发布时间】:2013-09-12 05:34:49
【问题描述】:

以下是用于显示的 JSON 数据和 JQGrid 代码。请帮助

JSON 数据* [{"Id":1,"Name":"番茄汤","Category":"杂货","Price":1.0},{"Id":2,"Name":"Yo-yo","类别":"玩具","价格":3.75},{"Id":3,"名称":"锤子","类别":"硬件","价格":16.99}]

**JQGrid Code**
function getData() {
            $.ajax({
                //url: 'api/products',
                data:'{}',
                dataType: "json",
                complete: function (jsondata, stat) {
                    if (stat == "success") {
                        var thegrid = jQuery("#gdCustomers")[0];
                        //var jdata = JSON.parse(eval("(" + jsondata.responseText + ")"));
                        thegrid.addJSONData(JSON.parse('[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]'));
                    }
                }
            });
        }
        'use strict';
        gdCustomers.jqGrid({
            //url: 'api/products',
            datatype: getData,
            mtype: 'GET',
            loadError: function (xhr, status, error) { alert(status + " " + error); },
            //ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
            //serializeGridData: function (postData) {
            //    return JSON.stringify(postData);
            //},
            //jsonReader: {
            //    //root: '',
            //    //page: 1,//obj.d.page; },
            //    //total: 1,//obj.d.total; },
            //    //records: 3,//; }
            //    //rows: [],
            //    id: '0',
            //    cell:'',
            //    //repeatitems: true
            //},
            colNames: ["Id", "Name", "Category", "Price"],
            colModel: [
                         { name: "Id", index: "Id",key:true, width: 50 },
                         { name: "Name", index: "Name", width: 200},
                         { name: "Category", index: "Category", width: 75 },
                         { name: "Price", index: "Price", width: 75}
            ],
            rowNum:10,
            rowList:[10,20,30],
            pager: '#gdCustomersPager',
            //sortname: 'id',
            viewrecords: true,
            //sortorder: "desc",
            width:500,
            height: 200,
            caption: "JSON Example",
            onCellSelect: function (rowid, index, contents, event) {
                alert('onCellSelect: ' + index + ' : ' + contents);
            }

        });
        jQuery("#gdCustomers").jqGrid('navGrid','#gdCustomersPager',{edit:false,add:false,del:false});
    });

服务器端代码 我正在使用带有 Web API 的 asp.net

公共类 ProductsController : ApiController {

    Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1}, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M}, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M} 
    };

    public dynamic GetAllProducts()
    {

        return JsonConvert.SerializeObject(products);

    }

【问题讨论】:

    标签: jquery json jqgrid


    【解决方案1】:

    如果您使用的是本地数据(不是从服务器检索的),那么您需要将loadonce 设置为 true 并使用以下代码:

    var gridData = '[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]'
    
                $("#gdCustomers").jqGrid({
                    loadError: function (xhr, status, error) {
                        alert('load error: ' + error);
                    },
                    datatype: "local",
                    data: gridData,
    
                    colNames: ["Id", "Name", "Category", "Price"],
                    colModel: [
                             { name: "Id", index: "Id",key:true, width: 50 },
                             { name: "Name", index: "Name", width: 200},
                             { name: "Category", index: "Category", width: 75 },
                             { name: "Price", index: "Price", width: 75}
                    ],
    
                    gridview: true,
                    rowNum: 10,
                    rowList: [10, 20, 30],
                    pager: '#gdCustomersPager',
                    viewrecords: true,
                    width:500,
                    height: 200,
                    caption: "JSON Example",
                    loadonce: true
                }).navGrid("#gdCustomersPager", { edit: false, add: false, del: false });
                $("#gdCustomers").jqGrid('setGridParam', { datatype: 'local', data: gridData }).trigger('reloadGrid');
    

    如果您想使用来自服务器的数据来填充网格,请使用以下内容:

    $("#gdCustomers").jqGrid({
                    loadError: function (xhr, status, error) {
                        alert('load error: ' + error);
                    },
                    mtype: 'GET',
                    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
                    url: 'api/products',
                    datatype: "json",
    
                    colNames: ["Id", "Name", "Category", "Price"],
                    colModel: [
                             { name: "Id", index: "Id",key:true, width: 50 },
                             { name: "Name", index: "Name", width: 200},
                             { name: "Category", index: "Category", width: 75 },
                             { name: "Price", index: "Price", width: 75}
                    ],
    
                    gridview: true,
                    rowNum: 10,
                    rowList: [10, 20, 30],
                    pager: '#gdCustomersPager',
                    viewrecords: true,
                    width:500,
                    height: 200,
                    caption: "JSON Example",
                }).navGrid("#gdCustomersPager", { edit: false, add: false, del: false });
                $("#gdCustomers").jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
    

    【讨论】:

    • 您好,感谢您的回复,但是是的,我能够加载本地数据,但无法从服务器加载数据。我使用了相同的设置,我也从服务器端获取 JSON 数据,但问题是它没有显示在网格上。从服务器检索的数据与我用于本地数据的格式相同。除了上述建议的设置之外,如果还有其他需要注意的事项,请告诉我。提前致谢。
    • @user2759271 向我们展示有问题的代码。您的代码没有任何服务器调用。请更新您的问题
    • @user2759271 正如Sharun所说,请发布您的服务器端代码。此外,如果您使用的是 Fiddler 或 Firebug 之类的工具(它们极大地有助于监控网络流量),那么您可以从这两者发布的任何信息也会有所帮助。
    • 嗨,Sharun 和 rwisch45,请在上面的问题中找到服务器端代码,我正在使用带有 Web API 的 asp.net。请指导我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2011-11-18
    • 2013-03-15
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多