老规矩
直接上代码
<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
---------------------------------------------------------------------------------------------------------