简体   繁体   English

使用异步数据更新Dojo Dijit FilteringSelect的商店

[英]Updating Dojo Dijit FilteringSelect's store with asynchonous data

I'm using Zend + Dojo form in an intranet context. 我在Intranet上下文中使用Zend + Dojo表单。 We need to update a list of counties when the selected state is changed on a form. 当表单上的选定状态更改时,我们需要更新县列表。 With HTML you would just parse the json response (or just provide a html fragment of options) and update the innerhtml of the select. 使用HTML,您只需解析json响应(或仅提供选项的html片段)并更新select的innerhtml。 However, doing so in Dojo has proved to be prohibitively complex. 但是,事实证明,在Dojo中这样做非常复杂。

In short, I have defined a generic method that allows you to set an onBlur update to a form field. 简而言之,我定义了一个通用方法,该方法允许您将onBlur更新设置为表单字段。 This links to a Controller action that calls the database with a function arguments, and gets back either a singleton response (which we handle okay already) or a list response. 这链接到Controller动作,该动作使用函数参数调用数据库,并获取单例响应(我们已经处理好了)或列表响应。 If we get back a list, we need to alter the options in a select with that list. 如果返回列表,则需要更改带有该列表的select中的选项。

All of this works fine so far; 到目前为止,所有这些工作都很好。 we get a valid json reponse with a nice list in response.data. 我们得到一个有效的json响应,并在response.data中列出了一个不错的列表。 Here's where the problem is. 这就是问题所在。 How can I simply update the existing store (examination reveals it is a _comboBoxDataStore)? 如何简单地更新现有商店(检查发现它是_comboBoxDataStore)? I cannot declare the select with a different kind of store, since it is auto-generated. 我无法使用其他类型的商店来声明选择,因为它是自动生成的。

Is there any way to brute force a swap of stores? 有没有办法强行换购商店? The main key is that whatever I do it has to work in Internet Explorer; 主要的关键是我所做的一切都必须在Internet Explorer中起作用。 My use of Firefox is only to allow better insight into the objects I'm working with. 我使用Firefox只是为了更好地了解正在使用的对象。

As a side question, does anyone know why _comboBoxDataStore cannot be updated? 作为附带的问题,有人知道为什么_comboBoxDataStore无法更新吗? Other frameworks allow for pretty seamless updating of stores, like for instance, extJS. 其他框架允许对商店进行相当无缝的更新,例如extJS。 Is there any reasoning behind designing a limitation into the combobox so it cannot be updated, which is a step down from raw HTML? 在组合框中设计一个限制以使其无法更新,这比原始HTML少了些什么? Or am I missing the obvious solution that doesn't involve juggling a list of esoteric objects? 还是我错过了不涉及杂耍深奥对象的明显解决方案?

Based on post of RiverC my method for updating the combo works fine like this: 基于RiverC的帖子,我的更新组合的方法可以正常运行,如下所示:

, updateData : function(data) {
    var storeData = {data: data, id:'attrFilterStore'};
    var newStore = new dojo.data.ItemFileWriteStore({data: data});
    this.element.store = newStore;
    this.element.startup();
}

Where " this.element " is a reference to the FilteringSelect object. 其中“ this.element ”是对FilteringSelect对象的引用。
The method " updateData " is a callback function from an ajax request which receives JSON with the structure RiverC posted. 方法“ updateData ”是来自ajax请求的回调函数,该请求接收具有RiverC发布结构的JSON。

You can even use options.. 您甚至可以使用选项。

var fldSelect = dijit.byId("fieldSelect");
fldSelect.options = fldoptions;
fldSelect.startup();

Where fldoptions is the new data, which needs to be populated. fldoptions是需要填充的新数据。 Keep in mind: DON'T FORGET TO CALL fldSelect.startup() , else the data will not get updated. 请记住:不要忘记调用fldSelect.startup() ,否则数据将不会更新。

As it so happens, this IS possible, though no tutorial I have seen shows how easy it actually is. 碰巧的是,这是有可能的,尽管我所见过的任何教程都没有显示出实际上是多么容易。

Make sure this is somewhere: 确保这是某处:

dojo.require("dojo.data.ItemFileWriteStore");

Then, regardless of how your filteringSelect came into being you can do the following: 然后,无论您的filterSelect如何出现,您都可以执行以下操作:

newStore = new dojo.data.ItemFileWriteStore({
    data:{
         identifier: 'id',
         label: 'name',
         items: response.data
    }
});

filteringSelect.store = newStore;
filteringSelect.startup();

If you have control on the creation of the _comboBoxDataStore, you can set the clearOnClose to true on that store. 如果可以控制_comboBoxDataStore的创建,则可以在该商店上将clearOnClose设置为true。 You can then close that store and set the url or data attribute to your new data (note that using data attribute after clearOnClose is supported only with dojo 1.4 and later). 然后,您可以关闭该存储并将url或data属性设置为新数据(请注意,仅dojo 1.4及更高版本支持在clearOnClose之后使用data属性)。 THis will automatically update the select/combobx widget with the new data 这将使用新数据自动更新select / combobx小部件

for an example, see: http://soularis999.blogspot.com/2011/03/reloading-dojodataitemfilereadstore.html 有关示例,请参阅: http : //soularis999.blogspot.com/2011/03/reloading-dojodataitemfilereadstore.html

If you are unable to set the clearOnClose (due to the Zend framework owning it without option to modify that behavior), you can create a new datastore and set the combobox's datastore to the newly created datastore. 如果您无法设置clearOnClose(由于Zend框架拥有它,而没有选择修改该行为的选项),则可以创建一个新的数据存储并将组合框的数据存储设置为新创建的数据存储。 dijit filteringselect and combobox provides a store attribute that you can modify. dijitfilteringselect和combobox提供了可以修改的存储属性。

As an altogether different approach, you may want to explore using the "query" attribute of the dojo filtering widgets for such usecases - they are very helpful and reduce the amount of code and management you need to do. 作为一种完全不同的方法,您可能希望针对此类用例使用dojo过滤小部件的“ query”属性-它们非常有用,可减少所需的代码和管理量。

See the url below for an example of how the state is changed based on country selection by modifying the query filter of the filteringselect http://livedocs.dojotoolkit.org/dijit/form/FilteringSelect 请参阅下面的URL,以获取有关如何通过修改filterselect的查询过滤器来基于国家/地区选择来更改状态的示例。http://livedocs.dojotoolkit.org/dijit/form/FilteringSelect

Also, from your usecase description, it sounds like dijit filteringSelect might be a better choice of widget than the combobox (since it is a list of counties, do you want user to be able to enter any county not present in the list)? 另外,从您的用例描述中,听起来好像dijit filterSelect可能是一个比组合框更好的窗口小部件选择(由于它是县列表,您是否希望用户能够输入列表中不存在的任何县)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM