elektroluse
elektroluse

Reputation: 29

AS3 Separating Arrays for different items

I have a function that creates a new value inside an associative array.

var array:Array = new Array(new Array());
var i : int = 0;

function personVelgLand(evt:MouseEvent)
{
    for(i = 0; i < personListe.dataProvider.length; i++)
    {
        if(txtPersonVelg.text == personListe.dataProvider.getItemAt(i).label)
        {
            personListe.dataProvider.getItemAt(i).reise = array;
            personListe.dataProvider.getItemAt(i).reise.push(landListe.selectedItem.land);
        }
    }
}

What happens is that the 'array' array which becomes 'personListe.dataProvider.getItemAt(i).reise' applies to every item in the list. I want it so that each time the function runs that the .reise only applies to the item chosen and not all of them.

EDIT:

I did this:

personListe.dataProvider.getItemAt(i).reise = new Array(array);

And now they are not the same but now each item in the list can not have multiple .reise values...

EDIT 2: dataProvider is nothing it would work just as fine without it. .reise is created in the function I originally posted it creates .reise in the object getItemAt(i).

personListe is a list which the users add their own items to by the use of a input textfield. It is given by this function:

function regPerson(evt:MouseEvent)
{
    regPersoner.push(txtRegPerson.text);

    personListe.addItem({label:regPersoner});

    regPersoner = new Array();
    txtRegPerson.text = "";
}

EDIT 3 : The user can register names which turn in to labels in a list. There is also list with 3 countries, Spain, Portugal and England. The user can then register a country to a person they select. Thats when I want to create the .reise inside the "name" items in the first list which contains the countries they have selected. I want every name to be able to select multiple countries which then will be created in the element .reise inside the item that holds their name. This would be easy if I could use strings. But later I plan to have the user type in a country and then something that would show every user that have selected that country. That is why the countries need to be stored as arrays inside the "name" items..

Upvotes: 1

Views: 79

Answers (1)

null
null

Reputation: 5255

You should first create a class for the User data that you are modelling. You already know all the properties.

  • The user can register names

  • The user can then register a country to a person they select.

  • able to select multiple countries

Such a class could look like this:

package
{
    public class User
    {
        private var _name:String;
        private var _countries:Array;
        
        public function User(name:String)
        {
            _name = name;
            _countries = [];
        }
        
        public function get name():String
        {
            return _name;
        }
        
        public function get countries():Array
        {
            return _countries;
        }
        
        public function set countries(value:Array):void
        {
            _countries = value;
        }
    }
}

Now create a DataProvider, fill it with objects of that class and use it for the list as described here:

import fl.controls.List; 
import fl.data.DataProvider; 
 
var users:List = new List(); 

users.dataProvider = new DataProvider([
                            new User("David"), 
                            new User("Colleen"), 
                            new User("Sharon"), 
                            new User("Ronnie"), 
                            new User("James")]); 
                            
addChild(users); 
users.move(150, 150);

In order to get a label from a User object, define a labelFunction

import fl.controls.List; 
import fl.data.DataProvider; 
 
var users:List = new List(); 

users.labelFunction = userLabelFunction;

function userLabelFunction(item:Object):String 
{
    return  item.name;
}

users.dataProvider = new DataProvider([
                            new User("David"), 
                            new User("Colleen"), 
                            new User("Sharon"), 
                            new User("Ronnie"), 
                            new User("James")]); 
                            
addChild(users); 
users.move(150,150);

This way you do not have to add a label property that you don't want in your class.


Selecting a name means selecting a user. The list of countries associated to the name should show up in a second List.

The DataProvider of that List remains constant, a list of all the available countries.

import fl.controls.List; 
import fl.data.DataProvider; 
 
 
// list of users

var users:List = new List(); 
addChild(users); 
users.move(150,150);

users.labelFunction = userLabelFunction;

function userLabelFunction(item:Object):String 
{
    return  item.name;
}

users.dataProvider = new DataProvider([
                            new User("David"), 
                            new User("Colleen"), 
                            new User("Sharon"), 
                            new User("Ronnie"), 
                            new User("James")]); 
                            

// lsit of countries

var countries:List = new List(); 
addChild(countries); 
countries.move(550,150); // adjut position as desired

countries.dataProvider = new DataProvider([
                            {label:"a"}, 
                            {label:"b"}, 
                            {label:"c"}, 
                            {label:"d"}, 
                            {label:"e"}, 
                            {label:"f"}]); 

Now all you have to do is to wire it all up. If a user is selected, select his countries in the countries list. If a country is selected, add that to the currently selected users list of countries. That could look somethign like this:

users.addEventLsitener(Event.CHANGE, onUserSelected);

function onUserSelected(e:Event):void
{
    countries.selectedItems = users.selectedItem.countries;
}

countries.addEventLsitener(Event.CHANGE, onCountrySelected);

function onCountrySelected(e:Event):void
{
    users.selectedItem.countries = countries.selectedItems;
}

The full code could look like this. I did not test this, but you get the idea.

// list of users

var users:List = new List(); 
addChild(users); 
users.move(150,150);

users.labelFunction = userLabelFunction;

function userLabelFunction(item:Object):String 
{
    return  item.name;
}

users.dataProvider = new DataProvider([
                            new User("David"), 
                            new User("Colleen"), 
                            new User("Sharon"), 
                            new User("Ronnie"), 
                            new User("James")]); 
                            

// list of countries

var countries:List = new List(); 
addChild(countries); 
countries.move(550,150); // adjut position as desired

countries.dataProvider = new DataProvider([
                            {label:"a"}, 
                            {label:"b"}, 
                            {label:"c"}, 
                            {label:"d"}, 
                            {label:"e"}, 
                            {label:"f"}]); 
                            
// events

users.addEventLsitener(Event.CHANGE, onUserSelected);

function onUserSelected(e:Event):void
{
    countries.selectedItems = users.selectedItem.countries;
}

countries.addEventLsitener(Event.CHANGE, onCountrySelected);

function onCountrySelected(e:Event):void
{
    users.selectedItem.countries = countries.selectedItems;
}

From what I understand this seems to work except for the fact that the names are already provided when the program starts. What I want is that the user adds the name themselves while the program is running.

You can add new items with the methods provided by the DataProvider class, like addItem() for example. Just add new User objects.

Upvotes: 2

Related Questions