Click here to Skip to main content
15,896,269 members
Articles / Web Development / XHTML

Custom Sorting with Jquery UI

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
19 Sep 2009CPOL2 min read 35.2K   346   19  
In this article, I will examine how to build custom sorting Using Jquery UI. The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CustomSort._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sortable Demo</title>
   
<script type="text/javascript" 
        src="http://www.google.com/jsapi"></script>
    <script language="javascript" type="text/javascript">
        google.load("jquery", "1.3.2");
        google.load("jqueryui", "1.7.2");
        </script>
        <script type="text/javascript" 
        src="http://www.json.org/json2.js"></script>
        <SCRIPT type=text/javascript>
            $(function() {
                $("#sortable").sortable();
                $("#sortable").disableSelection();
            });
            function update() {
                var list = new Array();
                $("ul").each(function(index, id) {
                    var result = $('#' + id.id).sortable('toArray');
                    var j = 0;
                    for (var i = 0; i < result.length; i++) {
                        list[j++] = $('#' + result[i])[0].value;
                    }
                });

                var jsonText = JSON.stringify({ list: list });

                $.ajax({
                    type: "POST",
                    url: "Default.aspx/SaveData",
                    data: jsonText,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(res) {
                        $("#divResult").text(res.d);
                    }
                });
 
            }
	</SCRIPT>

        
         <style type="text/css">   
    ul.horizontal_list li{
    text-align:left;
    list-style: none;
    padding: 3px 10px 3px 10px;
    margin: 5px;
    border: 1px solid #CCC;
    width: 250px; 
    cursor : move;
}
</style>
</head>
<body>
   
   <div id="divResult" style="color:red;"></div>
    <asp:Panel ID="spaceHolder" runat="server" ></asp:Panel> 
    <input type="button" value="Update" onclick="javascript:update();" />  
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions