jquery easyui datagrid实现增加,修改,删除方法总结
本文实例讲述了jqueryeasyuidatagrid实现增加,修改,删除的方法。分享给大家供大家参考,具体如下:
页面:
<body> <formid="form1"runat="server"> <tableid="tt"> </table> </form> </body>
引用的JS:
<linkrel="stylesheet"type="text/css"href="/script/themes/default/easyui.css"/> <linkrel="stylesheet"type="text/css"href="/script/themes/icon.css"/> <scripttype="text/javascript"src="/script/jquery-1.4.2.min.js"</script> <scripttype="text/javascript"src="/script/jquery.easyui.min.js"</script> <scripttype="text/javascript"src="/script/locale/easyui-lang-zh_CN.js"mce_src="script/locale/easyui-lang-zh_CN.js"></script>
JS:
<scripttype="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){
vars='<ahref="#"mce_href="#"onclick="view(\''+rec.xsbh+'\')">查看</a>';
vare='<ahref="#"mce_href="#"onclick="edit(\''+rec.xsbh+'\')">编辑</a>';
vard='<ahref="#"mce_href="#"onclick="del(\''+index+'\')">删除</a>';
returns+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
});
})
functionview(bh)//转到查看页面
{
window.location.href='StuView.aspx?id='+bh+'&page=stu';
//varrow=$('#tt').datagrid('getSelected');
//if(row)
//{
//alert(row.xsbh);
//}
}
functionedit(bh)//转到编辑页面
{
window.location.href='StuEdit.aspx?id='+bh;
}
functiondel(index){//删除操作
$.messager.confirm('确认','确认删除?',function(row){
if(row){
varselectedRow=$('#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:
usingSystem;
usingSystem.Web;
usingSystem.Data;
usingSystem.Text;
publicclassstudentHandler:IHttpHandler{
publicvoidProcessRequest(HttpContextcontext){
context.Response.ContentType="text/plain";
DataSetds=newDataSet();
//点击datagrid的分页按钮,自动向后台发送2个参数,rows和page,代表每页记录数和页索引
introw=int.Parse(context.Request["rows"].ToString());
intpage=int.Parse(context.Request["page"].ToString());
ds=GetContent(row,page);
stringtext=json.Dataset2Json(ds);
context.Response.Write(text);
}
privateDataSetGetContent(intpagesize,intpageindex)
{
Graduate.BLL.Studentbll=newGraduate.BLL.Student();
returnbll.GetList(pagesize,pageindex);
}
publicboolIsReusable{
get{
returnfalse;
}
}
}
删除ashx
usingSystem;
usingSystem.Web;
usingSystem.Web.SessionState;
publicclassdelHandler:IHttpHandler,IRequiresSessionState{
publicvoidProcessRequest(HttpContextcontext){
context.Response.ContentType="text/plain";
stringid=context.Request["id"].ToString();
stringtype=context.Request["type"].ToString();
switch(type)
{
case"stu":
Graduate.BLL.Studentstubll=newGraduate.BLL.Student();
stubll.Delete(id,HttpContext.Current.Session["username"].ToString(),HttpContext.Current.Session["usertype"].ToString());
break;
default:
break;
}
}
publicboolIsReusable{
get{
returnfalse;
}
}
}
IRequiresSessionState是因为用到了服务器端的session,没有用到的话可以去掉
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。