截图:

jquery easyui datagrid的增加,修改,删除_json

页面:


view plain​​copy to clipboard​​print​​?


  1. <body>
  2. <form id="form1" runat="server">
  3. <table id="tt">

  4. </table>


  5. </form>
  6. </body>

<body> <form id="form1" runat="server"> <table id="tt"> </table> </form> </body>


引用的JS:


view plain​​copy to clipboard​​print​​?


  1. <link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/script/themes/default/easyui.css" />
  2. <link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/script/themes/icon.css" />
  3. <script type="text/javascript" src="http://www.cnblogs.com/script/jquery-1.4.2.min.js" </script>
  4. <script type="text/javascript" src="http://www.cnblogs.com/script/jquery.easyui.min.js" </script>
  5. <script type="text/javascript" src="http://www.cnblogs.com/script/locale/easyui-lang-zh_CN.js" mce_src="script/locale/easyui-lang-zh_CN.js"></script>

<link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/script/themes/default/easyui.css" /> <link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/script/themes/icon.css" /> <script type="text/javascript" src="http://www.cnblogs.com/script/jquery-1.4.2.min.js" </script> <script type="text/javascript" src="http://www.cnblogs.com/script/jquery.easyui.min.js" </script> <script type="text/javascript" src="http://www.cnblogs.com/script/locale/easyui-lang-zh_CN.js" mce_src="script/locale/easyui-lang-zh_CN.js"></script>


JS:


view plain​​copy to clipboard​​print​​?


  1. <script type="text/javascript"><!--
  2. $(function(){
  3. $('#tt').datagrid({
  4. width:810,
  5. height:400,
  6. idField:'xsbh',
  7. url:'studentHandler.ashx',
  8. singleSelect:true,
  9. columns:[[
  10. {field:'xsbh',title:'编号',width:80},
  11. {field:'UserName',title:'姓名',width:100},
  12. {field:'Sex',title:'性别',width:30},
  13. {field:'SchoolYear',title:'年份',width:50},
  14. {field:'opt',title:'操作',width:100,align:'center',
  15. formatter:function(value,rec,index){
  16. var s = '<a href="#" mce_href="#" onclick="view(\''+ rec.xsbh + '\')">查看</a> ';
  17. var e = '<a href="#" mce_href="#" onclick="edit(\''+ rec.xsbh + '\')">编辑</a> ';
  18. var d = '<a href="#" mce_href="#" onclick="del(\''+ index +'\')">删除</a> ';
  19. return s+e+d;
  20. }
  21. }
  22. ]],
  23. toolbar:[{
  24. text:'增加',iconCls:'icon-add',handler:function(){
  25. window.location.href='StuAdd.aspx';
  26. }
  27. },
  28. {text:'导入',iconCls:'icon-add',handler:function(){
  29. window.location.href='StuImport.aspx'
  30. }
  31. },
  32. {text:'查找',iconCls:'icon-search'}
  33. ],
  34. pagination:true
  35. });
  36. })

  37. function view(bh) //转到查看页面
  38. {
  39. window.location.href='StuView.aspx?id='+bh+'&page=stu';
  40. // var row = $('#tt').datagrid('getSelected');
  41. // if(row)
  42. // {
  43. // alert(row.xsbh);
  44. // }
  45. }
  46. function edit(bh) //转到编辑页面
  47. {
  48. window.location.href='StuEdit.aspx?id='+bh;
  49. }

  50. function del(index){ //删除操作
  51. $.messager.confirm('确认','确认删除?',function(row){
  52. if(row){
  53. var selectedRow = $('#tt').datagrid('getSelected'); //获取选中行
  54. $.ajax({
  55. url:'delHandler.ashx?id='+selectedRow.xsbh+'&type=stu',
  56. //加了个type,作用是以后不管什么删除,都可以转到这个ashx中处理
  57. success:function(){alert('删除成功');}
  58. });
  59. $('#tt').datagrid('deleteRow',index);
  60. }
  61. })
  62. }

  63. // --></script>

