简体   繁体   English

ajax中的jqGrid本地数据

[英]jqGrid local data within ajax

Hello stackoverflow nation ! 你好stackoverflow国家! I've such a snag. 我有这样的障碍。 Trying to pass in jqGrid array data which is retrieved within ajax, but it doesn't work. 试图传入在ajax中检索的jqGrid数组数据,但它不起作用。 lets take a look at script => 让我们来看看script =>

$(function(){ // this script works just fine (of course this array and jqGrid initialization script is in the same file)
var arr = [
    {a:"a",b:"b"},
    {a:"c",b:"d"}
];
$("#_tb").jqGrid({
    datatype: "local",
    data: arr,
    colNames: ["ONE","Two"],
    colModel: [
        {name:"a",index:"a",align:"center"},
    {name:"b",index:"b",align:"center"}
    ],
    pager: $("#_pager"),
    height: "auto"
});
});

here is my problem => 这是我的问题=>

$.ajax({
    url: "../info.php",
    type: "get",
    data: {},
    success: function(r){
        $("#_tb").jqGrid({
        datatype: "local",
        data: r,
        colNames: ["ONE","Two"],
        colModel: [
            {name:"a",index:"a",align:"center"},
            {name:"b",index:"b",align:"center"}
        ],
        pager: $("#_pager"),
        height: "auto"
        });
    }
    });

this script doesn't work , but data is successfully retrieved within ajax into json format. 此脚本不起作用,但数据在ajax中成功检索为json格式。 here is info.php script by the way too 这里也是info.php脚本

// using PDO for connection
foreach($con->query("SELECT * FROM tb") as $row){
    $info[] = array(
        "a" => $row["a"],
        "b" => $row["b"]
    );
}
echo json_encode($info);

PS. PS。 In my opinion my problem is connected to datatype , but can't made up my mind how to solve that despite of searching examples like that . 在我看来,我的问题与数据类型有关 ,但是无论如何搜索这样的例子,我都无法解释如何解决这个问题。 Also remarkable is that I want datatype to be local , because of searching and filtering data in jqGrid without any SQL where statements. 同样值得注意的是,我希望数据类型是本地的,因为在jqGrid中搜索和过滤数据而没有任何SQL where语句。 With any advice I'll be pleased , thanks :) 有任何建议我会很高兴,谢谢:)

Sorry, but the code which you posted do work: see the demo : 抱歉,您发布的代码确实有效:请参阅演示

在此输入图像描述

So you should search for the problem in another place. 所以你应该在另一个地方搜索问题。

One possible problem for example could be that you execute the code multiple times . 例如,一个可能的问题可能是您多次执行代码。 You should create grid once and then change the data by changing the value of data parameter and trigger reloadGrid . 您应该创建一次网格,然后通过更改data参数的值并触发reloadGrid来更改data By the way you can use url: "../info.php" directly in the jqGrid. 顺便说一句,您可以直接在jqGrid中使用url: "../info.php" You need just add the corresponding jsonReader (see here ). 您只需添加相应的jsonReader (请参阅此处 )。 To be able to use local filtering you need just add loadonce: true to the list of jqGrid parameters. 为了能够使用本地过滤,您只需将loadonce: true添加到jqGrid参数列表中即可。 Alternatively you can call GridUnload method every time before recreating the grid (see the answer for exmaple). 或者,您可以重新创建网格之前每次调用GridUnload方法(请参阅GridUnload 的答案 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM