03-25-2011 01:29 PM
Hey guys,
I am currently working on an app where I need to have a function that is able to remove Items from a Picker.
I have an array which includes all the items. And create the Picker similar to this example codes: Link
This is my basic approach code (does not work):
private function removeSubject(subject:String):void {
var newArrSubjects:Array = [];
for(var i:int=0; i<arrSubjects.length; i++){
if(!(arrSubjects[i].label.equals(subject)))
newArrSubjects.push({label: arrSubjects[i].label});
}
arrSubjects = newArrSubjects;
subjectPicker.dataProvider = createDataProviderSubject();
}
My problem is that I don't know how to really to the test of Equality or how to get the contents of the "label".
I have a function to add an item to the Picker working like that:
private function addSubject(subject:String):void {
arrSubjects.push({label: subject});
subjectPicker.dataProvider = createDataProviderSubject(); }The createDataProviderSubject() uses the arrSubjects as their Source to create the DataProvider.
Would be very pleased if anyone had a great idea to help me out ![]()
Solved! Go to Solution.
03-25-2011 02:30 PM
hey,
try running the following code instead and see what you get:
private function removeSubject(subject:String):void
{
for(var i:int=0; i < arrSubjects.length; i++)
{
if(arrSubjects[i].label == subject)
{
/**
* use the splice method of the array
* to delete the object based on the index
*
*/
newArrSubjects.splice(i,1);
/** if there is a match break out of for loop */
break;
}
}
subjectPicker.dataProvider = createDataProviderSubject();
}
This time we are using the same array and simply removing the object u dont want anymore. hope that clears things up. good luck!
03-25-2011 04:20 PM
wow, thank you so much!
03-26-2011 10:15 AM - edited 03-26-2011 10:38 AM
Still one issue.
I get an error when trying to delete the last item (the one at arrSubject.length-1) of the Picker / Array.
This is what I get:
But the Array / the Picker contains 10 items, so index 9 should be just fine for the last element :/
03-26-2011 10:28 AM - edited 03-26-2011 10:31 AM
I also rewrote the removeSubject() method.
This time I directly take the index currently selected by the Picker and delete this index in the Array.
private function removeSubject():void {
var index:int = subjectPicker.selectedIndices[0];
arrSubjects.splice(index,1);
subjectPicker.dataProvider = createDataProviderSubject();
}
To make sure it's not the splice method causing the error I also tried this one:
private function removeSubject():void {
var index:int = subjectPicker.selectedIndices[0];
if(index == arrSubjects.length-1){
arrSubjects.pop();
} else {
arrSubjects.splice(index,1);
}
subjectPicker.dataProvider = createDataProviderSubject();
}
But i get the same error message...
03-26-2011 11:12 AM
Ok, i got it solved.
The selectedIndices of the picker needed to be set to a valid value before deleting the item and creating a new DataProvider.