【问题标题】:How Do I Load jqGrid w/o an Ajax Request?如何在没有 Ajax 请求的情况下加载 jqGrid?
【发布时间】:2011-05-03 12:32:42
【问题描述】:

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

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

当然,数据加载器并没有这么狭窄,但有人可以指出一个不以 ajax 为中心的示例吗?

谢谢。

【问题讨论】:

    标签: jqgrid jqgrid-php


    【解决方案1】:

    从 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】:

      查看 jqGrid 示例here。展开“加载数据”并点击“数组数据”。

      【讨论】:

      • +1,因为我非常专注于格式化选项。
      猜你喜欢
      • 2017-11-02
      • 2017-09-15
      • 1970-01-01
      • 2018-10-26
      • 2014-03-02
      • 2015-11-08
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多