【问题标题】:problem in jqgrid integration with CI?jqgrid与CI集成的问题?
【发布时间】:2011-11-13 01:14:18
【问题描述】:

顶部的数据是

121310000-00-00103.9845.34149.32这本可创下130000-00-00104.9846.34159.32这本历史记录240000-00-00104.9846.34159.32这本历史记录了250000-00-00104.9846.34159.32这是纪录260000-00-00159.32这本录得682015.34159.32这条记录了682011-09-07108.9850.34159.32这是历史记录792011-09-07108.9850.34159.32这是历史记录8102011-09-07108.9850.34159.32这是记录 9

然后网格有列名但没有数据。我不知道怎么了。如何获取网格内的数据?

控制器:

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

【问题讨论】:

    标签: php codeigniter jqgrid jqgrid-php


    【解决方案1】:

    也许您必须将 jqgrid 参数数据类型设置为“xml”而不是“json”*
    因为您的 url 输出发送 "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 类型。在您的控制器中,您使用 xml 类型创建输出,但将参数数据类型设置为“json”。

    More Information About jqGrid

    【讨论】:

    • 我有同样的问题,我已经将数据类型设置为 xml。我的网格 id 为空,我可以使用 firebug 看到 xml 响应。
    猜你喜欢
    • 2013-02-21
    • 2019-10-24
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多