将数据推送到 Dojo Filteringselect(带代码)

Posted

技术标签:

【中文标题】将数据推送到 Dojo Filteringselect(带代码)【英文标题】:Pushing data to Dojo Filteringselect (with code) 【发布时间】:2011-11-12 21:37:02 【问题描述】:

我对 Zend 和 JSON 很陌生,但是我需要学习。我想要实现的是:拥有一个 Dojo 过滤选择(带有自动完成功能)控件,该控件链接到数据库中的邮政编码(并跟踪 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"

【问题讨论】:

【参考方案1】:

我认为你有几个选择:

您可以控制在控制器上生成的 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 >1.6,你可以使用 dojo.store.JsonRest 和它的配套 dojo.data.ObjectStore 描述here

选项 1 显然是最简单的...

【讨论】:

以上是关于将数据推送到 Dojo Filteringselect(带代码)的主要内容,如果未能解决你的问题,请参考以下文章

对 Dojo 商店物品进行排序

将虚拟数据推送到服务器

通过 Autodesk Forge 将数据推送到云端

facebook 如何将数据推送到新闻提要?

将数据从多个输入推送到状态数组

将json数据推送到angular js中的现有数组