代码之家  ›  专栏  ›  技术社区  ›  Rob Wilkerson

如何加载不带Ajax请求的jqGrid?

  •  1
  • Rob Wilkerson  · 技术社区  · 15 年前

    我发现/看到的每个jsGrid使用示例都显示了通过Ajax请求加载的数据。我想基于已经可用的数据加载网格;除非是技术要求,否则完全不需要单独的请求。

    我真的希望我的控制器提取网格中显示所需的数据,将其传递给我的视图,并让jqGrid根据本地数据来执行它的操作,而不是发起另一个请求。我无法想象这是不可能的,但我甚至没有找到一个不使用 url 配置以获取JSON格式的数据。

    当然,数据加载器不是这么窄,但是有人能给我举一个不以ajax为中心的例子吗?

    谢谢。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Oleg    15 年前

    从3.7版开始,jqgrid就有了全面的本地支持,包括数据排序分页等。

    所以你可以用 data datastr 参数。使用 localReader 可以需要看 documentation . 下面是一个简单的例子:

    var mydata = [
        { id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "3", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "4", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "5", invdate: "2007-10-05", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "6", invdate: "2007-09-06", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "7", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "8", invdate: "2007-10-03", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "9", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "10", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "11", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "12", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "13", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "14", invdate: "2007-10-05", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "15", invdate: "2007-09-06", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "16", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "17", invdate: "2007-10-03", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "18", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }
    ];
    var grid = $("#list");
    grid.jqGrid({
        data: mydata,
        datatype: "local",
        colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
        colModel: [
            { name: 'id', index: 'id', key: true, width: 70, sorttype: "int" },
            { name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
            { name: 'name', index: 'name', width: 100 },
            { name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
            { name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
            { name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
            { name: 'note', index: 'note', width: 150, sortable: false }
        ],
        pager: '#pager',
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'id',
        sortorder: 'asc',
        viewrecords: true,
        height: "100%",
        caption: "Simple loading of local data"
    });
    grid.jqGrid('navGrid', '#pager', { add: false, edit: false, del: false, search: true, refresh: true });
    
        2
  •  1
  •   Jakub Konecki    15 年前

    看看jqGrid示例 here . 展开“加载数据”,然后单击“数组数据”。

    推荐文章