![]() |
Web Development »
Client side scripting »
General
Intermediate
Reorder multiple <select> fields in a web pageBy BabailiicaAn article on how to reorder a select field in multiple mode and submit the data to the server. |
Javascript, HTML, Windows, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
This script aims to extend the functionality of the standard <select> field in web pages. Sometimes, we need to order elements in a list. Such functionality extension may be useful, for example, in CMS when you need an interface to order and reorder some elements.
I started to work on a new CMS project for my client and searched for a script to reorder <select> elements. I found many scripts, but none of them does what I expect and what I need. As you know, the difference between programmers and normal people is that when programmers need something, they sit and script. So I wrote this script...
multiple="multiple" attribute of the sorted <select> field. You can move up, down, to top, or to bottom even a complex multiple selection. Read more in "About multiple selections";
<select> in to a hidden field, but submit the whole field as an array to the server. Note that the name of the <select> field must contain [] at the end. For example, if the name of the field should be "files", use: name="files[]";
<select> field - MSIE and Firefox. This script supports not only reordering texts and values, but also reorders the layout of the <select> field by changing colors, background colors, ID and class name attributes of the moved elements;
viceversa() and listsort() functions, the script remembers which element is selected and which is not;
There are six basic functions to order elements:
top(object);
Moves selected items in the <select> field to the top;
up(object);
Moves selected items in the <select> field one step upper;
down(object);
Moves selected items in the <select> field one step lower;
bottom(object);
Moves selected items in the <select> field to the bottom;
viceversa(object, onlyselected);
Reorders all or only selected items in the <select> field in backorder;
swap(object);
Swaps the first and the last selected items in the <select> field;
listsort(object, criteria, parameter, parameter);
Sorts all items in the <select> field by a criteria.
Also, there are additional functions:
selectall(object);
Selects all items in the <select> field before submitting the <form>;
selectnone(object);
Unselects all items in the <select> field;
mousewheel(object);
Moves selected items in the <select> field up or down by scrolling the mouse wheel.
additem(object, value, text, index, id, className, color, backgroundColor, selected);
Adds a new item with specified parameters to the existing <select> field;
removeitem(object, index);
Removes item from the <select> field;
You can create buttons for each function using this code:
<input type="button" value="Top" onclick="top(object);"
title="Move to top" />
Or you can use any page object with the onclick="function();" event. Don't use the <button> tag to create such buttons, because in some browsers, this type of buttons submits the form! Also, if using <img>, don't use the onclick="function();" event inside the image, but put the image in to an anchor tag:
<a href="#" onclick="top(object); return false;">
<img src="..." border="0" />
</a>
Never use href="javascript:". Later, I plan to submit an article on "Why we have never used href="javascript:"".
Another good idea is to use the mouse wheel to call the script. Unfortunately, it may be done only under the MSIE browser because of the onmousewheel="" event:
<select ... onmousewheel="mousewheel(this);">
If you use server side, you need to submit all elements in the <select> field. This is because we added the selectall(); function which can be used with the onsubmit="" event.
<form ... onsubmit="selectall('object'); return true;">
NEW! The new added function selectnone(); may be used with the ondblclick="" event. This way it is possible to unselect elements:
<select ... ondblclick="selectnone(this);">
Note that when calling any function of the script, as a first parameter, you have to set the <select> object. It means you have to send an instance to the object or just send the ID of the object:
f(object);
// or
f("id"); // when sending ID quote it!
The listsort(object, by, numeric, caseSensitive); function accepts four parameters, but the last three are optional.
object is an instance to the <select> field object or its ID;
by - integer. Sets the sort criteria. Acceptable values:
Default value when the parameter is not set or wrong;
numeric - boolean. If true, sorts by numeric values. For example, 2 is before 10;
caseSensitive - boolean. If true, sorts by case sensitive values. For example, a is before Z; The additem(object, text, value, index, id, className, color, backgroundColor, selected); function accepts nine parameters, but only the first and the second are required.
object is an instance to the <select> field object or its ID;
text - string. Sets the text of the new element;
value - string or integer. Sets the value of the new element;
index - integer. If set, determines in which position to add the new element. If not set or wrong, the new element is added at the end of the field. Index starts from 0 (zero) - the first element;
id - string or integer. Sets the ID of the new element. Be careful not to duplicate some ID!
className - string. Sets the CSS class of the new element;
color - string may be color name or HTML color value. Sets the color of the new element. See the known bugs!
backgroundColor - string may be color name or HTML color value. Sets the background color of the new element. See the known bugs!
selected - boolean. If true, the new added element will be selected by default. The removeitem(object, index); function accepts two parameters, and only the first is required.
object is an instance to the <select> field object or its ID;
index - integer or boolean:
False, the last element is removed;
True, the selected element(s) is(are) removed. Note: if more than one element is selected, all selected elements will be removed. The viceversa(object, onlyselected); from version 1.3, this function accepts two parameters, and only the first is required.
object is an instance to the <select> field object or its ID;
onlyselected - boolean:
swap function;
Multiple selection means that users can select more than one element of the <select> field. To enable multiple selection, add multiple="multiple" and size="#" attributes.
Two types of multiple selections exist:
When moving up or down a complex multiple selection, two script behaviors are possible:
If you need the second behavior, edit line 12 of up() and down() functions as follows:
// for up() function:
if (sel[i] != 0 && !obj[sel[i]-1].selected) {
if (sel[i] != 0) {
// for down() function:
if (sel[i] != obj.length-1 && !obj[sel[i]+1].selected) {
if (sel[i] != obj.length-1) {
To use the script:
<head> section of your web page, a link to load the script file: <script type="text/javascript" src="sort.js"></script>
Use the right location of the file in the src attribute, better relative;
form in your web page and add an onsubmit event to it: <form method="post" action="..." onsubmit="selectall('order');">
...
</form>
The selectall() function parameter is the ID attribute of the <select> field. Better use POST method, because of limitations of the GET method!
<select> field to your form: <select name="order[]" size="5" multiple="multiple"
id="order" onmousewheel="mousewheel(this);">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
onmousewheel event to the <select> field;
up() and down() functions: <input type="button" value="Move Up" onclick="up('order');"
title="Move up" />
<input type="button" value="Move Down" onclick="down('order');"
title="Move down" />
where the function parameter is the ID attribute of the <select> field.
Your server side script will receive an array variable containing the ordered values. For example, try this PHP code:
or ASP:
<%
For Each item In Request.Form()("order[]")
Response.Write item & "<br />"
Next
%>
A few bugs caused by some browsers' limitations:
onmousewheel event; selectnone();
additem();
removeitem();
<SELECT> with selected elements;
swap();
viceversa(); now supports only selected parameter;
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 15 Nov 2005 Editor: Smitha Vijayan |
Copyright 2004 by Babailiica Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |