【第十一篇】这一篇来说说MVC+EF+easyui datagrid的查询功能
老规矩
直接上代码
<form class="form-horizontal">
<div class="box-body">
<div class="row">
<div class="form-group col-xs-1" style="width: 390px;">
<label for="CersNo" class="col-sm-2 control-label">证书</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="CersNo" placeholder="证书号">
</div>
</div>
<div class="form-group col-xs-2" style="width: 390px;">
<label for="StoneID" class="col-sm-2 control-label">货号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="StoneID" placeholder="货号">
</div>
</div>
</div>
<div class="row">
<div class="form-group col-xs-1" style="width: 390px;">
<label for="size" class="col-sm-2 control-label">重量</label>
<div class="row">
<div class="col-xs-3">
<input type="text" class="form-control col-xs-3" style="width: 117px;" id="minSize" placeholder="minSize">
<hr style="width: 20px; border: solid 1px #D2D6DE; position: absolute; left: 137px; bottom: -5px;" />
</div>
<div class="col-xs-4">
<input type="text" class="form-control col-xs-4" style="width: 117px;" id="maxSize" placeholder="maxSize">
</div>
</div>
</div>
<div class="form-group col-xs-1" style="width: 390px;">
<button type="reset" class="btn btn-default pull-right" style="width: 130px; margin-right: 15px;">重置</button>
<button type="button" onclick="onSearch()" class="btn btn-info pull-right" style="width: 130px; margin-right: 5px;">查询</button>
</div>
</div>
</div>
</form> <table id="proGrid">
</table>
这里我想先上个效果图

