CakePHP + jqGrid整合的例子

html:
<table id="list47"></table>
<div id="plist47"></div>


js:
$(document).ready(function(){
    jQuery("#list47").jqGrid({
        //data: mydata,
        url:"/sites/ajaxListAll?XDEBUG_SESSION_START=php_debug",
        datatype: "json",
        height: 150,
        rowNum: 10,
        rowList: [10,20,30],
        colNames:['ID', '欢迎语句','显示订阅','显示联系方式','网站名称','网站邮箱','联系邮箱','电话','描述'],
        colModel:[
            {name:'site_id',index:'site_id', width:100},
            {name:'wellcome_title',index:'wellcome_title', width:80},
            {name:'is_subscription',index:'is_subscription', width:80},
            {name:'is_contact',index:'is_contact', width:80},
            {name:'name',index:'name', width:80},
            {name:'site_email',index:'site_email', width:80},
            {name:'site_contact_email',index:'site_contact_email', width:80},
            {name:'tel',index:'tel', width:80},
            {name:'descript',index:'descript', width:150}
        ],
        pager: "#plist47",
        viewrecords: true,
        jsonReader: {
            repeatitems: false
        },
        loadComplete:function(){
        },
        caption: "Manipulating Array Data"
    });
});



Controller:
<?php
class SitesController extends AppController
{
    public $name = "Site";
    public function ajaxListAll()
    {
        $list=$this->Site->find("all");
        $data=array();
        for($i=0; $i<count($list); $i++){
            $data[$i]['site_id'] = $list[$i]['Site']['site_id'];
            $data[$i]['wellcome_title'] = $list[$i]['Site']['wellcome_title'];
            $data[$i]['is_subscription'] = $list[$i]['Site']['is_subscription'];
            $data[$i]['is_contact'] = $list[$i]['Site']['is_contact'];
            $data[$i]['name'] = $list[$i]['Site']['name'];
            $data[$i]['site_email'] = $list[$i]['Site']['site_email'];
            $data[$i]['site_contact_email'] = $list[$i]['Site']['site_contact_email'];
            $data[$i]['tel'] = $list[$i]['Site']['tel'];
            $data[$i]['descript'] = $list[$i]['Site']['descript'];
        }
        if ($this->request->is('get')) { //这里怎么判断。可能根据情况,但是我使用这样判断是没问题的。
            $result = array(
                'rows' => $data,
                'page' => 1,
                'total' => 2,
                'records' =>1
            );
            return new CakeResponse(array('body' => json_encode($result), 'status' => 200));
        }
    }

}
?>

你可能感兴趣的:(cakephp)