donglu9872 2014-12-12 06:57
浏览 59

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

I'm working on a sample project to implement jqgrid using CodeIgniter. I've used the below code.

In controller i've created function with the name 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); 

    }

model code goes here

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();
}

And finally the view code goes here

<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>

But when i tried to print the results in controller it is showing like

{"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",""]}]}

When I was worked with PHP the data with jqgrid is coming properly. I was tried in so many ways to fix this in CodeIgniter, but i couldn't find the solution. Can some one please suggest on this.

Thanks very much.

  • 写回答

1条回答 默认 最新

  • duanlei2458 2014-12-12 07:11
    关注

    Hi I don't see where you are returning, ( outputting the return json from loadDataGrid ). You load this view, but what is it?

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

    If it is the file with the html in it, that wont work. I see this as the json source

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

    And that looks like it renders the html for the page, when it should just return result as a json string. You'll also want the proper header header('Content-type: application/json'); in that file and have it only return the json. I would expect something like this for the json source

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

    Not returning a html view with the json embedded in an array. Make sense ?

    So in essence you will need these parts,

    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
    

    Another way to put it is where in your code are you outputting this $data['order_data'] I don't see it anywhere. So I suspect you are feeding the grid your page HTML instead of just the JSON string.

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条