老规矩

直接上代码

                       <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的查询功能的更多相关文章

  1. 【第一篇】说说MVC+EF easyui dataGrid 动态加载分页表格

    首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...

  2. Mysql数据库优化技术之配置篇、索引篇 ( 必看 必看 转)

    转自:Mysql数据库优化技术之配置篇.索引篇 ( 必看 必看 ) (一)减少数据库访问对于可以静态化的页面,尽可能静态化对一个动态页面中可以静态的局部,采用静态化部分数据可以生成XML,或者文本文件 ...

  3. 前端工程师技能之photoshop巧用系列第三篇——切图篇

    × 目录 [1]切图信息 [2]切图步骤 [3]实战 前面的话 前端工程师除了使用photoshop进行测量之外,更重要的是要使用该软件进行切图.本文是photoshop巧用系列的第三篇——切图篇 切 ...

  4. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  5. 完善ext.grid.panel中的查询功能(紧接上一篇)

    今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...

  6. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]

    前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...

  7. Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...

  8. RabbitMQ学习总结 第一篇:理论篇

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  9. ecshop调用文章显示上一篇下一篇

    首先调用文章中的上一篇和下一篇语法为:  代码如下 复制代码 上一篇:<a href="{$next_article.url}">{$next_article.titl ...

随机推荐

  1. 初识JavaScript和面向对象

    1.javascript基本数据类型: number: 数值类型 string: 字符串类型 boolean: 布尔类型 null: 空类型 undefault:未定义类型 object: 基本数据类 ...

  2. 转载:hive分区(partiton)简介

    网上有篇关于hive的partition的使用讲解的比较好,所以转载了.原文https://blog.csdn.net/akon_vm/article/details/37832511 一.背景 1. ...

  3. openldap介绍和使用

    openldap介绍和使用 为什么会有本文? 早期,公司是没有统一认证这个东西的,所以各自玩各自的.于是, confluence一个用户体系,gitlab一个用户体系,Jenkins一个用户体系等等, ...

  4. Docker 方式部署 Solo 博客系统总结

      此篇为Docker部署方式,另有Tomcat部署方式,请参考文章<Tomcat 方式部署 Solo 博客系统总结>   最近搭建了一个博客系统,作为自己的主页,方便记录一些平时所见所闻 ...

  5. mysql数据库磁盘空间被撑爆,创建定时任务定期释放资源

    问题描述: 这是我在工作中遇到的一个问题,目前只发现mysql数据库存在该问题,Oracle和gaussDB未发现磁盘空间被占满的情况,部署堆栈服务的时候抛出了写入数据库表失败的问题,经排查,在数据库 ...

  6. sql server中的cte

    从SQL Server 2005开始,提供了CTE(Common Table Expression,公用表表达式)的语法支持. CTE是定义在SELECT.INSERT.UPDATE或DELETE语句 ...

  7. N个tomcat之间实现Session共享(写的不错,转来的)

    以下文章写的比较不错,转来的. tomcat的session共享设置如此简单为什么很少人去用.这个我说的重点. 1.自身的session如果服务器不在同一个网段会有session失效(本人使用的是阿里 ...

  8. C#之项目常用方法之静态扩展

    一般项目中我们经常用到数据Json的序列化与反序列化,为了方便在需要的地方快速使用,一般建议都封装为静态扩展方法,在需要的地方可直接使用. 而目前C#项目中序列化一般都是用的 Newtonsoft.J ...

  9. Python之基本数据类型概览

    Python之基本数据类型概览 什么是数据类型? 每一门编程语言都有自己的数据类型,例如最常见的数字1,2,3.....,字符串'小明','age','&D8'...,这些都是数据类型中的某一 ...

  10. 随笔编号-15 重构--改善既有代码的设计--Day01--学习笔记

    最近公司开发的系统在进行大批量数据查询的时候发现响应速度变得让人无法忍受,so 老大安排我进行代码重构的工作,主要目的就是为提高代码的执行效率.减小方法之间的响应时间.降低方法之间的耦合度.= =! ...