我对Zend& JSON,但我需要学习。我想要实现的是:使用Dojo filteringselect(带自动完成)控件,该控件链接到数据库中的zipcodes(并且跟踪ID,因此我可以将该ID作为FK存储在另一个表中(稍后)。结构是MVC。我确实得到了数据库的结果,但是我似乎无法使它发光。在filteringselect控件中没有显示任何东西。所以基本上我的数据库的结构字段需要进入filteringsselect控件并保持跟踪那个id,因为我以后需要它作为另一个表中的FK。 请帮帮我!
表:
<?php
class Application_Model_Place extends FS_Model_Popo {
protected $_fields = array(
'id' => NULL,
'zip' => NULL,
'name' => NULL,
'up' => NULL,
'structure' => NULL);
protected $_primaryKey = array('id');
}
形式:
$place = new Zend_Dojo_Form_Element_FilteringSelect('Place');
$place->setLabel('Place')
->setAttrib('title', 'Add a place.')
->setAutoComplete(true)
->setStoreId('placeStore')
->setStoreType('dojox.data.QueryReadStore')
->setStoreParams(array('url' => '/graph/place/autocomplete'))
->setAttrib("searchAttr", "structure")
->setRequired(true);
控制器:
class Graph_PlaceController extends Zend_Controller_Action {
public function autocompleteAction() {
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$structuur = $this->_getParam("structure", "");
$results = FS_Model_Factory::getInstance()->place->autocomplete(array('structure like ?'=> "%".$structure."%"));
$enc_res = array();
foreach ($results as $value) {
array_push($enc_res,$this->_helper->convert->toArray($value));
}
$this->_helper->json($enc_res);
$data = new Zend_Dojo_Data('id', $enc_res);
$this->_helper->autoCompleteDojo($data);
}
}
json($ enc_res)的一个例子是:
{"id":"235","zip":"3130","name":"Betekom","up":"BETEKOM","structure":"3130 Betekom"}, {"id":"268","zip":"3211","name":"Binkom","up":"BINKOM","structure":"3211 Binkom"},{"id":"377","zip":"3840","name":"Broekom","up":"BROEKOM","structure":"3840 Broekom"},{"id":"393","zip":"1081","name":"Brussel (Koekelberg)","up":"BRUSSEL (KOEKELBERG)","structure":"1081 BRUSSEL (KOEKELBERG)"},{"id":"421","zip":"1081","name":"Bruxelles (Koekelberg)","up":"BRUXELLES (KOEKELBERG)","structure":"1081 BRUXELLES (KOEKELBERG)"},{"id":"668","zip":"3670","name":"Ellikom","up":"ELLIKOM","structure":"3670 Ellikom"},{"id":"1236","zip":"3840","name":"Jesseren (Kolmont)","up":"JESSEREN (KOLMONT)","structure":"3840 Jesseren (Kolmont)"},{"id":"1275","zip":"3370","name":"Kerkom","up":"KERKOM","structure":"3370 Kerkom"}
答案 0 :(得分:0)
我认为你有几种选择:
您可以控制在控制器上生成的json的结构,因此您应该生成dojox.store.QueryReadStore所期望的格式(默认情况下与dojo.data.ItemFileReadStore相同) 。请参阅http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html#general-structure
或者您创建了一个了解json响应结构的自定义商店。请参阅http://dojotoolkit.org/reference-guide/dojox/data/QueryReadStore.html#query-translation
或者您使用的是dojo&gt; 1.6,您可以使用dojo.store.JsonRest及其配套的dojo.data.ObjectStore,如上所述here
选项1显然是最简单的......