C#的WebApi 与 EasyUi的DataGrid结合生成的可分页界面
1、从数据库每次取出的数据为当前分页的数据。
2、分页用的是EasyUI 的 Pagination控件,与DataGrid是相对独立的。
3、后台数据获取是通过WebApi去获取。
4、传入参数是:pageSize、pageNumber 及其它条件。传参用的是Post方法(Get同样可以).
效果如图:
其它的不说,直接上代码:
HTML代码:
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
- <title></title>
- <meta charset="utf-8" />
- <link href="/Content/themes/default/easyui.css" rel="stylesheet" />
- <link href="/Content/themes/icon.css" rel="stylesheet" />
- <link href="../../Content/bootstrap.min.css" rel="stylesheet" />
- <link href="../../Css/BootStrapExt.css" rel="stylesheet" />
- <%-- <script src="/Scripts/jquery-1.11.3.min.js"></script>--%>
- <script src="../../JScript/jquery-easyui-1.5.1/jquery.min.js"></script>
- <script src="/Scripts/bootstrap.min.js"></script>
- <%--<script src="/Scripts/jquery.easyui-1.4.5.min.js"></script>--%>
- <script src="../../JScript/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
- <script type="text/javascript">
- //分页事件
- function RefreshPages(totalNum) {
- $('#uiPages').pagination('refresh', { // change options and refresh pager bar information
- total: totalNum,
- pageList: [10, 15, 20, 25, 30, 50, 100]
- });
- }
- $(function(){
- GetUserData(0, 0);//加载dataGrid数据
- $('#uiPages').pagination({
- onSelectPage: function (pageNumber, pageSize) {
- GetUserData(pageSize, pageNumber);
- }
- });
- });
- function GetUserData(psize,pnumber){
- var grid = $('#grid1');
- var pagesize = $('#uiPages').pagination("options").pageSize; //分页控件的PageSize
- var currIndex = $('#uiPages').pagination("options").pageNumber + 1; //分页控件的当前页数
- if (psize > 0) {
- pagesize = psize;
- currIndex = pnumber;
- }
- var lno = $("#txt_LoginNo").val();
- var lnm = $("#txt_LoginName").val();
- var jsonStr = [{ "name": "pgSize", "value": pagesize }
- , { "name": "pgIndex", "value": currIndex }
- , { "name": "txt_LoginNo", "value": lno }
- , { "name": "txt_LoginName", "value": lnm }
- ];
- var request = JSON.stringify(jsonStr);
- var uri = "/api/UsersInfo/GetUserList1";
- $.ajax({
- url: uri,
- type: 'post',
- data: request,
- contentType: "application/json",
- success: function (data) {
- //取回来的是一个DataSet,有两个表,第一个表第一行是总资料数。第二个表是当前页资料。
- RefreshPages(data.Table[0].num);
- $('#grid1').datagrid('loadData', data.Table1);
- },
- error: function (data) {
- alert(data.responseText);
- }
- });
- }
- function chBrand(val, row)
- {
- if(val==0)
- {
- return "品牌0";
- }
- else if (val=="1")
- {
- return "品牌1";
- }
- }
- </script>
- <style type="text/css">
- .panel-body {
- padding:0;
- }
- </style>
- </head>
- <body class="easyui-layout" style="overflow: hidden;">
- <form id="form1">
- <div data-options="region:'north',split:false,border:false" style="height: 35px; margin-top: 10px; padding-top: 0px; overflow: hidden;">
- <div class="row">
- <div class="col-xs-1 col-sm-1 col-lg-1" style="padding-left:30px;">
- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" id="a_addUser">Add </a>
- </div>
- <div class="col-xs-2 col-sm-2 col-lg-2">
- <div class="col-xs-5 col-padding-0 col-text-right textbox5">登录名<span style="color: red;">%</span></div>
- <div class="col-xs-7 col-padding-0">
- <input id="txt_LoginNo" class="textbox textbox-text textbox5" name="txt_LoginNo" type="text" placeholder="">
- </div>
- </div>
- <div class="col-xs-2 col-sm-2 col-lg-2">
- <div class="col-xs-5 col-padding-0 col-text-right textbox5">
- 用户姓名<span style="color: red;">%</span>
- </div>
- <div class="col-xs-7 col-padding-0 ">
- <input class="textbox textbox-text textbox5" id="txt_LoginName" name="txt_LoginName" type="text" placeholder="">
- </div>
- </div>
- <div class="col-xs-2 col-sm-2 col-lg-2">
- <div class="col-xs-12">
- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" id="a_search">Search</a>
- </div>
- </div>
- </div>
- </div>
- <div data-options="region:'center',border:false" style="border-top: 1px solid #95B8E7; margin-top: 0; padding: 0; overflow: hidden;">
- <div class="easyui-layout" data-options="fit:true">
- <div data-options="region:'north',split:false,border:false" style="height: 30px; margin:0; padding: 0px; overflow: hidden;">
- <div id="uiPages" class="easyui-pagination" data-options="total:0" style="border: 0px solid #95B8E7; margin: 0; padding: 0;">
- </div>
- </div>
- <div data-options="region:'center',border:false,fit:true" style="border: 0px solid #95B8E7; margin-top: 0; padding: 0;">
- <table id="grid1" class="easyui-datagrid" style="width:100%; height: 99%; padding:0; margin: 0;"
- data-options="singleSelect:true,collapsible:true,fit:true,border:false">
- <thead>
- <tr>
- <th data-options="field:'row',width:50,align:'center'"></th>
- <th data-options="field:'LoginNo',width:100">登录名</th>
- <th data-options="field:'LoginName',width:100">用户姓名</th>
- <th data-options="field:'AgentName',">代理商</th>
- <th data-options="field:'AgentMobile',width:100,align:'right'">代理商电话</th>
- <th data-options="field:'Brand',width:100,formatter:chBrand">所属品牌</th>
- </tr>
- </thead>
- </table>
- </div>
- </div>
- </div>
- </form>
- </body>
- </html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title></title>
<meta charset="utf-8" />
<link href="/Content/themes/default/easyui.css" rel="stylesheet" />
<link href="/Content/themes/icon.css" rel="stylesheet" />
<link href="../../Content/bootstrap.min.css" rel="stylesheet" />
<link href="../../Css/BootStrapExt.css" rel="stylesheet" /> <%-- <script src="/Scripts/jquery-1.11.3.min.js"></script>--%>
<script src="../../JScript/jquery-easyui-1.5.1/jquery.min.js"></script>
<script src="/Scripts/bootstrap.min.js"></script>
<%--<script src="/Scripts/jquery.easyui-1.4.5.min.js"></script>--%>
<script src="../../JScript/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<script type="text/javascript">
//分页事件
function RefreshPages(totalNum) {
$('#uiPages').pagination('refresh', { // change options and refresh pager bar information
total: totalNum,
pageList: [10, 15, 20, 25, 30, 50, 100]
});
} $(function(){
GetUserData(0, 0);//加载dataGrid数据
$('#uiPages').pagination({
onSelectPage: function (pageNumber, pageSize) {
GetUserData(pageSize, pageNumber);
}
}); }); function GetUserData(psize,pnumber){
var grid = $('#grid1');
var pagesize = $('#uiPages').pagination("options").pageSize; //分页控件的PageSize
var currIndex = $('#uiPages').pagination("options").pageNumber + 1; //分页控件的当前页数
if (psize > 0) {
pagesize = psize;
currIndex = pnumber;
}
var lno = $("#txt_LoginNo").val();
var lnm = $("#txt_LoginName").val();
var jsonStr = [{ "name": "pgSize", "value": pagesize }
, { "name": "pgIndex", "value": currIndex }
, { "name": "txt_LoginNo", "value": lno }
, { "name": "txt_LoginName", "value": lnm }
]; var request = JSON.stringify(jsonStr); var uri = "/api/UsersInfo/GetUserList1";
$.ajax({
url: uri,
type: 'post',
data: request,
contentType: "application/json",
success: function (data) {
//取回来的是一个DataSet,有两个表,第一个表第一行是总资料数。第二个表是当前页资料。
RefreshPages(data.Table[0].num);
$('#grid1').datagrid('loadData', data.Table1);
},
error: function (data) {
alert(data.responseText);
}
});
} function chBrand(val, row)
{
if(val==0)
{
return "品牌0";
}
else if (val=="1")
{
return "品牌1";
}
} </script>
<style type="text/css">
.panel-body {
padding:0;
}
</style>
</head>
<body class="easyui-layout" style="overflow: hidden;">
<form id="form1">
<div data-options="region:'north',split:false,border:false" style="height: 35px; margin-top: 10px; padding-top: 0px; overflow: hidden;">
<div class="row">
<div class="col-xs-1 col-sm-1 col-lg-1" style="padding-left:30px;">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" id="a_addUser">Add </a>
</div>
<div class="col-xs-2 col-sm-2 col-lg-2">
<div class="col-xs-5 col-padding-0 col-text-right textbox5">登录名<span style="color: red;">%</span></div>
<div class="col-xs-7 col-padding-0">
<input id="txt_LoginNo" class="textbox textbox-text textbox5" name="txt_LoginNo" type="text" placeholder="">
</div>
</div>
<div class="col-xs-2 col-sm-2 col-lg-2">
<div class="col-xs-5 col-padding-0 col-text-right textbox5">
用户姓名<span style="color: red;">%</span>
</div>
<div class="col-xs-7 col-padding-0 ">
<input class="textbox textbox-text textbox5" id="txt_LoginName" name="txt_LoginName" type="text" placeholder="">
</div>
</div>
<div class="col-xs-2 col-sm-2 col-lg-2">
<div class="col-xs-12">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" id="a_search">Search</a>
</div>
</div>
</div>
</div>
<div data-options="region:'center',border:false" style="border-top: 1px solid #95B8E7; margin-top: 0; padding: 0; overflow: hidden;">
<div class="easyui-layout" data-options="fit:true">
<div data-options="region:'north',split:false,border:false" style="height: 30px; margin:0; padding: 0px; overflow: hidden;">
<div id="uiPages" class="easyui-pagination" data-options="total:0" style="border: 0px solid #95B8E7; margin: 0; padding: 0;">
</div>
</div>
<div data-options="region:'center',border:false,fit:true" style="border: 0px solid #95B8E7; margin-top: 0; padding: 0;">
<table id="grid1" class="easyui-datagrid" style="width:100%; height: 99%; padding:0; margin: 0;"
data-options="singleSelect:true,collapsible:true,fit:true,border:false">
<thead>
<tr>
<th data-options="field:'row',width:50,align:'center'"></th>
<th data-options="field:'LoginNo',width:100">登录名</th>
<th data-options="field:'LoginName',width:100">用户姓名</th>
<th data-options="field:'AgentName',">代理商</th>
<th data-options="field:'AgentMobile',width:100,align:'right'">代理商电话</th>
<th data-options="field:'Brand',width:100,formatter:chBrand">所属品牌</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</form>
</body>
</html>
WebAPI代码:
- [HttpPost]
- public IHttpActionResult GetUserList1([FromBody]JArray js)
- {
- JToken jt = js.Single(e => e["name"].ToString() == "pgSize");
- int pgSize = int.Parse(jt["value"].ToString());
- jt = js.Single(e => e["name"].ToString() == "pgIndex");
- int pgCurIndex = int.Parse(jt["value"].ToString());
- jt = js.Single(e => e["name"].ToString() == "txt_LoginNo");
- string loginNo = jt["value"].ToString();
- jt = js.Single(e => e["name"].ToString() == "txt_LoginName");
- string LoginName = jt["value"].ToString();
- List<SqlParameter> Parameters = new List<SqlParameter>(); ;
- if (!string.IsNullOrWhiteSpace(loginNo))
- {
- Parameters.Add(new SqlParameter("@LoginNo", loginNo));
- }
- if (!string.IsNullOrWhiteSpace(LoginName))
- {
- Parameters.Add(new SqlParameter("@LoginName", LoginName));
- }
- int startIndex = (pgCurIndex - 1) * pgSize + 1;
- int endIndex = pgCurIndex * pgSize;
- UsersInfo info = new UsersInfo();
- UsersBLL bll = new UsersBLL(info);
- DataSet ds = bll.QueryByPage(Parameters.ToArray(), "", startIndex, endIndex);
- int recordCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
- return Ok(ds);
- }
[HttpPost]
public IHttpActionResult GetUserList1([FromBody]JArray js)
{
JToken jt = js.Single(e => e["name"].ToString() == "pgSize");
int pgSize = int.Parse(jt["value"].ToString()); jt = js.Single(e => e["name"].ToString() == "pgIndex");
int pgCurIndex = int.Parse(jt["value"].ToString()); jt = js.Single(e => e["name"].ToString() == "txt_LoginNo");
string loginNo = jt["value"].ToString(); jt = js.Single(e => e["name"].ToString() == "txt_LoginName");
string LoginName = jt["value"].ToString(); List<SqlParameter> Parameters = new List<SqlParameter>(); ;
if (!string.IsNullOrWhiteSpace(loginNo))
{
Parameters.Add(new SqlParameter("@LoginNo", loginNo));
}
if (!string.IsNullOrWhiteSpace(LoginName))
{
Parameters.Add(new SqlParameter("@LoginName", LoginName));
}
int startIndex = (pgCurIndex - 1) * pgSize + 1;
int endIndex = pgCurIndex * pgSize; UsersInfo info = new UsersInfo();
UsersBLL bll = new UsersBLL(info); DataSet ds = bll.QueryByPage(Parameters.ToArray(), "", startIndex, endIndex);
int recordCount = int.Parse(ds.Tables[0].Rows[0][0].ToString()); return Ok(ds);
}
读库的我就不贴出来了,我这里用的是自已编写的一个BLL与Module结合紧密的一个DAL层,用SqlParameter传参是因为会对公网,用这个后不需再做SQL注入过虑。
最终返回的DataSet 里有两个表,第一个表是读出资料总数,一个Count(0) num,第二个表,是当前页的资料。所以对应的我HTML的data值也有两个Table。
C#的WebApi 与 EasyUi的DataGrid结合生成的可分页界面的更多相关文章
- Easyui的datagrid结合hibernate实现数据分页
最近在学习easyui的使用,在学到datagrid的时候遇到了一些问题,终于抽点时间整理了一下,分享出来,请各位前辈高手多多指教! 1.先来看看效果,二话不说,上图直观! 2.easyui的data ...
- 运用EasyUI中datagrid读取数据库数据实现分页
1dao层 package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import org.hibernate ...
- abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之一(二十七)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- easyUI 中datagrid 返回列隐藏方法
easyui的datagrid方法返回的列,有的值不需要显示可以使用hidden(属性进行隐藏) columns : [ [{ field : 'bailClass', title : '类别', w ...
- EasyUI 中 DataGrid 控件 列 如何绑定对象中的属性
EasyUI 中 DataGrid 控件 是我们经常用到的控件之一, 但是 DataGrid 控件 在绑定显示列时却不支持对象属性绑定. 模型如下: public class Manager impl ...
- easyui的datagrid行的某一列添加链接
通过formatter方法给easyui 的datagrid 每行增加操作链接. 效果图 jsp代码: <th field="url" width="100&quo ...
- easyui的datagrid打印(转)
在使用easyui插件的时候,使用最多的应该是datagrid插件.有时候根据客户需求,可能需要将datagrid内容进行打印,这时候如果直接调用window.print,可能由于easyui的dat ...
- EasyUI的datagrid分页
EasyUI的datagrid分页 前台代码: <script type="text/javascript"> $(function () { //查询 search( ...
- easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题
做一个用easyui的datagrid显示数据的功能时发现表格的列头与数据错位了,而且这个现象不总是能重现,一直没搞清楚原因.后来偶然在控制台看出了一点端倪: 推测表头或者单元格的class名应该是用 ...
随机推荐
- 手把手教你用Python代码实现微信聊天机器人 -- Python wxpy
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 来学习了,微信聊天机器人. 环境要求: Windows / Li ...
- mysql sum()函数 , 计算总和
mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...
- 将爬取的实习僧网站数据传入HDFS
一.引言: 作为一名大三的学生,找实习对于我们而言是迫在眉睫的.实习作为迈入工作的第一步,它的重要性不言而喻,一份好的实习很大程度上决定了我们以后的职业规划. 那么,一份好的实习应该考量哪些因素呢? ...
- 部分NLP工程师面试题总结
面试题 https://www.cnblogs.com/CheeseZH/p/11927577.html 其他 大数据相关面试题 https://www.cnblogs.com/CheeseZH/p/ ...
- SpringMVC 给请求路径加上统一前缀
最开始想到的是通过硬编码的方式手动在每个路径上加上前缀, 后面发现这种方式太不智能了,万一要修改那还不得改死, Spring既然支持EL表达式, 那能不能通过EL表达式的方式去读取配置文件里面的属性来 ...
- Xamarin图表开发基础教程(8)OxyPlot框架
Xamarin图表开发基础教程(8)OxyPlot框架 [示例OxyPlotFormsDemo]在Xamarin.Forms中实现线图的显示. (1)打开Xamarin.Forms项目. (2)将Ox ...
- visual studio(vs)初始化
cmd 进入到 devenv.exe 所在目录 执行一下命令 devenv.exe /setup /resetuserdata /resetsettings
- 认领该应用 apk空白包签名 方法
起因: apicloud开发的项目,上架应用宝市场,被要求做这个 解决方法: 下载签名工具,并解压缩.解压缩.解压后是两个文件夹,选择keystore签名工具. 下载地址: linux 签名工具(命令 ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...