Skip to main content
Known Participant
April 9, 2018
Answered

How to search a data grid that is loading a csv file?

  • April 9, 2018
  • 1 reply
  • 1292 views

Hello all.  I am loading a csv file into a datagrid.  Is there a way to setup a text input to search and display the desired results?

For ex.  I have a long list of addresses which is the csv file that is being loaded into the data grid.  I want to be able to search for zip codes and only display those addresses with that zip code.

Thanks for any help.

This topic has been closed for replies.
Correct answer kglad

This has been a few months since I have started this journey.

I am pulling in the csv file correctly.

I have an input text field that I would like to use to search and list zip codes.

You had posted a search function

function zipF(zip:String):DataProvider{

var dp_zipA:Array=[];

for(var i:int=0;i<dpA.length;i++){

if(dpA.Zip==zip){

dp_zipA.push({"Business":dpA.Business,"Name":dpA.Name,"Address":dpA.Address,"City ":dpA.City,"State":dpA.State});

}

}

return new DataProvider(dp_zipA);

}

I am trying to understand it.

Do I give the input text field the property name zipF?  Or create a sep button?

Again I am sorry for being such a noob.  But this is very exciting!


you call the zipF and pass the zip code (as a string).  zipF returns a dataprovider of matching zip codes.  each object contains all the data you showed was in your csv file.

if your input textfield is input_tf, you could use:

somebtn.addEventListener(MouseEvent.CLICK,searchF);

function searchF(e:MouseEvent):void{

// i assume you want to display this in datagrid, eg dg

dg.dataProvider=zipF(input_tf.text);

}

1 reply

kglad
Adobe Expert
April 10, 2018

yes, but exactly how you do that depends on your csv file structure and possibly on how you parse that file with as3.

without knowing that info, there's a fair chance the indexOf() method of strings and of arrays would be helpful.

dubsdubsAuthor
Known Participant
April 10, 2018

Would it help to show you how Im pulling in the csv file?

kglad
Adobe Expert
April 10, 2018

Here is the as3 Im using.

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE,completeF);

urlLoader.load(new URLRequest("cust-list-delim.csv"));

var dg:DataGrid

function completeF(e:Event):void{

    var dataS:String = e.target.data;

    var dataA:Array = dataS.split("\n").join("").split("\r");

    var dpA:Array = [];

    var itemA:Array;

    for(var i:int=0;i<dataA.length;i++){

        itemA = dataA.split(",");

        dpA.push({"Business":itemA[1],"Name":itemA[2],"Address":itemA[3],"City":itemA[4],"State":itemA[5],"Zip":itemA[6]});

    }

    var dp:DataProvider = new DataProvider(dpA);

    dg.columns = ["Business","Name","Address","City","State","Zip"]

    dg.dataProvider = dp;

}


to search for a particular zip, call zipF:

var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,completeF);
urlLoader.load(new URLRequest("cust-list-delim.csv"));
var dg:DataGrid;
var dpA:Array = []; 
function completeF(e:Event):void{
     var dataS:String = e.target.data;
    var dataA:Array = dataS.split("\n").join("").split("\r"); 
   var itemA:Array;    
for(var i:int=0;i<dataA.length;i++){ 
       itemA = dataA.split(",");   
     dpA.push({"Business":itemA[1],"Name":itemA[2],"Address":itemA[3],"City":itemA[4],"State":itemA[5],"Zip":itemA[6]}); 
   }   
var dp:DataProvider = new DataProvider(dpA);
    dg.columns = ["Business","Name","Address","City","State","Zip"] 
   dg.dataProvider = dp;
}

function zipF(zip:String):DataProvider{

var dp_zipA:Array=[];

for(var i:int=0;i<dpA.length;i++){

if(dpA.Zip==zip){

dp_zipA.push({"Business":dpA.Business],"Name":dpA.Name,"Address":dpA.Address,"City":dpA.City,"State":dpA.State});

}

}

return new DataProvider(dp_zipA);

}