开发者

CakePHP and jqGrid

开发者 https://www.devze.com 2023-01-27 13:39 出处:网络
I am u开发者_StackOverflow社区sing CakePHP and jqGrid, but am having a difficult time making them talk nice.I can go through the trouble of building a JSON response (I know, \"it\'s not that difficult

I am u开发者_StackOverflow社区sing CakePHP and jqGrid, but am having a difficult time making them talk nice. I can go through the trouble of building a JSON response (I know, "it's not that difficult" ;)) but I would prefer to simply return a flattened version of the result from find("all"...).

Here is my current code (selecting only the fields I want). Is there a quick and easy way to format this nicely for jqGrid?

$result = $this->Contact->find('all', array(
                'fields' => array('first_name', 'last_name', 'company', 'title'),
                'conditions' => array('OR' => $filters),
                'offset' => $skip,
                'limit' => $rows
                ));

Thanks in advance!

EDIT

I Was able to turn the results of this into an array like Jquery wanted it, but now I'm getting a blank table (table renders but all cells are empty). Here is my updated code, any ideas?

$result = $this->Contact->find('all', $options);

$data = array();
for ($i = 0; $i < count($result); $i++) {
    $data[$i]['id'] = $result[$i]['Contact']['id'];
    $data[$i]['cell'] = $result[$i]["Contact"];
}

$result = array(
    'rows' => $data,
    'page' => $page,
    'total' => ceil($totalPages / $rows),
    'records' => $totalPages
);

Here is a snip of the json:

{
   "rows":[
      {
         "id":"2160",
         "cell":{
            "first_name":"Rory",
            "last_name":"Johnson",
            "company":"somewhere",
            "title":"Director of Engineering",
            "id":"2160"
         }
      },
      {
         "id":"2297",
         "cell":{
            "first_name":"Steven",
            "last_name":"Johnson",
            "company":"Another place",
            "title":"Dir Busn Proc Desgn",
            "id":"2297"
         }
      }
   ],
   "page":"1",
   "total":"75",
   "records":"748"
}


As some will notice, I have an associative array for the cells. I ended up doing the following to get it to work:

In my PHP I changed the array to be up one level:

for ($i = 0; $i < count($result); $i++) {
    $data[$i] = $result[$i]["Contact"];
}

In my Javascript I changed one of the default values for the JsonReader:

...
    jsonReader: {
       repeatitems: false
    },
...


check out Integration of Cakephp and JqGrid done with cakephp 1.1. but it can be simply convert to Cakephp 1.2. Hope this helps.


i think you want to format the JSON array as like this JSON Array Formatting

see the whole thread to see the solution..

hope it help!

0

精彩评论

暂无评论...
验证码 换一张
取 消