Hi guys 'n' gals how's it all going?
I've a question about moving items about in a list box, I've got one of these pages where you can insert items to a list, edit them, delete them etc... but I would also like to order them,
There are two ways I would like to do this, firstly but alpha ordering (A-Z, Z-A) and also moving one item at a time, ordering them via up and down buttons.
Now I've got a list box which displays my items and a text box which is used to add/edit items, I also have buttons which add, edit, delete, A-Z, Z-A, Up, and Down the items, I'm having trouble applying the last four functons however, I'm not actually sure how to start, here's what I've got so far...
code:
function addItem(myObj){
var numItems = myObj.length; // manual list counter </strong>
var d=document.form1;
var txt = d.txtItem.value;
addOption = new Option(txt,txt);
myObj.options[numItems++] = addOption;
}
function editItem(myObj){
var numItems = myObj.length; // manual list counter </strong>
var d=document.form1;
var txt = d.txtItem.value;
var myItem = myObj.selectedIndex; //currently selected item
editOption = new Option(txt,txt);
if (myObj.selectedIndex>-1){myObj.options[myItem] = editOption;}
}
function delItem(myObj){
var numItems = myObj.length; // manual list counter </strong>
var d=document.form1;
var myItem = myObj.selectedIndex; //currently selected item
if (myObj.selectedIndex>-1){myObj.options[myItem] = null;}
}
If anyone has any clues as to how I should go about applying the last four items, I would really appreciate the advice, I'm just a little confused about moving one item up and one item down at the same time, also I think the alpha ordering will be a big task.
[edit]
I've just finished the code for moving items up or down, I'm quite pleased with it, it seems to work pretty shmick.
code:
function moveItem(myObj, dir){
var numItems = myObj.length;
var d=document.form1;
var myItem=myObj.selectedIndex;
var nextItem=0;
var myItemTxt, nextItemTxt;
var myOption, nextOption;
if(dir=='up'){
nextItem=myItem-1;
}
else if(dir=='down'){
nextItem=myItem+1;
}
if((myItem > -1) && (nextItem > -1) && (nextItem < numItems)){
myItemTxt=myObj.options[myItem].value; //current selected text
nextItemTxt=myObj.options[nextItem].value; //previous or next items text
myOption=new Option(myItemTxt, myItemTxt); //option containing current item
nextOption=new Option(nextItemTxt, nextItemTxt); //option containing next item
myObj.options[myItem]=nextOption //put the next option into the current position
myObj.options[nextItem]=myOption //put the selected option into the next position
myObj.options[nextItem].selected = true; //keep teh currently selected item selected
}
}
[/edit]
[edit again]
Alright I finally managed to do it all by myself and how proud I feel because of it here's teh final function which uses a bubble sort to order items alphabetically...
code:
function sortItem(myObj, dir){
var numItems = myObj.length;
var d=document.form1;
var myItem, nextItem;
var myItemTxt, nextItemTxt;
var myOption, nextOption;
var complete='no'
while(complete=='no'){//loop through list until it has been ordered correctly
complete='yes'
//loop through each item in list
for(myItem=0; myItem<numItems; myItem++){
nextItem=myItem+1
if((myItem > -1) && (nextItem > -1) && (nextItem < numItems)){
myItemTxt=myObj.options[myItem].value;
nextItemTxt=myObj.options[nextItem].value;
myOption=new Option(myItemTxt, myItemTxt);
nextOption=new Option(nextItemTxt, nextItemTxt);
if(dir=='a-z'){
//check to see if the next item is later in the alphabet than myItemTxt
if(myItemTxt.localeCompare(nextItemTxt)==1){
//swap items and set complete to no
myObj.options[myItem]=nextOption
myObj.options[nextItem]=myOption
complete='no'
}
}
else if(dir=='z-a'){
//check to see if the next item is earlier in the alphabet than myItemTxt
if(myItemTxt.localeCompare(nextItemTxt)==-1){
//swap items and set complete to no
myObj.options[myItem]=nextOption
myObj.options[nextItem]=myOption
complete='no'
}
}
}
}
}
}
Well if anyone has any comments on this I'd be pleased to hear them, I'm not certain that I've programmed this the most efficient way, and I'm also not entirely satisfied with the bubble sort, if anyone can recommend another sort method and how to do this then please post here!
[/edit]
TIA,
Blaise.
[This message has been edited by Blaise (edited 10-15-2003).]
[This message has been edited by Blaise (edited 10-15-2003).]
[This message has been edited by Blaise (edited 10-15-2003).]