Reply
Multiple Select
Old 08-04-2006, 06:15 PM Multiple Select
dcp
Junior Talker

Posts: 1
I'm using code from Gizmola's MySQL/PHP Tutorial, and am having a terrible time getting the JavaScript Chooser to work.

The code below is supposed to move values from one form element to another by clicking an HTML button. Trouble is, when I select one option to move, it moves as anticipated, but all the options above it (except the topmost) disappear.

The code is below. I apologize if this is obvious, but I'm not seeing it.

dcp

____________________





<script language="JavaScript">
<!-- Begin
sortitems = 1; // Sort list items? (1=yes)

function move(fbox,tbox) {
for(var i=0; i<fbox.options.length; i++) {
if(fbox.options[i].selected && fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
BumpUp(fbox);
if (sortitems)
SortD(tbox);
}
function BumpUp(box) {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "") {
for(var j=1; j<box.options.length-1; j++) {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
}
}

if(ln < box.options.length) {
box.options.length -= 1;
BumpUp(box);
}
}
function SortD(box) {
var temp_opts = new Array();
var temp = new Object();

for (var i=0; i<box.options.length; i++) {
temp_opts[i] = box.options[i];
}
for (var x=0; x<temp_opts.length-1; x++) {
for(var y=(x+1); y<temp_opts.length; y++) {
if(temp_opts[x].text > temp_opts[y].text) {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
}
}
}
for(var i=0; i<box.options.length; i++) {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
}
}
function SelectListAll(box) {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value != "") {
box.options[i].selected = true;
}
}
return true;
}
// End -->
</script>


<form action="?action=load" method="post" name="site_form" >

<table>
<tr>
<td style="width: 200px;"><select name="list_u[]" multiple="multiple" size="7">
<option value="1">Bernalillo</option>
<option value="2">Catron</option>
<option value="3">Chaves</option>
<option value="4">Cibola</option>
<option value="5">Colfax</option>
<option value="6">Curry</option>
<option value="7">De Baca</option>
<option value="8">Dona Ana</option>
<option value="9">Eddy</option>
<option value="10">Grant</option>


</select>
</td>

<td style="width: 100px; text-align: center;"><input type="button" value=" >> " onclick="move(document.forms[0].elements['list_u[]'],document.forms[0].elements['list_s[]'])"><br />

<input type="button" value=" << " onclick="move(document.forms[0].elements['list_s[]'],document.forms[0].elements['list_u[]'])"></td>

<td style="width: 200px;">
<select name="list_s[]" size="7" multiple="multiple">
</select></td></tr></table></td></tr><tr><td>&nbsp;</td><td><input name="submit" type="button" value="Submit" /></td></tr></table><br /><br />
</form>

dcp is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 08-05-2006, 01:42 AM Re: Multiple Select
funkdaddu's Avatar
Web Design Snob

Posts: 636
That script doesn't seem very streamlined... try this. I whipped it up from scratch - it's about half the size. Please test in IE though, I don't have a copy to try out at home:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
function moveOptions(fromBox,toBox,sort) {
	//set our variables
	selectedOptions = new Array();
	fromBoxLength = fromBox.options.length;
	toBoxLength = toBox.options.length;

	//find selected options, add them to an array and then delete them
	for (x=fromBoxLength-1; x>-1; x--) {
		if (fromBox.options[x].selected == true) {
			selectedOptions.push(fromBox.options[x]);
			fromBox.options[x].parentNode.removeChild(fromBox.options[x]);
		}
	}
	
	//put selected options into new box
	for (y in selectedOptions) {
		toBox.appendChild(selectedOptions[y]);
	}
	
	//sort array by name alphabetically
	if (sort) {
		var sortArray = new Array();
		for (z=toBox.options.length-1; z>-1; z--) {
			sortArray.push(toBox.options[z]);
			toBox.options[z].parentNode.removeChild(toBox.options[z]);
		}
		sortArray.sort(optionsSorter);
		for (a in sortArray) {
			toBox.appendChild(sortArray[a]);
		}
	}
	fromBox.focus();
}

function optionsSorter(a,b) {
	return a.text > b.text;
}
//-->
</script>
	</head>

	<body bgcolor="#ffffff">
		<form action="./?action=load" method="post" name="site_form">
			<table>
				<tr>
					<td style="width: 200px;"><select name="list_u[]" multiple="multiple" size="7">
							<option value="1">Bernalillo</option>
							<option value="2">Catron</option>
							<option value="3">Chaves</option>
							<option value="4">Cibola</option>
							<option value="5">Colfax</option>
							<option value="6">Curry</option>
							<option value="7">De Baca</option>
							<option value="8">Dona Ana</option>
							<option value="9">Eddy</option>
							<option value="10">Grant</option>
						</select></td>
					<td style="width: 100px; text-align: center;"><input type="button" value=" &gt;&gt; " onclick="moveOptions(document.forms[0].elements['list_u[]'],document.forms[0].elements['list_s[]'],true)"><br />
						 <input type="button" value=" &lt;&lt; " onclick="moveOptions(document.forms[0].elements['list_s[]'],document.forms[0].elements['list_u[]'],true)"></td>
					<td style="width: 200px;"><select name="list_s[]" size="7" multiple="multiple"></select></td>
				</tr>
			</table>
		</form>
	</body>

</html>

Last edited by funkdaddu : 08-05-2006 at 01:48 AM.
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 08-05-2006, 01:53 AM Re: Multiple Select
funkdaddu's Avatar
Web Design Snob

Posts: 636
Hmm it's not sorting reliably... I'll check it out in the morning...

EDIT: OK Here is the new optionsSorter function:
Code:
function optionsSorter(a,b) {
    var c = a.text;
    var d = b.text;
    return ((c < d) ? -1 : ((c > d) ? 1 : 0));
}
replace the old function, and it will work now.

Last edited by funkdaddu : 08-05-2006 at 12:01 PM.
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Reply     « Reply to Multiple Select
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16387 seconds with 12 queries