Jquery controls the movement and sorting of items in listbox. Code _ jquery

Source: Internet
Author: User
Jquery implementation code for moving and sorting items in listbox. For more information about jquery and listbox, see. The first step is html code. There are two listbox controls and two buttons on the page for moving projects.

The Code is as follows:













All fruits: I selected:








Below are some data bound to the. cs File

The Code is as follows:


Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
BindData ();
}
}
Private void BindData ()
{
ArrayList list = DataArray ();
For (int I = 0; I <list. Count; I ++)
{
Listall. Items. Add (list [I]. ToString ());
Listall. Items [I]. Attributes ["tag"] = I. ToString (); // records the sorting fields with tags.
}
}
Private ArrayList DataArray ()
{
// Some data used, which is sorted by the first pinyin character by default
ArrayList list = new ArrayList ();
List. Add ("strawberry ");
List. Add ("Li ");
List. Add ("orange ");
List. Add ("Mango ");
List. Add ("apple ");
List. Add ("banana ");
Return list;
}
}




In actual use, you can sort data by fields in the database.
The following is the jquery code:


The Code is as follows:


// Move the user-selected role
// Setname: name of the list to be removed from the data getname: name of the list to be moved into the data
Function move (setname, getname)
{
Var size = $ ("#" + setname + "option"). size ();
Var selsize = $ ("#" + setname + "option: selected"). size ();
If (size> 0 & selsize> 0)
{
$. Each ($ ("#" + setname + "option: selected"), function (id, own ){
Var text = $ (own). text ();
Var tag = $ (own). attr ("tag ");
$ ("#" + Getname). prepend (""+ Text +"");
$ (Own). remove ();
$ ("#" + Setname + ""). children ("option: first"). attr ("selected", true );
});
}
// Re-sort
$. Each ($ ("#" + getname + "option"), function (id, own ){
Orderrole (getname );
});
}
// Sort the role list by the first letter
Function orderrole (listname)
{
Var size = $ ("#" + listname + "option"). size ();
Var one = $ ("#" + listname + "option: first-child ");
If (size> 0)
{
Var text = $ (one). text ();
Var tag = parseInt ($ (one). attr ("tag "));
// All elements under the first value in the loop list
$. Each ($ (one). nextAll (), function (id, own ){
Var nextag = parseInt ($ (own). attr ("tag "));
If (tag> nextag)
{
$ (One). remove ();
$ (Own). after (""+ Text +"");
One = $ (own). next ();
}
});
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.