Home > jquery > Dual listboxes in jquery

Dual listboxes in jquery

Ok, so this one should be simple, more for reference. The goal is to create a dual list box using jQuery and MVC. To accomplish this, we are going to use the “appendTo” manipulation (http://docs.jquery.com/Manipulation). You can also download the dual listbox code sample here.

Add two select boxes, and four buttons

<table border="0">
<tr>
<td valign="top" align="left">
<%= Html.ListBox("AvailableCategories", null, new { @size = "8", @style = "width:200px" })%>
</td>

<td valign="middle" align="center">
<input id="btnAdd" type="button" value=" > " onclick="add();" /><br />
<input id="btnAddAll" type="button" value=" >> " onclick="addall();" /><br />
<input id="btnRemove" type="button" value=" < "  onclick="remove();" /><br />
<input id="btnRemoveAll"type="button" value=" << "  onclick="removeall();" />
</td>

<td valign="top" align="left">
<%= Html.ListBox("SelectedCategories", null, new { @size = "8", @style = "width:200px" })%>
</td>
</tr>
</table>

Add javascript handlers for the button clicks

<script language="javascript" type="text/javascript">
function add() {
$("#AvailableCategories option:selected").appendTo("#SelectedCategories");
}

function addall() {
$("#AvailableCategories option").appendTo("#SelectedCategories");
}

function remove() {
$("#SelectedCategories option:selected").appendTo("#AvailableCategories");
}

function removeall() {
$("#SelectedCategories option").appendTo("#AvailableCategories");
}
</script>

Generate listbox items in Controller

The AvailableCategories and SelectedCategories collections are populated in the controller using ViewData:

public ActionResult Index()
{
Category[] AvailableCategories = Category.FindAvailable();
Category[] SelectedCategories = Category.FindSelected();

ViewData["AvailableCategories"] = new SelectList(AvailableCategories,"CategoryID","Category");
ViewData["SelectedCategories"] = new SelectList(SelectedCategories,"CategoryID","Category");

return View();
}

That’s it!

, , , ,

Comments:4

Leave a Reply
  1. william
    10/07/22

    any download link?

  2. bill sternberger
    10/07/23

    I haven’t rolled that into my samples app but can if you guys want it.

  3. lisa
    10/08/13

    This example is just what I was looking for thanks :)
    Can you also explain how we would go about saving the data in the second listbox to the db?

    Cheers

  4. Balaji
    10/12/09

    This is fine.
    I want to use other attribute as selector ex: title instead of id as here. I have tried but it is not appends the list.. any idea..?

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks:0

Listed below are links to weblogs that reference
Dual listboxes in jquery from C#, jQuery, .NET, MVC, nHibernate, MySQL
TOP