jquery easyui datagrid實現增加,修改,刪除方法總結
本文實例講述了jquery easyui datagrid實現增加,修改,刪除的方法。分享給大家供大家參考,具體如下:
頁面:
<body> <form id="form1" runat="server"> <table id="tt"> </table> </form> </body>
引用的JS:
<link rel="stylesheet" type="text/css" href="/script/themes/default/easyui.css" /> <link rel="stylesheet" type="text/css" href="/script/themes/icon.css" /> <script type="text/javascript" src="/script/jquery-1.4.2.min.js" </script> <script type="text/javascript" src="/script/jquery.easyui.min.js" </script> <script type="text/javascript" src="/script/locale/easyui-lang-zh_CN.js" mce_src="script/locale/easyui-lang-zh_CN.js"></script>
JS:
<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:
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的分頁按鈕,自動向后臺發(fā)送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
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,沒有用到的話可以去掉
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
- jquery Easyui Datagrid實現批量操作(編輯,刪除,添加)
- jQuery Easyui datagrid連續(xù)發(fā)送兩次請求問題
- easyui datagrid 大數據加載效率慢,優(yōu)化解決方法(推薦)
- jQuery EasyUI編輯DataGrid用combobox實現多級聯動
- jQuery Easyui DataGrid點擊某個單元格即進入編輯狀態(tài)焦點移開后保存數據
- jQuery EasyUI學習教程之datagrid點擊列表頭排序
- jQuery Easyui學習教程之實現datagrid在沒有數據時顯示相關提示內容
- jQuery EasyUI框架中的Datagrid數據表格組件結構詳解
- jQuery easyUI datagrid 增加求和統(tǒng)計行的實現代碼
- 簡介EasyUI datagrid editor combogrid搜索框的實現
- 詳解EasyUi控件中的Datagrid
相關文章
jQuery UI Dialog控件中的表單無法正常提交的解決方法
研究了頁面源碼后發(fā)現,jQuery UI Dialog控件初始化時動態(tài)生成的HTML元素被添加到頁面的尾部、form元素的后面,而原始的Dialog模板部分(其內包含表單元素)也被移到了 動態(tài)生成的HTML元素內。2010-12-12jQuery實現獲取table中鼠標click點擊位置行號與列號的方法
這篇文章主要介紹了jQuery實現獲取table中鼠標click點擊位置行號與列號的方法,涉及jQuery事件響應及針對table表格元素相關操作技巧,需要的朋友可以參考下2017-10-10