2010-12-01 74 views
1

我正在使用CakePHP和jqGrid,但是我有一段困難的時間讓他們說得很好。我可以經歷構建JSON響應的麻煩(我知道,「這並不困難」)),但我寧願簡單地從find(「all」...)返回一個扁平版本的結果。CakePHP和jqGrid

這裏是我當前的代碼(僅選擇的領域我想)。有沒有一種快速簡便的方式來格式化jqGrid的這個很好?

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

在此先感謝!

編輯

我能夠把這個結果到一個數組如jQuery想要它,但現在我得到一個空白表(表呈現,但所有單元格爲空)。這是我的更新代碼,有什麼想法?

$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 
); 

這裏是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" 
} 

回答

1

作爲一些會注意到,我對細胞的關聯數組。最後我做以下,以得到它的工作:

在我的PHP我改變了陣列將上升一個層次:

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

在我的JavaScript我改變了JsonReader默認值之一:

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

退房Integration of Cakephp and JqGrid使用CakePHP 1.1完成。但它可以簡單地轉換爲Cakephp 1.2。希望這可以幫助。

+0

唯一的問題是新的API返回jqGrid無法識別的多維數組。 – Benny 2010-12-01 15:29:53

+0

@Benny:如果您發佈了JSON數據(您可以使用http://www.fiddler2.com/fiddler2/的Fiddler或http://getfirebug.com/的Firebug查看從服務器發送的數據)併發布jqGrid的定義可以幫助你包含讀取數據的`jsonReader`。 – Oleg 2010-12-01 19:22:14

0

我認爲你要格式化的JSON數組作爲這樣JSON Array Formatting

看到整個線程看到的解決辦法..

希望有幫助!