1

以下は、表示に使用される JSON データと JQGrid コードです。

JSON DATA * [{"Id":1,"Name":"トマトスープ","Category":"食料品","Price":1.0},{"Id":2,"Name":"ヨーヨー","Category":"Toys","Price":3.75},{"Id":3,"Name":"ハンマー","Category":"Hardware","Price":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 を使用しています

public class 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);

    }
4

1 に答える 1

0

(サーバーから取得されていない) ローカル データを使用している場合は、loadoncetrue に設定し、次のコードを使用する必要があります。

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');
于 2013-09-08T18:04:45.587 に答える