EasyUI笔记(六)数据表格
用asp.net结合easyui做个一个数据网格的页面,包括对数据的增删改查,界面如下:

<body><table id="tb1"></table><div id="tb" style="padding:5px"><div style="margin-bottom:5px"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="reload()">刷新</a><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="Add()">添加</a><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="edit()">修改</a><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="destroy()">删除</a></div><div>查询名称:<input type="text" id="Eq" name="Eq" style="width:150px"/><select id="order" class="easyui-combobox" style="width:100px"><option value="asc">升序</option><option value="desc">降序</option></select><a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="obj.search()">查询</a></div></div><%-- 弹窗 Start--%><div id="dlg" class="easyui-dialog" style="width:400px;height:320px;padding:10px 20px" data-options="closed:true,buttons:'#dlg-buttons'"><div class="ftitle">设备信息</div><form id="fm" method="post" novalidate="novalidate"><div class="fitem"><label>产品编号:</label><input id="ProductID" name="ProductID" class="easyui-validatebox" data-options="required:false"/></div><div class="fitem"><label>产品名称:</label><input id="Name" name="Name" class="easyui-validatebox" data-options="required:true"/></div><div class="fitem"><label>描述:</label><input id="Description" name="Description" class="easyui-validatebox" data-options="required:true"/></div><div class="fitem"><label>种类:</label><input id="Category" name="Category" class="easyui-validatebox" data-options="required:true"/></div><div class="fitem"><label>价格:</label><input id="Price" name="Price" class="easyui-validatebox" data-options="required:true,validType:'intOrFloat'"/></div></form></div><div id="dlg-buttons"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="save()" style="width:90px">Save</a><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a></div><%-- 弹窗 End --%></body>
$(function () {$('#tb1').datagrid({url: '../Handler/GetDataFormSql.ashx',width: 1200,title: '管理',method: 'get',toolbar: '#tb',//rownumbers: true,singleSelect:true,columns: [[{ field: 'ProductID', title: '编号', width: 150 },{ field: 'Name', title: '名称', width: 150 },{ field: 'Description', title: '描述', width: 250 },{ field: 'Category', title: '种类', width: 150 },{ field: 'Price', title: '价格', width: 150 }]],pagination: true,pageSize: 20,pageList: [20, 30 , 40]});})
public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";int page = 1, rows = 10;//EasyUI自带的两个参数rows与page ,表示当前页与行数if (context.Request.QueryString["rows"] != null){rows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());}if (context.Request.QueryString["page"] != null){page = int.Parse(context.Request.QueryString["page"].ToString().Trim());}//查询分页 stratIndex endIndexint stratIndex, endIndex;stratIndex = (page - 1) * rows + 1;endIndex = page * rows;//查询排序 key orderstring key = "", order = "desc";if (context.Request.QueryString["key"] != null){key = context.Request.QueryString["key"].ToString().Trim();}if (context.Request.QueryString["order"] != null){order = context.Request.QueryString["order"].ToString().Trim();}//设置模糊查询StringBuilder sqlWhere = new StringBuilder();if (key != "") {sqlWhere.AppendFormat("upper(ProductID) like '%{0}%' or "+"upper(Name) like '%{0}%' or "+"upper(Description) like '%{0}%' or " +"upper(Category) like '%{0}%' or " +"upper(Price) like '%{0}%'",key);}//查询数据库DAL.SqlHelper sqlhelper = new DAL.SqlHelper();//获取查询数据的行数int count = sqlhelper.EUGetRecordCount(sqlWhere.ToString());//封装数据到datasetDataSet ds = sqlhelper.GetListByPagebykey(sqlWhere.ToString(), order, stratIndex, endIndex);//将dataset转化为Json格式string strToJon = DAL.ToJson.DatasetJson(ds, count);context.Response.Write(strToJon);context.Response.End();}
public int EUGetRecordCount(string key){StringBuilder strSql = new StringBuilder();strSql.Append("select count(1) FROM Products ");if (key.Trim() != ""){strSql.Append("where " + key);}object obj = DBHelper.GetSingle(strSql.ToString());if (obj == null){return 0;}else{return Convert.ToInt32(obj);}}public DataSet GetListByPagebykey(string Key, string order, int startIndex, int endIndex){StringBuilder strSql = new StringBuilder();strSql.Append("SELECT * FROM ( ");strSql.Append(" SELECT ROW_NUMBER() OVER (");if (!string.IsNullOrEmpty(Key.Trim())){strSql.Append("order by T." + Key+" " + order);}else{strSql.Append("order by T.ProductID asc");}strSql.Append(")AS Row, T.* from Products T ");if (!string.IsNullOrEmpty(Key.Trim())){strSql.Append(" WHERE " + Key);}strSql.Append(" ) TT");strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);return DBHelper.Query(strSql.ToString());}
public class ToJson{/// <summary>/// DataSet转换成Json格式/// </summary>/// <param name="ds">DataSet</param>/// <returns></returns>public static string DatasetJson(DataSet ds, int total = -1){StringBuilder json = new StringBuilder();foreach (DataTable dt in ds.Tables){json.Append("{\"total\":");if (total == -1){json.Append(dt.Rows.Count);}else{json.Append(total);}json.Append(",\"rows\":");json.Append(DataTableJson(dt));json.Append("]}");} return json.ToString();}private static string DataTableJson(DataTable dt){StringBuilder jsonBuilder = new StringBuilder();jsonBuilder.Append("[");for (int i = 0; i < dt.Rows.Count; i++){jsonBuilder.Append("{");for (int j = 0; j < dt.Columns.Count; j++){jsonBuilder.Append("\"");jsonBuilder.Append(dt.Columns[j].ColumnName);jsonBuilder.Append("\":\"");jsonBuilder.Append(dt.Rows[i][j].ToString());jsonBuilder.Append("\",");}jsonBuilder.Remove(jsonBuilder.Length - 1, 1);jsonBuilder.Append("},");}jsonBuilder.Remove(jsonBuilder.Length - 1, 1);return jsonBuilder.ToString();}}
public int Add(Products model){StringBuilder strSql = new StringBuilder();strSql.Append("insert into Products(");strSql.Append("Name,Description,Category,Price)");strSql.Append(" values (");strSql.Append("@Name,@Description,@Category,@Price)");strSql.Append(";select @@IDENTITY");SqlParameter[] parameters = {new SqlParameter("@Name", SqlDbType.NVarChar,255),new SqlParameter("@Description", SqlDbType.NVarChar,255),new SqlParameter("@Category", SqlDbType.NVarChar,255),new SqlParameter("@Price ", SqlDbType.Decimal,255),};//parameters[0].Value = model.ProductID;parameters[0].Value = model.Name;parameters[1].Value = model.Description;parameters[2].Value = model.Category;parameters[3].Value = model.Price;object obj = DBHelper.GetSingle(strSql.ToString(), parameters);if (obj == null){return 0;}else{return Convert.ToInt32(obj);}}
EasyUI笔记(六)数据表格的更多相关文章
- 第二百二十二节,jQuery EasyUI,DataGrid(数据表格)组件
jQuery EasyUI,DataGrid(数据表格)组件 学习要点: 1.加载方式 2.分页功能 本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于 Pa ...
- 第二百二十四节,jQuery EasyUI,ComboGrid(数据表格下拉框)组件
jQuery EasyUI,ComboGrid(数据表格下拉框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 ComboGrid(数据表格下拉框)组件的 ...
- jQuery EasyUI/TopJUI实现数据表格的增删改查功能(不写js,纯HTML实现!!!)
jQuery EasyUI/TopJUI实现数据表格的增删改查功能(不写js,纯HTML实现!!!) 废话不多说,直接贴上代码 <table id="configEdatagrid&q ...
- Jquery EasyUI datagrid后台数据表格生成及分页详解
由于项目原因,网站后台需要对用户信息进行各种操作,有时还需要进行批量操作,所以首先需要将用户信息展示出来,查了不少资料.发现Jquery EasyUI确实是一个不错的选择,功能强大,文档也比较全面,而 ...
- (五)EasyUI使用——datagrid数据表格
DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.DataGrid的设计用于缩短开发时间,并且使开发人员不需要具备特定的知识.它是轻量级的且功能丰富.单元格合并. ...
- DataGrid( 数据表格) 组件[9]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- DataGrid( 数据表格) 组件[8]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- DataGrid( 数据表格) 组件[7]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- DataGrid( 数据表格) 组件[6]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- DataGrid( 数据表格) 组件[5]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
随机推荐
- webpack入门系列2
前面介绍了使用webpack做最基础的打包,接下来讲讲webpack的进阶. 1.使用 webpack 构建本地服务器: 想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webp ...
- ROS中3D机器人建模(一)
一,机器人建模的ROS软件包 *urdf : 机器人建模最重要的ros软件包是urdf软件包.这个软件包包含一个用于统一机器人描述格式(URDF)的C++解析器,它是一个表示机器人模型的XML文件,还 ...
- 物流跟踪API-快递单订阅
上一篇文章我们讲解了轨迹查询的接口,通过快递鸟接口可以实现实时查询物流轨迹,这次给大家推荐订阅服务功能. 为了更好的理解订阅服务,我们来做个对比, 即时查询是主动查询物流轨迹,需要我们主动调用接口才能 ...
- Python 代码风格规范(Google)
Python风格规范 分号 tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...
- Codeforces_731_C
http://codeforces.com/problemset/problem/731/C 并查集,然后找每个集合里颜色的最大数量,求集合中元素数量-这个最大数量,最后总数相加即答案. #inclu ...
- python库之matplotlib学习---图无法显示中文
在代码前面加上 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] ...
- ajax 原生js封装ajax [转]
/* 封装ajax函数 * @param {string}opt.type http连接的方式,包括POST和GET两种方式 * @param {string}opt.url 发送请求的url * @ ...
- angular 控件间的通信
先引入 设置meta元素 http://blog.sina.com.cn/s/blog_51048da70101cgea.html //设置 虚拟窗口的大小等于设备的大小 <meta name= ...
- 关于线段树的感悟(Segment Tree)
线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...
- 我说我了解集合类,面试官竟然问我为啥HashMap的负载因子不设置成1!?
在Java基础中,集合类是很关键的一块知识点,也是日常开发的时候经常会用到的.比如List.Map这些在代码中也是很常见的. 个人认为,关于HashMap的实现,JDK的工程师其实是做了很多优化的,要 ...