1
我有一個問題,當我使用codeigniter框架時,在jqGrid中顯示數據。 我已經用幾乎相同的代碼做了一個測試,沒有codeigniter,而且是o.k.我可以在網格中看到數據。Codeigniter jqGrid
我使用codeigniter 2.01,jquery 1.6.2和jqGrid 4.1.2 這裏是我的代碼,每一個幫助表示讚賞。
控制器1:(我使用模板和功能schulform調用死視圖「grid.php」)
public function schulform()
{
fill_template('grid',navigation(),'banner_login');
}
控制器2:(我知道SQL語句應該是一個模型,這只是用於測試)
class Gridserver extends CI_Controller {
public function __construct()
{
parent::__construct();
[.... css and js-Files....]
}
public function start()
{
$page = $this->input->post('page',true);
$limit = $this->input->post('rows',true);
$sidx = $this->input->post('sidx',true);
$sord = $this->input->post('sord',true);
if(!$sidx) $sidx =1;
$sql="SELECT * FROM tbl_schulform";
$query = $this->db->query($sql);
$count = $query->num_rows();
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;
$sql = "SELECT SFId,Schulformname from tbl_schulform";
$query = $this->db->query($sql);
//just for testing
$page="1";
$total_pages="1";
$count="3";
$et='>';
$this->output->set_content_type("content-type: text/xml");
$s = "<?xml version='1.0' encoding='utf-8'?$et\n";
$s = "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";
foreach($query->result_array() as $row){
$s .= "<row id='". $row['SFId']."'>";
$s .= "<cell>'". $row['SFId']."'</cell>";
$s .= "<cell><![CDATA[". $row['Schulformname']."]]></cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
}
}
視圖(grid.php):
<script type="text/javascript">
$(function(){
jQuery('#listSchulform').jqGrid({
url:'<?=base_url().'gridserver/start'?>',
datatype: 'xml',
mtype: 'post',
colNames:['ID', 'Schulformname'],
colModel :[
{name:'SFId', index:'SFId', width:400},
{name:'Schulformname', index:'Schulformname', width:150}
],
pager: '#pager',
rowNum:15,
rowList:[15,30,45],
sortname: 'Schulformname',
sortorder: 'asc',
viewrecords: true,
caption: 'Schulform',
editurl:"",
height:335
});
});
</script>
<center><table id='listSchulform'></table></center>
<div id='pager'></div>
功能fill_template是一個幫手:
function fill_template($content,$navi,$login)
{
$ci=& get_instance();
$ci->template->write('title','DWO');
$ci->template->write_view('content',$content);
if ($navi != '')
$ci->template->write('navi',$navi);
if ($login != '')
$ci->template->write_view('login',$login);
$footer="Footer dummy";
$ci->template->write('footer',$footer);
$ci->template->render();
}
最後這是由gridserver生成/啓動時,我直接調用這些函數的數據(我deletet所有<和>括號顯示代碼在這裏):
- rows page1/page total1/total records3/records - row id="1" cell'1'/cell - cell - ![CDATA[ Berufsschule ]] /cell /row - row id="2" cell'2'/cell - cell - ![CDATA[ Vollzeitschulform ]] /cell /row - row id="3" cell'3'/cell - cell - ![CDATA[ test ]] /cell /row /rows
您能發佈確切的服務器響應(HTTP響應的正文與XML數據)嗎?您可以使用[Fiddler](http://www.fiddler2.com/fiddler2/)或[Firebug](http://getfirebug.com/)來獲取數據。您還可以另外驗證服務器響應的HTTP標頭中有哪些「Content-type」? – Oleg