View Full Version : Reordering in list component
rondog
December 1st, 2008, 08:47 PM
I have a list component with some items in it. I have two movie clips (up and down arrows). I need the ability to have the user choose an item and when they hit up or down, it moves it down or up in the list. Is that possible? Anyone know of something like this already made? Thanks.
scottc
December 1st, 2008, 09:00 PM
Is that possible?
Yes, very possible...
I'ld start with an array and start playing with the methods and try to reorder the elements. like splice etc
Then make the gui...
Anyone know of something like this already made?
I haven't seen anything, but i haven't looked for that sort of thing... I'm sure google might list an example or 2.
rondog
December 2nd, 2008, 01:05 PM
I was thinking this last night. I was just using addItem based off the users input, but I moved the items to an array then did addItem. So I now have an array of all the data. I haven't found an array funtion that 'swaps' items, but I have my up and down button functions almost doing what I want. Here is the code:
private function moveItemUp(e:MouseEvent):void
{
var index:Number = p.playList.customList.selectedIndex;
if(index != 0)
{
trace("swap " + index + " with " + (index - 1));
}
else
{
trace("already at the top");
}
}
private function moveItemDown(e:MouseEvent):void
{
var index:Number = p.playList.customList.selectedIndex;
if((customData.length - 1) != index)
{
trace("swap " + index + " with " + (index + 1));
}
else
{
trace("already at the end");
}
}
So I have the current selected index of the array(or list item) and the item that it needs to swap with. now how would I actually swap them?
scottc
December 2nd, 2008, 01:37 PM
I haven't found an array funtion that 'swaps' items.
Then make one...
//swap items 3 & 4
i = 3
temp = myarray[i]; //store it
myarray[i] = myarray[i+1]; //move the 1st element
myarray[i+1] = temp; //restore the temp element into the new location
rondog
December 2nd, 2008, 01:44 PM
I made one myself actually, but yours looks more compact:
private function swap(array,index1,index2):Array
{
var firstRep:Object = array[index1];
var secondRep:Object = array[index2];
var output:Array = new Array();
for (var ar:* in array)
{
if (ar == index1)
{
output[index1] = secondRep;
}
else if (ar == index2)
{
output[index2] = firstRep;
}
else
{
output[ar] = array[ar];
}
}
return output;
}
scottc
December 2nd, 2008, 01:53 PM
But yours can swap even if they are not next to each other.
rondog
December 2nd, 2008, 02:04 PM
Ahh true..
I am adding a title feature where they will be able to insert before/after an item in the list so swapping if they aren't next to each other will come in handy then =)
This is basically a video editor where they take a 2 hour long teaching session and the teacher can select only parts they want to show. This is turning into a nice little app :D I wish I could show someone, but my work would not like that ha ha
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.