Closed Thread Icon

Topic awaiting preservation: Ordering items in a select/list box (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8908" title="Pages that link to Topic awaiting preservation: Ordering items in a select/list box (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Ordering items in a select/list box <span class="small">(Page 1 of 1)</span>\

 
Blaise
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted posted 10-15-2003 01:58

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).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-15-2003 08:33

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Blaise
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted posted 10-16-2003 01:12

well I would have liked to post it online, but I didn't have the means to do so.

So sorry about the long post, butI thought I should post the solution up if nobody else was going to

Cheers,

Blaise

Hub-izer
Bipolar (III) Inmate

From: The little green dot at the center of your monitor
Insane since: Jul 2003

posted posted 10-23-2003 03:50

Hello,

This in brief may be just what I'm looking for...
Do you mind posting a brief working demo with an option box too? All I need, really, is option adding, deleting, and value retrieving.

Thanks.

« BackwardsOnwards »

Show Forum Drop Down Menu