Hello dear people
, I have a question, I want to filter an xml data which is loaded in a datagrid, but I want to filter this data for search the names
Is it posible filter in a text input?, I have search it in many ways, but there is not found much about it. this is my code. Thanks in advance for any help.
Regards
Joselyn
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.TextInput;
import fl.data.DataProvider;
import flash.net.URLRequest;
import flash.net.URLLoader;
var dp:DataProvider = new DataProvider();
//Create and populate a new DataProvider object.
var itemTextInput:TextInput = new TextInput();
itemTextInput.addEventListener(Event.CHANGE, changeHandler);
var colTF:TextFormat = new TextFormat();
colTF.bold = false;
colTF.indent = 5;
colTF.size = 11;
colTF.color = 0x000000;
colTF.font = "Verdana";
var col2TF:TextFormat = new TextFormat();
col2TF.bold = false;
col2TF.indent = 5;
col2TF.size = 11;
col2TF.color = 0x000000;
col2TF.font = "Verdana";
var dg:DataGrid = new DataGrid();
dg.setSize(800, 200);
dg.move (0, 100);
var nameCol:DataGridColumn = new DataGridColumn("names");
nameCol.headerText = "Name ";
var descCol:DataGridColumn = new DataGridColumn("address");
domCol.headerText = "Address";
dg.addColumn(nameCol);
dg.addColumn(domCol);
dg.setRendererStyle("textFormat", colTF);
dg.setRendererStyle("textFormat", col2TF);
dg.rowHeight = 22;
dg.headerHeight = 30;
addChild(dg);
var xml:XML;
var url:String = "xmls/othersnames.xml";
var urlXML:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(urlXML);
function Cargar(event):void
{
var xml:XML = new XML (loader.data);
var dp:DataProvider = new DataProvider(xml);
dg.dataProvider = dp;
dp.sortOn("name", Array.CASEINSENSITIVE);
dg.rowCount = dp.length;
}
loader.addEventListener(Event.COMPLETE, Cargar);
// textInput changeHandler function
function changeHandler(event:Event):void {
var xml:XML = new XML (loader.data);
var dp:DataProvider = new DataProvider(xml);
dg.dataProvider = dp;
var arr:Array = dp.toArray();
var filteredArr:Array = arr.filter(filterDataProvider);
dg.dataProvider = new DataProvider(filteredArr);
}
function filtering(obj:Object, idx:int, arr:Array):Boolean
{
var txt1:String = itemTextInput.text;
var txt2:String = obj.name.substr(0, txt1.length);
if (txt1.toLowerCase() == txt2.toLowerCase()) {
return true;
}
return false;
}
Hello Kglad:
Thanks again for your great help, this is part of the xml it has as 100 names, but I send only this little part.
have a nice affternoon.
<?xml version="1.0" encoding="UTF-8" ?>
<othername>
<name name="Sebastian Gonzalez Cruz" address="Coronel Calderon 412 Grado: Segundo"/>
<name name="Clara Salazar." address="Bolivia 1261 Grado: Cuarto"/>
<name name="Gustavo Ramirez Tellez" address="Dolores del R. 115 Grado: primero"/>
<name name="Gerardo Novela Cuellar" address="Lazaro Cardenas Grado: Quinto"/>
</othername>
if you want to filter on the name attributes, use:
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name.(@name.indexOf(whateverfilteringtext)>-1));
var dp:DataProvider = new DataProvider([xml@name,xml@address]); // you may want to switch rows and columns which would look like:
/*
var a:Array = [];
for(var i:int=0;i<xmlList.length();i++){
a[i] = [xmlList[i].@name,xmlList[i].@address]
}
var dp:DataProvider=new DataProvider(a);
*/
dg.dataProvider = dp;
}
HELLO KGLAD:
Excuse me, I was confused, to understand this code I used the first function "changHandler"
like this, then it call is "whateverfilteringtext" function?,
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name(@name.indexOf(whateverfilteringtext)>-1));
var dp:DataProvider = new DataProvider([xml@name,xml@address]);
}
dg.dataProvider = dp;
// Is this function like this ?
function whateverfilteringtext(obj:Object, idx:int, arr:Array):Boolean
{
var txt1:String = itemTextInput.text;
var txt2:String = obj.name.substr(0, txt1.length);
if (txt1.toLowerCase() == txt2.toLowerCase()) {
return true;
}
return false;
}
I was tried to test but it mark this error:
1084: Error de sintaxis: se esperaba rightbracket antes de ampersand, in the next line
"var dp:DataProvider = new DataProvider([xml@name,xml@address]); "
I hope don't bother you but I am a very beginner with flash as3. ![]()
Thanks a lot
Regards.
Joselyn
whateverfilteringtext is not a function. it's the string you want to use as your filter. but you could use a function if it returns a string.
from the look of your code that would be something like itemTextinput.text.
i'm not sure what you're trying to do with your test of txt1.toLowerCase() and txt2.toLowerCase().
Hi, Kglad :
My intention, is to display a large list of names, if somebody search an specific name will do it in a TextInput wrinting the first and/
or the second letters of the name then the datagrid will display the names with the two specific first letters, is similar as I saw in the datagrid tutorials, but it does not load an xml.
Well, in the code you send me where says "whateverfilteringtext" instead is "name" or "address"??
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name.(@name.indexOf(name)>-1));
var dp:DataProvider = new DataProvider([xml@name,xml@address]); //
}
After this function the other will be if the TextInput return something true or false nothing, can I use this function?
function filterTextinput(obj:Object, idx:int, arr:Array):Boolean
{
var txt1:String = itemTextInput.text;
var txt2:String = obj.name.substr(0, txt1.length);
if (txt1.toLowerCase() == txt2.toLowerCase()) {
return true;
}
return false;
}
Thanks again for your great help.![]()
Nice day!
Regards
Joselyn
if TextInput is your input textfield that you want to use to filter your xml, use the following. don't change the code. just copy and paste it.
the only thing you should change is TextInput if that's not the name of your input textfield;
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name.(@name.indexOf(TextInput.text)>-1));
var dp:DataProvider = new DataProvider([xml@name,xml@address]); // you may want to switch rows and columns which would look like:
/*
var a:Array = [];
for(var i:int=0;i<xmlList.length();i++){
a[i] = [xmlList[i].@name,xmlList[i].@address]
}
var dp:DataProvider=new DataProvider(a);
*/
dg.dataProvider = dp;
}
// don't worry about upper and lower case or whether the user's input matches the start of the xml name, yet. just get the basics working. then you can fine tune the code.
Good Day Kglad: ![]()
Excuse, I do what you said, I copy and paste but it gave an error:
1084: Syntax error: expecting rightbracket before ampersand. Line 93
THIS IS THE LINE:
var dp:DataProvider = new DataProvider([xml@name,xml@address]);
// textInput changeHandler function
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name.(@name.indexOf(itemTextInput.text)>-1));
var dp:DataProvider = new DataProvider([xml@name,xml@address]);
// you may want to switch rows and columns which would look like:
/*
var a:Array = [];
for(var i:int=0;i<xmlList.length();i++){
a[i] = [xmlList[i].@name,xmlList[i].@address]
}
var dp:DataProvider=new DataProvider(a);
*/
dg.dataProvider = dp;
}
Thank you in advance Kglad
Regards
Joselyn
use:
// textInput changeHandler function
function changeHandler(event:Event):void {
var xml:XMLList = XMLList(loader.data.name.(@name.indexOf(itemTextInput.text)>-1));
var dp:DataProvider = new DataProvider([xml.@name,xml.@address]);
// you may want to switch rows and columns which would look like:
/*
var a:Array = [];
for(var i:int=0;i<xmlList.length();i++){
a[i] = [xmlList[i].@name,xmlList[i].@address]
}
var dp:DataProvider=new DataProvider(a);
*/
dg.dataProvider = dp;
}
Good Day Kglad:![]()
Thank you for your commens, I assume you mean this code is correct?
You can see it, it is in the first code that I send to you.
var itemTextInput:TextInput = new TextInput();
itemTextInput.addEventListener(Event.CHANGE, changeHandler);
So I tried to trace the changeHandler but maybe I cannot to do that sorry![]()
it gave me errors.
maybe it can something like this
trace(itemTextInput.event.CHANGE) ?????
thankyou for your patience again
Have nice day!!
Joselyn
what's the trace output from:
function changeHandler(event:Event):void {
trace(itemTextInput.text)
var xml:XML = XML(loader.data);
var xmlList:XMLList = XMLList(xml.name.(@name.indexOf(itemTextInput.text)>-1));
var a:Array = [];
for(var i:int=0;i<xmlList.length();i++){
a[i] = {names: xmlList[i].@name, address: xmlList[i].@address};
}
trace(a.length)
var dp:DataProvider=new DataProvider(a);
dg.dataProvider=dp;
}
HI KGLAD :![]()
kglad I'm very sorry to you, but I regret to say that do not throw anything the "trace" and any error either, I copy and paste the code exactly as you sent me, only loads the xml datagrid.
Excuse me please
Could you say me what's wrong? Do you belive that the xml is bad? ![]()
thanks a lot.
Have nice day!
Joselyn
North America
Europe, Middle East and Africa
Asia Pacific