View Full Version : Remove an item on a list

09-06-2006, 12:04 AM
Hi, how can I remove an item on a list just by knowing the label, not the index..? I've tried to read the reference but can't find a way to do this.

09-06-2006, 01:34 AM
Hey, Brianng,

A good way of maintaining a list is to create an array, then set it as the dataProvider for your list component. You can do a lot of things with arrays: add items, delete items, search for items, etc. Then when you want the list to reflect those changes, just set it as the dataProvider again, to force it to update itself. So here's how it would work:

Drag out an instance of the List component. Give it an instance name of myList. Create an actions layer, click on frame 1, press F9, and enter this:

var myArray:Array = new Array("Mike", "Bill", "Andrew", "Cynthia", "Sue");
myList.dataProvider = myArray;

After you enter that, and you test the movie, you'll find that the List component has picked up the contents of your array. Next, here's a function to search for and remove a name (add this to your code):
function removeName(name) {
for(var i:Number = 0; i < myArray.length; i++) {
if (myArray[i] == name) {
myArray.splice(i, 1);
myList.dataProvider = myArray;
//now call the function:

With a little Yankee ingenuity, you can easily make a function that adds names (using either "push()" or "shift()" to add items to the beginning or end of the array--read up on arrays)--buttons and input boxes that let you input names and trigger the functions, etc, etc.

Of course, there's more than one way to do any given task, so what I've given you as an answer is what I've tried, and found to be a really cool, easy way. Hope that helps.

09-06-2006, 04:12 AM
Hi Mazoonist,

Thanks for the help :) I'm new to Flash and it's kinda surprised me that the List component does not has that function built in :D

09-06-2006, 12:28 PM
Well, it does. You probably saw the "removeItemAt()" function. But you've got to supply the index of the item. So I'm not really sure how you'd search the items by their label, although I suppose with some digging through the properties, it could be done. But there's nothing wrong with the way I outlined, in fact, I'm sure that's what they intended when they provided a "dataProvider" property.

Although I haven't looked into the internal workings of the List component, I'm sure that somewhere in there is a property that's an array of the list items. So when you issue the command:

myList.dataProvider = someArray;

Internally the List component takes your array and makes it equal to it's internal one. So you are effectively manipulating the internal Array of List items. And all you really need to do is create a variable to do it with. An Array is a variable, after all.

09-06-2006, 12:54 PM
I worked out how you would do it without using a separate array:

function removeName(name) {
for(var i = 0; i < myList.length; i++) {
if(myList.getItemAt(i).label == name) {

09-06-2006, 02:39 PM
Oh I meant the function to get the index just by knowing the label :)
I've used Borland Delphi and for List component, it has something like this: List.indexOf("label") to get the index of an item. Thanks again for your help :)