Move items up or down in list box

Hello,

I have an application that inserts data into a list box. The results are retrieved in an array, and want to be able to add arrow buttons that will allow the data in the array to be rearranged (moved up or down into a desired order). Can anyone give me advice as to the best way to do this?
liltygaAsked:
Who is Participating?
 
negatyveConnect With a Mentor Commented:
Personally, I use these with the ListBox component (v. 1 components):

// =========================================
FListBoxClass.prototype.getInfo = function()
{
      var len = this.getLength();
      var row = this.getRowCount();
      var ind = this.getSelectedIndices();
      var pos = this.getScrollPosition();
      return {len:len, row:row, ind:ind, pos:pos};
};
FListBoxClass.prototype.moveItemDown = function()
{
      var info = this.getInfo();
      if(info.ind.length == 1){
            var num = Number(info.ind[0]), temp = this.getItemAt(num);
            this.removeItemAt(num);
            if(num < info.len - 1){
                  this.addItemAt(num + 1, temp.label, temp.data);
                  this.setSelectedIndex(num + 1);
                  if(num >= info.pos + info.row - 1){
                        this.setScrollPosition((num + 1) - (info.row - 1));
                  } else if(num <= info.pos - 1){
                        this.setScrollPosition(num + 1);
                  } else {
                        this.setScrollPosition(info.pos);
                  }
            }else{
                  this.addItemAt(0, temp.label, temp.data);
                  this.setSelectedIndex(0);
                  this.setScrollPosition(0);
            }
      }
};
FListBoxClass.prototype.moveItemUp = function()
{
      var info = this.getInfo();
      if(info.ind.length == 1){
            var num = Number(info.ind[0]), temp = this.getItemAt(num);
            this.removeItemAt(num);
            if(num > 0){
                  this.addItemAt(num - 1, temp.label, temp.data);
                  this.setSelectedIndex(num - 1);
                  if(num - 1 <= info.pos)      {
                        this.setScrollPosition(num - 1);
                  } else if(num >= info.pos + info.row){
                        this.setScrollPosition(num - info.row);
                  } else {
                        this.setScrollPosition(info.pos);
                  }
            }else{
                  this.addItem(temp.label, temp.data);
                  this.setSelectedIndex(info.len - 1);
                  this.setScrollPosition(info.len - info.row);
            }
      }
};

And these for List component (v. 2 components):

// =========================================
mx.controls.List.prototype.moveItemDown = function(Void):Void
{
      var info:Object = this.getScrollingInfo();
      if(info.sel == undefined)return;
      var item:Object = this.getItemAt(info.sel);
      this.removeItemAt(info.sel);
      if(info.sel < info.len - 1){
            this.addItemAt(info.sel + 1, item);
            this.clearIdList();
            this.selectedIndex = info.sel + 1;
            if(info.sel >= info.pos + info.row - 1)      {      this.vPosition = (info.sel + 1) - (info.row - 1);      }
            else if(info.sel <= info.pos - 1)            {      this.vPosition = (info.sel + 1);                        }
            else                                          {      this.vPosition = (info.pos);                              }
      } else {
            this.addItemAt(0, item);
            this.clearIdList();
            this.selectedIndex = this.vPosition = 0;
      }
};
mx.controls.List.prototype.moveItemUp = function(Void):Void
{
      var info:Object = this.getScrollingInfo();
      if(info.sel == undefined)return;
      var item:Object = this.getItemAt(info.sel);
      this.removeItemAt(info.sel);
      if(info.sel > 0){
            this.addItemAt(info.sel - 1, item);
            this.clearIdList();
            this.selectedIndex = info.sel - 1;
            if(info.sel - 1 <= info.pos)                  {      this.vPosition = info.sel - 1;            }
            else if(info.sel >= info.pos + info.row)      {      this.vPosition = info.sel - info.row;      }
            else                                          {      this.vPosition = info.pos;                  }
      }else{
            this.addItemAt(info.len - 1, item);
            this.clearIdList();
            this.selectedIndex = info.len - 1;
            this.vPosition = info.len - info.row;
      }

};
// clears ID
mx.controls.List.prototype.clearIdList = function(Void):Void
{
      var d:Array = this.dataProvider;
      for(var i:Number = 0; i < d.length; i++)delete(d[i].__ID__);
};
0
 
liltygaAuthor Commented:
Thanks Negatyve,

Unfortunatley, I'm not as seasoned as you, and although I tried integrating this, I am a bit confused as to how to implement this. Right now, I am retrieving data to the list box dynamically from the database. Can you clarify how I can grab the data from, lets say, lb_someData and moving that content with buttons btn_moveUp and btn_moveDown?
0
 
negatyveCommented:
1. what component version are you using? v1 (flash mx) or v2 (flash mx 2004)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
liltygaAuthor Commented:
v2 - 2004 pro
0
 
liltygaAuthor Commented:
You are awesome! Thanks so much!!
0
 
liltygaAuthor Commented:
hi again negatyve, I have a quick question to append to this - let me know if you want me to create another post for this - how can i best use the splice function to duplicate the removal/addition when moving items up or down, so that the adjusted data can be reflected in an array that I need to pass data with?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.