Tuesday, May 18, 2010

jQuery sortable with asp.net



การ sorttable จะใช้ jquery ui อยู่ 3 ไฟล์นะครับ คือ jquery.x.x.js ,ui.core.js,ui.sortable.js
ในส่วนของ form
  • ">
  • หน้าที่ #
    " width="120" height="64">
    ส่วนของ javascript
    $(document).ready(function(){
    $("#reorder").sortable({ handle:"span", start:sorting, stop:sortStop, update:updatePageNo });
    });

    function sorting(event,ui){
    $(ui.item).addClass("draging");
    //Set loading image
    $('#loading').html("").text("กรุณารอสักครู่...");
    }
    function sortStop(event,ui){
    $(ui.item).removeClass("draging");
    }
    function updatePageNo(event,ui){
    var idArr = $('#reorder').sortable('toArray');
    var dataTransfer;
    //var issueID = $("#HiddenIssueID").val();
    dataTransfer = "[";
    //Convert to json array
    jQuery.each(idArr,function(index){
    dataTransfer +="{\"ContentID\":"+this+",\"PageNo\":"+(index+1)+"},";
    });
    dataTransfer = dataTransfer.substring(0,dataTransfer.length-1);
    dataTransfer += "]";
    //$.toJSON(idArr);
    //Sys.Serialization.JavaScriptSerializer.serialize(idArr
    $.post(
    'UpdateReorderHandler.ashx?q='+dataTransfer,
    function(data) {
    //Remove loading image
    $('#loading').empty();
    if(data=="success"){
    alert("เเก้ไขข้อมูลการเรียงลำดับเรียบร้อยแล้ว");
    window.location = window.location;
    }else{
    alert("ไม่สามารถเเก้ไขข้อมูลการเรียงลำดับได้");
    $('#reorder').sortable('cancel');
    window.location = window.location;
    }
    }
    );
    }

    และส่วนของ Server
    context.Response.ContentType = "text/plain";
    string q = context.Request.QueryString["q"];
    JsonTextParser parser = new System.Net.Json.JsonTextParser();
    JsonObject obj = parser.Parse(q);
    JsonUtility.GenerateIndentedJsonText = false;
    string name = string.Empty;
    string value = string.Empty;
    JsonArrayCollection arrjSon = (JsonArrayCollection)obj;
    List PageList = new List();
    ContentPage page = null;
    foreach (JsonObject field in arrjSon)
    {
    List jsons = (List)field.GetValue();
    page = new ContentPage();
    foreach (JsonObject o in jsons)
    { //issue = new Issue();
    if (o.Name == "ContentID")
    page.ContentID = Convert.ToInt64(o.GetValue());
    if (o.Name == "PageNo")
    page.PageNo = Convert.ToInt32(o.GetValue());
    //if (o.Name == "IssueID")
    //issue.IssueID = o.GetValue().ToString();
    //page.Issue = issue;
    }
    PageList.Add(page);
    }
    if (_issueService == null)
    _issueService = new IssueService();
    try
    {
    if (_issueService.UpdateSequenceNo(PageList.ToArray()))
    { //Success
    context.Response.Write("success");
    }
    else
    {
    //Fail
    context.Response.Write("fail");
    }
    }
    catch (Exception)
    {
    context.Response.Write("fail");
    }


    No comments: