JQgrid中的数据不会出现在CodeIgniter中

时间:2014-12-12 06:57:06

标签: php jquery codeigniter jqgrid

我正在使用CodeIgniter来实现一个示例项目来实现jqgrid。我使用了以下代码。

在控制器中我创建了名为loadDataGrid的函数

function loadDataGrid()
    {
        $page = isset($_GET['page'])?$_GET['page']:1;
        $limit = isset($_GET['rows'])?$_GET['rows']:10;
        $sidx = isset($_GET['sidx'])?$_GET['sidx']:'order_id';
        $sord = isset($_GET['sord'])?$_GET['sord']:'desc';

        if(!$sidx) $sidx =1;

        $count = $this->db->count_all_results('tbl_order');

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $i=0;

        $query = $this->dashboard_model->fetchAllorders($start,$limit,$sidx,$sord);
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i=0;
        foreach($query as $row) {
            $responce->rows[$i]['id']=$row->order_id;
            $responce->rows[$i]['cell']=array($row->order_status,$row->order_auto_id,$row->date,$row->address,$row->borrower);
        $i++;
        }

        $data['order_data'] = json_encode($responce);
        $data['main_content'] = 'dashboard/loadDataGrid';
    $this->load->view('lib/template', $data); 

    }

模型代码在这里

function fetchAllorders($start,$limit,$sidx,$sord,$where=''){

    $this->db->select('order_id,order_status,order_auto_id,date,address,borrower');
    $this->db->limit($limit);
    if($where != NULL)
        $this->db->where($where,NULL,FALSE);
   // $this->db->order_by($sidx,$sord);
    $query = $this->db->get('tbl_order',$limit,$start);

    return $query->result();
}

最后,视图代码就在这里

<link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css' />
<link rel='stylesheet' type='text/css' href='http://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css' />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery-ui-custom.min.js'></script>        
<script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js'></script>
<script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.js'></script>
 <?php
        $ci =& get_instance();
        $base_url = base_url();
    ?>

<script>
$(document).ready(function () {
$("#list_records").jqGrid({
    url: '<?php echo $base_url.'dashboard/loadDataGrid'?>',
    datatype: "json",
    mtype: "GET",
    colNames: ['Status','Order number','Date','Address','Name'],
    colModel: [
    { name: "order_status",align:"right"},
    { name: "order_auto_id"},
    { name: "date"},
    { name: "address"},
    { name: "borrower"}
    ],
pager: "#perpage",
rowNum: 10,
rowList: [10,20,50,100],
sortname: "order_id",
sortorder: "desc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: ""
});     

});
</script>

<table id="list_records"><tr><td></td></tr></table> 
<div id="perpage"></div>

但是当我试图在控制器中打印结果时,它显示为

{"page":1,"total":11674,"records":116732,"rows":[{"id":"1","cell":["","921018","0000-00-00","11343 xxx",""]},{"id":"2","cell":["","921017","0000-00-00","15422 yyy",""]},{"id":"3","cell":["","921016","0000-00-00","930 zzz",""]},{"id":"4","cell":["","921015","0000-00-00","2798 aaa",""]}]}

当我使用PHP时,jqgrid的数据正常运行。我在很多方面尝试过在CodeIgniter中解决这个问题,但我无法找到解决方案。有人可以就此提出建议。

非常感谢。

1 个答案:

答案 0 :(得分:2)

嗨,我不知道你要返回的地方,(从loadDataGrid输出返回的json)。你加载这个视图,但它是什么?

$this->load->view('lib/template', $data); 

如果是包含html的文件,那就不行了。我认为这是json源

 url: '<?php echo $base_url.'dashboard/loadDataGrid'?>',

看起来它呈现页面的html,它应该只是将结果作为json字符串返回。您还需要该文件中正确的标头header('Content-type: application/json');,并让它只返回json。对于json源我会期待这样的东西

 header('Content-type: application/json');
 ... other code here ...
 echo  json_encode($responce);

不返回嵌入数组中的json的html视图。有意义吗?

所以从本质上讲,你需要这些部分,

HTML Content
1.Controller method for HTML page
2.View for HTML page  ( output )

JSON Content
1.Controller for JSON data source ( output )
2.Model for JSON

另一种说法就是在你的代码中输出这个$data['order_data']我在任何地方都看不到它。因此,我怀疑您正在为网格提供HTML而不仅仅是JSON字符串。