2011-11-17 143 views
1

我在動作控制器返回的JSON數據:jQuery用戶界面自動完成

$results = $repo->getMatchingCityName($searchTerm); 

迴應說我越來越:

[{"CityName":"Montreal"},{"CityName":"New york"}]........ 

但jQuery UI的自動完成功能不顯示任何

試圖

$this->_helper->json(array_values($results)); 

Zend_Json::encode($results); 

但沒有用。如何轉換成

[{"value":"Montreal","label":"Montreal"},{"value":"New york","label":"New york"}] 

回答

0

我得到它的工作是這樣

$temp = array(); 
foreach($results as $row) 
{ 
    $value = $row["CityName"]; 
    array_push($temp, array(
     "label" => $value, 
     "value" => $value 
    )); 
} 

$data = $this->_helper->json($temp); 
$this->_helper->autoComplete($data); 

添加視圖助手的引導

Zend_Controller_Action_HelperBroker::addHelper(
    new ZendX_JQuery_Controller_Action_Helper_AutoComplete() 
); 
0

試試這個代碼

$results = $repo->getMatchingCityName($searchTerm); // [{"CityName":"Montreal"},{"CityName":"New york"}]........ 


$data = Zend_Json::decode($results); 

$new = array(); 

foreach ($data as $row) { 
    $temp['value'] = $row['CityName']; 
    $temp['label'] = $row['CityName']; 
    array_push($new, $temp); 
} 

$newEncode = Zend_Json::decode($new); //[{"value":"Montreal","label":"Montreal"},{"value":"New york","label":"New york"}] 
相關問題