Php JQgrid中的数据没有进入CodeIgniter
我正在做一个使用CodeIgniter实现jqgrid的示例项目。我使用了下面的代码 在控制器中,我创建了名为loadDataGrid的函数Php JQgrid中的数据没有进入CodeIgniter,php,jquery,codeigniter,jqgrid,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'])?$_
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);
}
最后是视图代码
<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>
当我使用PHP时,jqgrid中的数据是正确的。我在CodeIgniter中尝试了很多方法来解决这个问题,但我找不到解决方案。有人能给我建议一下吗
非常感谢。您好,我不知道您要返回哪里(从loadDataGrid输出返回json)。您加载此视图,但它是什么
$this->load->view('lib/template', $data);
如果是包含html的文件,那就不起作用。我将其视为json源代码
url: '<?php echo $base_url.'dashboard/loadDataGrid'?>',
不返回数组中嵌入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\u data']
我在任何地方都看不到。因此,我怀疑您正在向网格提供页面HTML,而不仅仅是JSON字符串。您好,谢谢您的帮助。由于我是codeigniter的新手,我正在努力实现这一点。我还尝试添加标题('Content-type:application/json');echo json_编码($response);但它只是简单地显示json响应,如{“page”:1,“total”:11674,“records”:116732,“rows”:[{“id”:“1”,“cell”:[”,“921018”,“0000-00-00”,“11343 Redbud Court”,“]},{“id”:“2”,“cell”:[”,“921017”,“0000-00-00”,“15422 Via Penoles”,“]},{“id”:“3”,“cell”:[”,“921016”,“0000-00-00”,“930 Hayes Avenue”,“]}。你能给我一个代码片段来实现这一点吗?你必须在一个独立于html的请求中完成json输出。这已经过了我睡觉的时间,但基本上是做一个请求,用一个url呈现页面视图,然后从网格中请求一个不同的url,并让这个url只返回json内容。明白吗?
url: '<?php echo $base_url.'dashboard/loadDataGrid'?>',
header('Content-type: application/json');
... other code here ...
echo json_encode($responce);
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