我就不分步骤了,直接来代码
先是动态表格的生成,注意看column里面的值,后面我会说到为什么会这么写
<script type="text/javascript">
$(function () {
LoadGrid();
})
//加载表格!!!
function LoadGrid() {
$('#proGrid').datagrid({
width: 900,
striped: true, //交替条纹
fitColumns: false, //防止水平滚动
fit: true,//自动补全
iconCls: "icon-save",//图标
idField: 'UserName', //唯一列
url: "GetStock",
dataType: "json",
singleSelect: true, //设置为true将只允许选择一行
loadMsg: '正在拼命加载,请稍后...',
rownumbers: true, //显示行数
pagination: true, //底部分页工具栏
nowrap: true, //截取超出部分的数据
checkOnSelect: true,//点击一行的时候 checkbox checked(选择)/unchecked(取消选择)
pageNumber: 1,//初始化分页码。
pageSize: 20, //初始化每页记录数。
pageList: [10, 20, 30, 50, 100, 500, 1000], //初始化每页记录数列表
showFooter: false, //定义是否显示行底
columns: column,
onLoadError: function () {
layer.msg("没有查询到记录!");
}
});
};
var column = [[
{ field: "StockId", title: "编号", width: 80, align: "center", sortable: "true", hidden: true },
{ field: "ProductId", title: "产品编号", width: 80, align: "center", sortable: "true", hidden: true },
{ field: "ProductNum", title: "数量", width: 100, align: "center" },
{ field: "IsOnline", title: "是否在商城", width: 100, align: "center" },
{
field: "StoneID", title: "货号", width: 130, align: "center", formatter: function (value, row, index) {
if (row.Product != null) {
return row.Product.StoneID;
}
}
},
{
field: "SuppliersId", title: "供应商编号", width: 100, align: "center", sortable: "true", formatter: function (value, row, index) {
if (row.Product != null) {
return row.Product.SuppliersId;
}
}
},
{
field: "Shape", title: "形状", width: 100, align: "center", formatter: function (value, row, index) {
if (row.Product != null) {
return row.Product.Shape;
}
}
},
因为列太多了,我就不一一贴出来了,都是一样的。
下面的是搜索的方法,用到的是datagrid的load事件,注意带参过去的
function onSearch() {
if (checkInput()) {
$("#proGrid").datagrid('load', {
CersNo: $("#CersNo").val().trim(),
StoneID: $("#StoneID").val().trim(),
minSize: $("#minSize").val().trim(),
maxSize: $("#maxSize").val().trim()
});
}
}
接着是控制器的代码,这里接收了参数
public JsonResult GetStock(int? page, int? rows)
{
page = page == null ? : page;
rows = rows == null ? : rows; string CersNo = Request["CersNo"];
string StoneID = Request["StoneID"];
double minSize = ;
double maxSize = ;
if (!string.IsNullOrEmpty(Request.Params["minSize"]))
{
minSize = Convert.ToDouble(Request.Params["minSize"]);
}
if (!string.IsNullOrEmpty(Request.Params["maxSize"]))
{
maxSize = Convert.ToDouble(Request.Params["maxSize"]);
} List<Product> proList = pService.GetAllPro();
List<Product> prodList = new List<Product>();
if (proList != null)
{
for (int i = ; i < proList.Count; i++)
{
Product p = new Product();
p.ProductId = proList[i].ProductId;
p.StoneID = proList[i].StoneID;
p.SuppliersId = proList[i].SuppliersId;
p.Shape = proList[i].Shape;
p.Size = proList[i].Size;
..............
prodList.Add(p);
}
} List<Stock> produList = stService.SearchList(CersNo, StoneID, minSize, maxSize, Convert.ToInt32(page), Convert.ToInt32(rows));
List<Stock> pList = new List<Stock>();
if (proList != null)
{
for (int i = ; i < produList.Count; i++)
{
Stock p = new Stock();
p.StockId = produList[i].StockId;
p.ProductId = produList[i].ProductId;
p.ProductNum = produList[i].ProductNum;
p.IsOnline = produList[i].IsOnline == "" ? "是" : "否";
if (prodList!=null)
{
Product pr = prodList.SingleOrDefault(m => m.ProductId == produList[i].ProductId);
if (pr!=null)
{
p.Product = pr;
}
}
pList.Add(p);
}
var json = new
{
total = stService.GetTotal(CersNo, StoneID, minSize, maxSize),
rows = pList
};
return Json(json, JsonRequestBehavior.AllowGet);
}
else
{
return null;
}
}
代码看了之后,我想说说为什么我要用集合转存集合
我贴一个代码块出来
/// <summary>
/// 搜索
/// </summary>
/// <param name="CersNo"></param>
/// <param name="StoneID"></param>
/// <param name="minSize"></param>
/// <param name="maxSize"></param>
/// <param name="page"></param>
/// <param name="rows"></param>
/// <returns></returns>
public List<Stock> SearchList(string CersNo, string StoneID, double minSize, double maxSize, int page, int rows)
{
using (diamondEntities entity = new diamondEntities())
{
IQueryable<Stock> proList = null;
if (string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize == && maxSize == )
{//查询全部
proList = entity.Stocks.OrderBy(a => a.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize != && maxSize != )
{//重量区间
proList = entity.Stocks.Where<Stock>(a => a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (!string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize == && maxSize == )
{//证书编号和货号
proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo) && a.Product.StoneID.Equals(StoneID)).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (!string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize != && maxSize != )
{//证书和重量
proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo) && a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize != && maxSize != )
{//货号和重量
proList = entity.Stocks.Where<Stock>(a => a.Product.StoneID.Equals(StoneID) && a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (!string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize == && maxSize == )
{//只有证书
proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo)).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else if (string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize == && maxSize == )
{//只有货号
proList = entity.Stocks.Where<Stock>(a => a.Product.StoneID.Equals(StoneID)).OrderBy(m => m.StockId).Skip((page - ) * rows).Take(rows);
List<Stock> pList = proList.ToList();
if (pList.Count > )
{
return pList;
}
else
{
return null;
}
}
else
{
return null;
}
}
}
有没有看见,我用的using它会自动释放掉ef的资源
在程序运行时会报错 object资源已释放什么的
所以才用了另外一个集合来转存所有的数据
同时碰见了一个问题
public partial class Stock
{
public decimal StockId { get; set; }
public Nullable<decimal> ProductId { get; set; }
public Nullable<int> ProductNum { get; set; }
public string IsOnline { get; set; } public virtual Product Product { get; set; }
}
这是EF自动生成的数据模型,有一个Product的模型,我的页面主要显示的是它的所有属性
因此才会用另外的集合存起来
这个也是因为会报Object资源已释放的错
至此就完成了模糊查询 区间查询
---------------------------------------------------------------------------------------------------------
转载请记得说明作者和出处哦-.-
作者:KingDuDu
原文出处:https://www.cnblogs.com/kingdudu/articles/4762667.html
---------------------------------------------------------------------------------------------------------
【第十一篇】这一篇来说说MVC+EF+easyui datagrid的查询功能的更多相关文章
- 【第一篇】说说MVC+EF easyui dataGrid 动态加载分页表格
首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...
- Mysql数据库优化技术之配置篇、索引篇 ( 必看 必看 转)
转自:Mysql数据库优化技术之配置篇.索引篇 ( 必看 必看 ) (一)减少数据库访问对于可以静态化的页面,尽可能静态化对一个动态页面中可以静态的局部,采用静态化部分数据可以生成XML,或者文本文件 ...
- 前端工程师技能之photoshop巧用系列第三篇——切图篇
× 目录 [1]切图信息 [2]切图步骤 [3]实战 前面的话 前端工程师除了使用photoshop进行测量之外,更重要的是要使用该软件进行切图.本文是photoshop巧用系列的第三篇——切图篇 切 ...
- [转]Android开源项目第二篇——工具库篇
本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...
- 完善ext.grid.panel中的查询功能(紧接上一篇)
今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...
- Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]
前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...
- Android开源项目第二篇——工具库篇
本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...
- RabbitMQ学习总结 第一篇:理论篇
目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...
- ecshop调用文章显示上一篇下一篇
首先调用文章中的上一篇和下一篇语法为: 代码如下 复制代码 上一篇:<a href="{$next_article.url}">{$next_article.titl ...
随机推荐
- Android UI控件常用库汇总
现在App的开发已经是非常成熟,涌现了一大批开源的工具.这些项目能够提高我们的搬砖效率.以下是一些在开发中比较常使用的控件和库. ListView WaveSwipeRefreshLayout 水滴效 ...
- StudyAndroid.2 Activity生命周期
onCreate(): 当我们点击activity的时候,系统会调用activity的oncreate()方法,在这个方法中我们会初始化当前布局setContentLayout()方法. onStar ...
- Appium+python自动化(三十四)- 有图有真相,很美很精彩 - 屏幕截图和Android APP类型简介(超详解)
简介 在实际自动化项目运行过程中,很多时候App可以会出现各种异常,为了更好的定位问题,除了捕捉日志我们还需要对运行时的设备状态来进行截屏.从而达到一种“有图有真相”的效果. 截图方法 方法1 sav ...
- windows安装nginx、mysql等软件并加入系统服务启动详细
windows类系统安装nginx.mysql软件 (PS:windows系统环境中设置完nginx.mysql环境变量,需要重新启动系统才会生效.) 一.NGINX:首先下载windows版ngin ...
- SpringMVC源码分析3:DispatcherServlet的初始化与请求转发
在我们第一次学Servlet编程,学java web的时候,还没有那么多框架.我们开发一个简单的功能要做的事情很简单,就是继承HttpServlet,根据需要重写一下doGet,doPost方法,跳转 ...
- spring-boot-plus V1.2.2 发布,5 Minutes Finish CRUD
更新日志 CHANGELOG [V1.2.2-RELEASE] 2019.08.26
- tf.name_scope() 和 tf.variable_scope() 的用法和玄机
https://my.oschina.net/liusicong/blog/1593467
- python+unittest框架第六天unittest之优化测试报告
今天的内容主要是,用第三方的HTMLRUNner 第三方的报告来优化之前第五天批量执行案例的测试报告.案例的部分看第五天的批量执行笔记~ HTMLRUNner他可以生成更美观的测试报告,基于前辈造的车 ...
- 安排:《蚂蚁花呗1234面:Redis+分布式架构+MySQL+linux+红黑树》
前言: 大厂面试机会难得,为了提高面试通关率,建议朋友们在面试前先复盘自己的知识栈,依据掌握程度划分重要.优先级,系统地去学习!如果不准备充分就去参加面试,既会失去进入大厂的机会,更是对自己的不负责. ...
- hibernate 报错com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
解释:JavaBean的主键类型只能是int类型,因为在映射关系配置是自动增长的,String类型是不能自动增长的,所以会报错.