<script type="text/javascript"><!-- $(function(){ $('#tt').datagrid({ width:810, height:400, idField:'xsbh', url:'studentHandler.ashx', singleSelect:true, columns:[[ {field:'xsbh',title:'编号',width:80}, {field:'UserName',title:'姓名',width:100}, {field:'Sex',title:'性别',width:30}, {field:'SchoolYear',title:'年份',width:50}, {field:'opt',title:'操作',width:100,align:'center', formatter:function(value,rec,index){ var s = '<a href="#" mce_href="#" onclick="view(\''+ rec.xsbh + '\')">查看</a> '; var e = '<a href="#" mce_href="#" onclick="edit(\''+ rec.xsbh + '\')">编辑</a> '; var d = '<a href="#" mce_href="#" onclick="del(\''+ index +'\')">删除</a> '; return s+e+d; } } ]], toolbar:[{ text:'增加',iconCls:'icon-add',handler:function(){ window.location.href='StuAdd.aspx'; } }, {text:'导入',iconCls:'icon-add',handler:function(){ window.location.href='StuImport.aspx' } }, {text:'查找',iconCls:'icon-search'} ], pagination:true }); }) function view(bh) //转到查看页面 { window.location.href='StuView.aspx?id='+bh+'&page=stu'; // var row = $('#tt').datagrid('getSelected'); // if(row) // { // alert(row.xsbh); // } } function edit(bh) //转到编辑页面 { window.location.href='StuEdit.aspx?id='+bh; } function del(index){ //删除操作 $.messager.confirm('确认','确认删除?',function(row){ if(row){ var selectedRow = $('#tt').datagrid('getSelected'); //获取选中行 $.ajax({ url:'delHandler.ashx?id='+selectedRow.xsbh+'&type=stu', //加了个type,作用是以后不管什么删除,都可以转到这个ashx中处理 success:function(){alert('删除成功');} }); $('#tt').datagrid('deleteRow',index); } }) } // --></script>


这里面要注意的是,"操作"的跨行,一定要带上field:'opt',当然,field可以是任何值,这个值不用从数据库中绑定,随便取.如果没有field的话,会弹出 "rowspan为空或不是对象"的错误

获取数据和分页ashx:


view plain​​copy to clipboard​​print​​?


  1. using System;
  2. using System.Web;
  3. using System.Data;
  4. using System.Text;

  5. public class studentHandler : IHttpHandler {

  6. public void ProcessRequest (HttpContext context) {
  7. context.Response.ContentType = "text/plain";
  8. DataSet ds = new DataSet();
  9. //点击datagrid的分页按钮,自动向后台发送2个参数,rows和page,代表每页记录数和页索引
  10. int row = int.Parse(context.Request["rows"].ToString());
  11. int page = int.Parse(context.Request["page"].ToString());
  12. ds = GetContent(row, page);
  13. string text =json.Dataset2Json(ds);
  14. context.Response.Write(text);
  15. }

  16. private DataSet GetContent(int pagesize,int pageindex)
  17. {
  18. Graduate.BLL.Student bll = new Graduate.BLL.Student();
  19. return bll.GetList(pagesize, pageindex);
  20. }
  21. public bool IsReusable {
  22. get {
  23. return false;
  24. }
  25. }

  26. }

using System; using System.Web; using System.Data; using System.Text; public class studentHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; DataSet ds = new DataSet(); //点击datagrid的分页按钮,自动向后台发送2个参数,rows和page,代表每页记录数和页索引 int row = int.Parse(context.Request["rows"].ToString()); int page = int.Parse(context.Request["page"].ToString()); ds = GetContent(row, page); string text =json.Dataset2Json(ds); context.Response.Write(text); } private DataSet GetContent(int pagesize,int pageindex) { Graduate.BLL.Student bll = new Graduate.BLL.Student(); return bll.GetList(pagesize, pageindex); } public bool IsReusable { get { return false; } } }


删除ashx


view plain​​copy to clipboard​​print​​?


  1. using System;
  2. using System.Web;
  3. using System.Web.SessionState;

  4. public class delHandler : IHttpHandler,IRequiresSessionState {

  5. public void ProcessRequest (HttpContext context) {
  6. context.Response.ContentType = "text/plain";
  7. string id = context.Request["id"].ToString();
  8. string type = context.Request["type"].ToString();
  9. switch (type)
  10. {
  11. case "stu":
  12. Graduate.BLL.Student stubll = new Graduate.BLL.Student();
  13. stubll.Delete(id, HttpContext.Current.Session["username"].ToString(), HttpContext.Current.Session["usertype"].ToString());
  14. break;
  15. default:
  16. break;
  17. }
  18. }

  19. public bool IsReusable {
  20. get {
  21. return false;
  22. }
  23. }

  24. }

using System; using System.Web; using System.Web.SessionState; public class delHandler : IHttpHandler,IRequiresSessionState { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string id = context.Request["id"].ToString(); string type = context.Request["type"].ToString(); switch (type) { case "stu": Graduate.BLL.Student stubll = new Graduate.BLL.Student(); stubll.Delete(id, HttpContext.Current.Session["username"].ToString(), HttpContext.Current.Session["usertype"].ToString()); break; default: break; } } public bool IsReusable { get { return false; } } }


IRequiresSessionState 是因为用到了服务器端的session,没有用到的话可以去掉