سؤال

I have JSON data returned in my action controller:

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

Response that i'm getting:

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

But jquery ui autocomplete doesn't show anything

tried

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

and

Zend_Json::encode($results);

But no use. How do I convert into

[{"value":"Montreal","label":"Montreal"},{"value":"New york","label":"New york"}]
هل كانت مفيدة؟

المحلول 2

I got it working like this

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

Added the view helper in bootstrap

Zend_Controller_Action_HelperBroker::addHelper(
    new ZendX_JQuery_Controller_Action_Helper_AutoComplete()
);

نصائح أخرى

Try this code

$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"}]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top