jqGrid在MVC中使用方法

  • A+
所属分类:jqGrid

Views

  1. <span style="white-space:pre">    </span><div class="jqGrid_wrapper">    
  2.      <span style="white-space:pre">       </span>  <table id="table_list_2"></table>    
  3.     <span style="white-space:pre">        </span>  <div id="pager_list_2"></div>    
  4.   <span style="white-space:pre">  </span> </div>    
  1. <script>    
  2.     $(document).ready(    
  3.         function () {    
  4.                 
  5.             $("#table_list_2").jqGrid(    
  6.                 {    
  7.                     url: "/Handler1.ashx",    
  8.                     datatype: "json",    
  9.                     height: 450,    
  10.                     autowidth: true,    
  11.                     shrinkToFit: true,    
  12.                     rowNum: 20,    
  13.                     rowList: [10, 20, 30],    
  14.                     colNames: ["序号""日期""客户""金额""运费""总额"],    
  15.                     colModel: [    
  16.                         {    
  17.                             name: "U_Id",    
  18.                             index: "id",    
  19.                             editable: true,    
  20.                             width: 60,    
  21.                             sorttype: "int",    
  22.                             search: true    
  23.                         },    
  24.                         {    
  25.                             name: "U_LoginName",    
  26.                             index: "U_LoginName",    
  27.                             editable: true,    
  28.                             width: 90,    
  29.                             sorttype: "string",    
  30.                             formatter: "string"    
  31.                         },    
  32.                         {    
  33.                             name: "U_Pass",    
  34.                             index: "U_Pass",    
  35.                             editable: true,    
  36.                             width: 100    
  37.                         },    
  38.                         {    
  39.                             name: "U_Email",    
  40.                             index: "U_Email",    
  41.                             editable: true,    
  42.                             width: 80,    
  43.                             align: "right",    
  44.                             sorttype: "float",    
  45.                             formatter: "string"    
  46.                         },    
  47.                         {    
  48.                             name: "U_PhoneNumber",    
  49.                             index: "U_PhoneNumber",    
  50.                             editable: true,    
  51.                             width: 80,    
  52.                             align: "right",    
  53.                             sorttype: "float"    
  54.                         },    
  55.                         {    
  56.                             name: "UType_Id",    
  57.                             index: "UType_Id",    
  58.                             editable: true,    
  59.                             width: 80,    
  60.                             align: "right",    
  61.                             sorttype: "float"    
  62.                         }],    
  63.                     pager: "#pager_list_2",    
  64.                     viewrecords: true,    
  65.                     caption: "jqGrid 示例2",    
  66.                     add: true,    
  67.                     edit: true,    
  68.                     addtext: "Add",    
  69.                     edittext: "Edit",    
  70.                     hidegrid: false    
  71.                 });    
  72.             $("#table_list_2").setSelection(4, true);    
  73.             $("#table_list_2").jqGrid("navGrid""#pager_list_2", { edit: true, add: true, del: true, search: true },    
  74.                                                                   { height: 200, reloadAfterSubmit: true });    
  75.             $(window).bind("resize", function () {    
  76.                 var width = $(".jqGrid_wrapper").width();    
  77.                 $("#table_list_2").setGridWidth(width)    
  78.             })    
  79.         });    
  80. </script>    

一般处理程序

  1. public void ProcessRequest(HttpContext context)    
  2.         {    
  3.             NameValueCollection forms = context.Request.Form;    
  4.             string strOperation = forms.Get("oper");    
  5.             if (strOperation == null)    
  6.             {    
  7.                 DataTable dt = GetAccountInfo();    
  8.                 string jsonRes = GetJson(dt);    
  9.                 context.Response.Write(jsonRes);    
  10.     
  11.             }    
  12.         }    
  13.     
  14.         private DataTable GetAccountInfo()    
  15.         {    
  16.             CYX.Model.UserInfo muser = new CYX.Model.UserInfo();    
  17.             CYX.BLL.UserInfo buser = new CYX.BLL.UserInfo();    
  18.             System.Data.DataTable mydt = buser.GetAllList().Tables[0];        
  19.             return mydt;    
  20.         }    
  21.     
  22.         private string GetJson(DataTable mydt)    
  23.         {    
  24.             JavaScriptSerializer serializer = new JavaScriptSerializer();    
  25.             List<Dictionary<stringobject>> rows =    
  26.                 new List<Dictionary<stringobject>>();    
  27.             Dictionary<stringobject> row = null;    
  28.     
  29.             foreach (DataRow dr in mydt.Rows)    
  30.             {    
  31.                 row = new Dictionary<stringobject>();    
  32.                 foreach (DataColumn col in mydt.Columns)    
  33.                 {    
  34.                     row.Add(col.ColumnName.Trim(), dr[col]);    
  35.                 }    
  36.                 rows.Add(row);    
  37.             }    
  38.             return serializer.Serialize(rows);    
  39.         }    
钰玺

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: