1

我的动作控制器中返回了 JSON 数据:

$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"}]
4

2 回答 2

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"}]
于 2011-11-18T06:34:58.190 回答
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()
);
于 2011-11-21T02:11:52.493 回答