ActionScript - 实时搜索和更新数据提供者列表?
我想将实时搜索添加到一个列表,该列表的数据提供者是一个非常大的 XML。为简单起见,我们假设我的 XML 只是世界上 180 个国家/地区的列表:
package
{
//Imports
import fl.controls.List;
import fl.data.DataProvider;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
//Class
public class LiveSearchXMLList extends Sprite
{
//Variables
private var XMLData:XML;
private var dp:DataProvider;
private var list:List;
//Constructor
public function LiveSearchXMLList()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
//Initialization
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Download XML File
var XMLLoader:URLLoader = new URLLoader();
XMLLoader.addEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
XMLLoader.addEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
XMLLoader.load(new URLRequest( /* COUNTRY_LIST_XML_FILE */ ));
}
//XMLLoader Error Handler
private function IOEventErrorHandler(evt:IOErrorEvent):void
{
//Remove Event Listeners
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
evt.target.removeEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
//Throw Error
throw(evt.text);
}
//XMLLoader Complete Handler
private function XMLLoaderCompleteEventHandler(evt:Event):void
{
//Remove Event Listeners
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
evt.target.removeEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
//Assign XMLData
XMLData = new XML(evt.target.data);
createList();
}
//List
private function createList():void
{
//Assign And Alphabetize Data
dp = new DataProvider(XMLData);
dp.sortOn("countryName");
//Create List Object
list = new List();
list.width = 400;
list.height = 400;
list.x = list.y = 25;
list.labelField = "countryName";
list.dataProvider = dp;
}
}
}
该列表的 labelFields 由 XML 元素的
填充并按字母顺序排列。
我想创建一个输入文本字段,该字段将侦听击键并根据与countryName 元素值匹配的输入字符串更新列表。因此,如果我在文本字段中输入“can”,列表将突然减少到只有 5 行:
- 美属萨摩亚
- 加拿大
- 中非共和国
- 多米尼加共和国
- 梵蒂冈城
当然,实时搜索也应该是非破坏性的 - 按删除一次,这样搜索字段现在显示为“ca”,这会将 5 行列表增加到 21 行。 (南极洲、牙买加等)
这是怎么做到的?实时搜索和更新 XML 提供的列表对象的最快或最常见的方法是什么?
i want to add live searching to a list whose data provider is a very large XML. for simplicity, let's assume my XML is just a list of the world's 180ish countries:
package
{
//Imports
import fl.controls.List;
import fl.data.DataProvider;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
//Class
public class LiveSearchXMLList extends Sprite
{
//Variables
private var XMLData:XML;
private var dp:DataProvider;
private var list:List;
//Constructor
public function LiveSearchXMLList()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
//Initialization
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Download XML File
var XMLLoader:URLLoader = new URLLoader();
XMLLoader.addEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
XMLLoader.addEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
XMLLoader.load(new URLRequest( /* COUNTRY_LIST_XML_FILE */ ));
}
//XMLLoader Error Handler
private function IOEventErrorHandler(evt:IOErrorEvent):void
{
//Remove Event Listeners
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
evt.target.removeEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
//Throw Error
throw(evt.text);
}
//XMLLoader Complete Handler
private function XMLLoaderCompleteEventHandler(evt:Event):void
{
//Remove Event Listeners
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, IOEventErrorHandler);
evt.target.removeEventListener(Event.COMPLETE, XMLLoaderCompleteEventHandler);
//Assign XMLData
XMLData = new XML(evt.target.data);
createList();
}
//List
private function createList():void
{
//Assign And Alphabetize Data
dp = new DataProvider(XMLData);
dp.sortOn("countryName");
//Create List Object
list = new List();
list.width = 400;
list.height = 400;
list.x = list.y = 25;
list.labelField = "countryName";
list.dataProvider = dp;
}
}
}
the list's labelFields are populated by the XML element's <countryName>
and alphabetized.
i would like to create an input text field that will listen for keystrokes and update the list according to the input string matching the countryName element values. therefore, if i enter "can" in the text field, the list will suddenly be reduced to only 5 rows:
- American Samoa
- Canada
- Central African Republic
- Dominican Republic
- Vatican City
of course, live search should be non destructive as well - press delete once so the search field now reads "ca", which increases the 5 row list to 21 rows. (Antarctica, Jamaica, etc.)
how is this done? what is the fastest or most common approach for live searching and updating an XML provided list object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就个人而言,除非您有理由将数据保留为 xml,否则我会首先将 xml 列表转换为对象的 arrayCollection。如果您在加载数据时这样做,它应该会给您带来更好的整体性能,尤其是对于较大的数据集。
但为了回答你的问题,我会这样做:(这假设你将 xml 数据转换为一个名为 _acData 的 arrayCollection,并且你有另一个名为 _acFilteredData 的 arrayCollection,这是列表控件所绑定的。一旦你将数据加载到_acData,将其复制到 _acFilteredData 中,然后将这样的函数附加到您的文本输入中)
Personally, unless you have a reason to keep your data as xml, I would transform the xml list into an arrayCollection of objects first. If you do that when you load the data it should give you some better performance overall, especially with larger data sets.
But to answer your question, I would do something like this: (this assumes you transform the xml data into an arrayCollection called _acData and you have another arrayCollection called _acFilteredData that is what the list control is bound to. Once you load up the data into _acData, copy it into _acFilteredData and then attach a function like this to your text input)