0

我正在尝试将 php 脚本的输出加载到 jqgrid 中,但我可以看到没有数据的表。

<div id='Results'>
<div id= 'abcd' style="display:block";>
<?php include("simple.php");?> 
</div>
<script type="text/javascript">
jQuery(function($) {
var data=  $('#abcd').text();
var data1= data.replace(/\"/ig,'\"');
var data1= data1.replace(/[\“\”\″]/ig,'\"');
jQuery("#tableid").jqGrid({
dataType: "json",
data:data1,
colNames:['snp_id','chr','start_pos','end_pos','class','refbase','variantbase'],
colModel:[
  {name:'snp_id',index:'snp_id', width:50},
  {name:'chr',index:'chr', width:50},
  {name:'start_pos',index:'start_pos', width:60},
  {name:'end_pos',index:'end_pos', width:75},
  {name:'class',index:'class', width:75, align:"right" },
  {name:'refbase',index:'refbase', width:75},
  {name:'variantbase',index:'variantbase', width:150}
],
pager: '#pager',
rowNum:100,
sortname: 'snp_id',
sortorder: "asc",
viewrecords: true,
gridview: true,
height: '100%',
width: '100%',
caption:"Title",
loadComplete: function(reload) {
  jQuery("#myGridID").trigger("reloadGrid");
}
});
});
</script> 

Simple.php 将 JSON 格式的数据检索到名为“abcd”的 div

{"total":1,"page":1,"records":5,"rows":[{"id":"CaSNP000002","cell":[{"snp_id":"CaSNP000002","chr":"Ca4","start_pos":"10077235","end_pos":"10077254","class":"D","refbase":"CTCTCTCTCTCTCTCTCTCT","variantbase":null}]},{"id":"CaSNP000003","cell":[{"snp_id":"CaSNP000003","chr":"Ca4","start_pos":"10077245","end_pos":"10077254","class":"D","refbase":"CTCTCTCTCT","variantbase":null}]},{"id":"CaSNP000004","cell":[{"snp_id":"CaSNP000004","chr":"Ca4","start_pos":"10077253","end_pos":"10077254","class":"D","refbase":"CT","variantbase":null}]},{"id":"CaSNP000005","cell":[{"snp_id":"CaSNP000005","chr":"Ca4","start_pos":"10077254","end_pos":"10077255","class":"I","refbase":"------                                                                                              ","variantbase":"CTCTCT"}]},{"id":"CaSNP000006","cell":[{"snp_id":"CaSNP000006","chr":"Ca4","start_pos":"10077254","end_pos":"10077255","class":"I","refbase":"--------                                                                                            ","variantbase":"CTCTCTCT"}]}]}
4

2 回答 2

0

试试这个代码......我不是 100% 它会工作:)

PS>更新..如果它不工作尝试调试你的问题..搜索错误使用控制台日志等..没有错误不容易找到你的问题

<div id= 'abcd' style="display:block";>
<?php include("simple.php");?> 
</div>
<script type="text/javascript">
jQuery(function($) {
var data= $('#abcd').text();
data = jQuery.trim(data); // to remove all empty spaces else json parse may fail
data = jQuery.parseJson(data); // parsing string to json object/array
jQuery("#tableid").jqGrid({
    datatype: "local",
    colNames:['snp_id','chr','start_pos','end_pos','class','refbase','variantbase'],
    colModel:[
      {name:'snp_id',index:'snp_id', width:50},
      {name:'chr',index:'chr', width:50},
      {name:'start_pos',index:'start_pos', width:60},
      {name:'end_pos',index:'end_pos', width:75},
      {name:'class',index:'class', width:75, align:"right" },
      {name:'refbase',index:'refbase', width:75},
      {name:'variantbase',index:'variantbase', width:150}
    ],
    pager: '#pager',
    rowNum: data.rows.length,
    sortname: 'snp_id',
    sortorder: "asc",
    viewrecords: true,
    gridview: true,
    height: '100%',
    width: '100%',
    caption:"Title",
    loadComplete: function(reload) {
      jQuery("#myGridID").trigger("reloadGrid");
    }
});
var mydata = data.rows;
for(var i=0;i<=mydata.length;i++)
    jQuery("#tableid").jqGrid('addRowData',i+1,mydata[i]);
});
</script> 
于 2013-11-07T10:33:55.230 回答
0

我根据您的代码片段做了一些演示,使用如下数据类型:

            datatype: 'jsonstring',
            datastr:data,

终于为我填充了网格。我从这里找到了解决方案https://stackoverflow.com/a/6833082/1945020

于 2013-11-07T14:18:17.930 回答