我试图让 jqgrid 与 codeigniter 一起工作,但我做不到,我只想以 json 格式显示表中的数据......但什么也没发生......但我不知道我做错了什么,我看不到包含我正在调用的内容的表格。
我的 Controller
class Grid extends Controller
{
public function f()
{
$this->load->model('dbgrid');
$var['grid'] = $this->dbgrid->getcontentfromtable();
foreach($var['grid'] as $row) {
$responce->rows[$i]['id']=$row->id;
$responce->rows[$i]['cell']=array($row->id,$row->id_catalogo);
}
$json = json_encode($responce);
$this->load->view('vgrid',$json);
}
function load_view_grid()
{
$this->load->view('vgrid');
}
}
我的模型
class Dbgrid extends Model{
function getcontentfromtable()
{
$sql = 'SELECT * FROM anuncios';
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
我的观点(脚本)
$(document).ready(function() {
jQuery("#list27").jqGrid({
url:'http://localhost/sitio/index.php/grid/f',
datatype: "json",
mtype: "post",
height: 255,
width: 600,
colNames:['ID','ID_CATALOGO'],
colModel:[
{name:'id',index:'id', width:65, sorttype:'int'},
{name:'id_catalogo',index:'id_catalogo', sorttype:'int'}
],
rowNum:50,
rowTotal: 2000,
rowList : [20,30,50],
loadonce:true,
rownumbers: true,
rownumWidth: 40,
gridview: true,
pager: '#pager27',
sortname: 'item_id',
viewrecords: true,
sortorder: "asc",
caption: "Loading data from server at once"
});
});
希望有人帮助我
最佳答案
您在评论中发布的服务器生成的数据
{"rows":{"":{"id":"11","cell":["11","225101503"]}}}
格式错误。输出应该看起来像
{
"total": "xxx",
"page": "yyy",
"records": "zzz",
"rows" : [
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
...
]
}
(参见 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data)。所以至少应该像
{"rows":[{"id":"11","cell":["11","225101503"]}]}
一般来说,如果你定义了一个jsonReader
,你几乎可以读取任何数据。您生成的数据只能由使用函数定义的 jsonReader
读取(参见 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#jsonreader_as_function 和 jquery with ASP.NET MVC - calling ajax enabled web service )。最简单的方法是更改服务器代码以生成标准格式的数据(见上文),标准格式的 jsonReader
可以读取这些数据。
还有一个小评论。使用 sorttype
对 datatype: "json"
没有影响。参数 sorttype
仅适用于本地 数据的排序。在 datatype: "json"
的情况下,服务器将负责正确的数据排序。 jqGrid 只向服务器发送列名,用户选择列名进行数据排序。
关于php - Jqgrid + 代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2917689/