Jump to content

[SOLVED] combine an option transfer with sort


tryingtolearn

Recommended Posts

Hi all,

I am trying to piece together two seperate examples to make a form that will

Transer items in a list box to another listbox

Then in the second list box allow them to sort the order to their preference.

 

Everything is working until I get to the sorting

 

If I try to move an item up, I cant move it into the top position, it will sort of duplicate itself in the listbox.

 

Not exactly sure what the code means and does so I would appreciate any guidance on where I am screwing up.

 


<script language="javascript" type="text/javascript">
function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
    }
}


fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
    }
}
</script>
<script type="text/javascript">
function moveO(listform,w){
var tolist=document.getElementsByName(listform)[0];
var opt=tolist.options[tolist.selectedIndex];
if(w=='up'){
var prev=opt.previousSibling;
while(prev&&prev.nodeType!=1){
prev=prev.previousSibling;
}
prev?tolist.insertBefore(opt,prev):tolist.appendChild(opt)
}
else{
var next=opt.nextSibling;
while(next&&next.nodeType!=1){
next=next.nextSibling;
}
if(!next){tolist.insertBefore(opt,tolist.options[0])}
else{
var nextnext=next.nextSibling;
	while(next&&next.nodeType!=1){
	next=next.nextSibling;
	}
nextnext?tolist.insertBefore(opt,nextnext):tolist.appendChild(opt);
}
}
}
</script>
<form name="listform">
<table><tr><td>
<select multiple size="15" name="fromlist" style="width:100">
<option value="1">Event 1</option>
<option value="2">Event 2</option>
<option value="3">Event 3</option>
<option value="4">Event 4</option>
<option value="5">Event 5</option>
<option value="6">Event 6</option>
<option value="7">Event 7</option>
<option value="8">Event 8</option>
<option value="9">Event 9</option>
<option value="10">Event 10</option>
<option value="11">Event 11</option>
<option value="12">Event 12</option>
<option value="13">Event 13</option>
<option value="14">Event 14</option>
<option value="15">Event 15</option>
</select>
</td>
<td align="center" valign="middle">
<input type="button" onClick="move(this.form.tolist,this.form.fromlist)" value="<<">
<input type="button" onClick="move(this.form.fromlist,this.form.tolist)" value=">>">
</td>
<td>
<select multiple size="15" name="tolist" style="width:150">
</select>
</td>
<td>
<input type="button" value="UP" onclick="moveO('tolist','up')" />
<br />
<input type="button" value="DOWN" onclick="moveO('tolist','down')" />
</td>
</tr></table>
</form>



Fixed:

function move(fbox, tbox) {
var arrFbox ={};
var arrTbox = {};
var arrLookup = {};
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value !== "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
    }
}
}

im fixing up your code making it error free :P

 

 

fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
  var nope=new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
    }

Well this is what I eventually ended up with from a different tutorial.

(If anyone is following or in need)

Seems to be working but I wouldnt know if there was an error in it as can be seen from my previous code.

 

<script type="text/javascript" language="javascript">


var max_vent = 15; 

function inDest(dest, text, value) { 
var opt, o = 0; 
while (opt = dest[o++]) if (opt.value == value && opt.text == text) return true; 
return o > max_vent; 
} 

function toDest(s, dest) { 
var opt, o = 0; 
while (opt = s[o++]) if (opt.selected && !inDest(dest, opt.text, opt.value)) 
dest.options[dest.length] = new Option(opt.text,opt.value); 

} 

function moveSelected (select, down) { 
if (select.selectedIndex != -1) { 
if (down) { 
if (select.selectedIndex != select.options.length - 1) 
var x = select.selectedIndex + 1; 
else 
return; 
} 
else { 
if (select.selectedIndex != 0) 
var x = select.selectedIndex - 1; 
else 
return; 
} 
var swapOption = new Object(); 
swapOption.text = select.options[select.selectedIndex].text; 
swapOption.value = select.options[select.selectedIndex].value; 
swapOption.selected = select.options[select.selectedIndex].selected; 
swapOption.defaultSelected = select.options[select.selectedIndex].defaultSelected; 
for (var property in swapOption) { 
    select.options[select.selectedIndex][property] = select.options[x][property]; 
    select.options[x][property] = swapOption[property]; 
} 

} 
} 

function delSelected(dest) { 
var opt, o = 0; 
while (opt = dest[o++]) if (opt.selected) dest[o-1] = null; 
} 

function setHidden(f) { 
var destVals = new Array(), opt = 0, separator = '|', d = f.dest; 
while (d[opt]) destVals[opt] = d[opt++].value; 
f.destItems.value = separator + destVals.join(separator) + separator; 
alert('destItems.value = ' + f.destItems.value); 
return true; 
} 

</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.