jqgrid与CI集成的问题?

时间:2011-09-08 14:17:26

标签: php codeigniter jqgrid jqgrid-php

顶部的数据是

  

121310000-00-00103.9845.34149.32这是记录120000-00-00104.9846.34159.32这是记录230000-00-00104.9846.34159.32这是记录240000-00-00104.9846.34159.32这是记录250000-00-00104.9846.34159.32这是记录260000-00-00104.9846.34159.32这是记录272011-09-07108.9850.34159.32这是记录682011-09-07108.9850.34159.32这是记录792011-09-07108.9850.34159.32这是记录8102011-09-07108.9850.34159.32这是记录9

然后网格有了coloumn名称,但没有数据。我不知道出了什么问题。如何在网格中获取数据?

控制器:

function loadDataGrid() {
    $this->load->view('test2');//test.php view file
    $page = isset($_POST['page']) ? $_POST['page'] : 1; // get the requested page
    $limit = isset($_POST['rows']) ? $_POST['rows'] : 10; // get how many rows we want to have into the grid
    $sidx = isset($_POST['sidx']) ? $_POST['sidx'] : 'name'; // get index row - i.e. user click to sort
    $sord = isset($_POST['sord']) ? $_POST['sord'] : ''; // get the direction

    $start = $limit * $page - $limit; // do not put $limit*($page - 1)
    $start = ($start < 0) ? 0 : $start;  // make sure that $start is not a negative value

    $where = ""; //if there is no search request sent by jqgrid, $where should be empty
    $searchField = isset($_POST['searchField']) ? $_POST['searchField'] : false;
    $searchOper = isset($_POST['searchOper']) ? $_POST['searchOper'] : false;
    $searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false;

    $this->load->model('users');
    if (!$sidx)
        $sidx = 1;
    $count = $this->users->total_results(); //get total rows from table daily

    if ($count > 0) {
        $total_pages = ceil($count / $limit);    //calculating total number of pages
    } else {
        $total_pages = 0;
    }

    if ($page > $total_pages)
        $page = $total_pages;
    $query = $this->users->getAllGrid($start, $limit, $sidx, $sord, $where); //add parameter to model

    $responce->page = $page;
    $responce->total = $total_pages;
    $responce->records = $count;

    $this->output->set_header("Content-type: text/xml;charset=utf-8");
    $s = "<?xml version='1.0' encoding='utf-8'?>";
    $s .= "<rows>";
    $s .= "<page>" . $responce->page . "</page>";
    $s .= "<total>" . $responce->total . "</total>";
    $s .= "<records>" . $responce->records . "</records>";

    foreach ($query as $row) {
        $s .= "<row id='" . $row['invid'] . "'>";
        $s .= "<cell>" . $row['invid'] . "</cell>";
        $s .= "<cell>" . $row['invdate'] . "</cell>";
        $s .= "<cell>" . $row['amount'] . "</cell>";
        $s .= "<cell>" . $row['tax'] . "</cell>";
        $s .= "<cell>" . $row['total'] . "</cell>";
        $s .= "<cell>" . $row['note'] . "</cell>";
        $s .= "</row>";

    }
    $s .= "</rows>";
    echo $s;
}

查看:

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" type="text/css" media="all" />
        <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
        <link type="text/css" href="<?php echo base_url() ?>jqgrid/css/ui.jqgrid.css" rel="stylesheet" />
        <link type="text/css" href="<?php echo base_url() ?>jqgrid/css/jquery.searchFilter.css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>jqgrid/js/i18n/grid.locale-en.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>jqgrid/jquery.jqGrid.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>jqgrid/jquery.jqGrid.min.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>jqgrid/jquery.layout.js"></script>

        <title>Demo Jquery JqGrid Codeigniter</title>
    </head>
    <body>
        <?
        $ci = & get_instance();
        $base_url = base_url();
        ?>

        <script type="text/javascript">
            jQuery().ready(function (){
                jQuery("#list1").jqGrid({
                    url:'<?= $base_url . 'index.php/daily/loadDataGrid' ?>',      //another controller function for generating data
                    mtype : "post",             //Ajax request type. It also could be GET
                    datatype: "json",            //supported formats XML, JSON or Arrray
                    colNames:['Date','Name','Amount'],       //Grid column headings
                    colModel:[
                        {name:'invid', index:'invid', width:55},
                        {name:'invdate', index:'invdate', width:90},
                        {name:'amount', index:'amount', width:80, align:'right'},
                        {name:'tax', index:'tax', width:80, align:'right'},
                        {name:'total', index:'total', width:80, align:'right'},
                        {name:'note', index:'note', width:150, sortable:false}
                    ],
                    rowNum:10,
                    width: 450,
                    height: 300,
                    rowList:[10,20,30],
                    pager: '#pager1',
                    sortname: 'id',
                    viewrecords: true,
                    rownumbers: true,
                    gridview: true,
                    caption:"List Daily"
                }).navGrid('#pager1',{edit:false,add:false,del:false});
            });
        </script>

        <table id="list1"></table> <!--Grid table-->
        <div id="pager1"></div>  <!--pagination div-->
    </body>
</html>

型号:

function getAllGrid($start, $limit, $sidx, $sord, $where) {
    $this->db->select('invid,invdate,client_id,amount,tax,total,note');//->from('invheader');
    $this->db->limit($limit);
    if ($where != NULL
        )$this->db->where($where, NULL, FALSE);
    $this->db->order_by('invid',$sord);//$sord $sidx
    $query = $this->db->get('invheader', $limit, $start);
    return $query->result_array();
}

output screenshot

1 个答案:

答案 0 :(得分:1)

也许您必须将jqgrid 参数数据类型设置为“xml”而不是“json”*
因为您网址的输出会发送“xml”代码,但您将其设置为“json”
也许它导致你的jqgrid错误的结果(没有数据)。

我为你使用json为我在本地创建的不使用url的数据做了示例。

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JqGrid Test</title>
<script src="js/jquery-1.5.1.min.js" type="application/javascript"></script>
<script src="js/jquery-ui-1.8.11.custom.min.js" type="application/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="application/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="application/javascript"></script>
<link href="css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" type="text/css" />
<link href="css/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script>
$(document).ready( function(){
   // Create JSON data locally   
   var myData= [
       { name: 'jqgrid1', address: 'in the sky', jobs: 'give a nice table' },
       { name: 'jqgrid2', address: 'in the sky', jobs: 'give a nice table' },
       { name: 'jqgrid3', address: 'in the sky', jobs: 'give a nice table' },
       { name: 'jqgrid4', address: 'in the sky', jobs: 'give a nice table' }
    ];

   $grid= $("#list2");      
   $grid.jqGrid({       
       data: myData,
       datatype: "local", // Set datatype to detect json data locally
       colNames:['Name','Address','Jobs'],
       colModel:[
           {name:'name',index:'name', width:55},
           {name:'address',index:'address', width:90},
           {name:'jobs',index:'jobs', width:100}
       ],
       rowNum: 2,
       rowList:[10,20],
       pager: '#pager2',
       sortname: 'name',
       viewrecords: true,
       sortorder: "asc",
       caption:"JSON Example"
   })
   $grid.jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
});

</script>
</head>
<body>
<table id="list2"></table>
<div id="pager2"></div>
</body>
</html>

如果要生成json数据,请从数组类型中获取数据,然后使用json_encode($ yourdata)更改为json类型。在Controller中,您可以使用xml类型创建输出,但设置参数数据类型将变为“json”。

More Information About jqGrid