datagrid在MVC中的运用02-结合搜索
本文接着上一篇,来体验给datagrid加上搜索功能。主要涉及到:
※ 把一个div与datagrid相关起来
※ datagrid接收查询参数
※ 查询参数的封装
效果图:
查询参数封装
分页相关的是每个页面都能用到的,所以把分页相关的封装成基类。
与查询相关的,封装成继承基类的子类。
public class PageParam
{
public int PageSize { get; set; }
public int PageIndex { get; set; }
}
public class BookParam : PageParam
{
public string ItemId { get; set; }
public string ProductId { get; set; }
}
查询Book的服务类方法就要考虑分页和查询参数


展开using System.Linq;
using DataGridInMvc.Models;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities; namespace DataGridInMvc.Helper
{
public class BookService
{
public IEnumerable<Book> LoadPageBookData(BookParam param, out int total)
{
//创建Book的集合
var books = new List<Book>();
for (int i = 0; i < 30; i++)
{
books.Add(new Book()
{
ItemId = "EST-" + i,
ProductId = "AV-SB-" + i,
ListPrice = (i + 5) * 10m,
UnitCost = (i + 2) * 10m,
Attr1 = "Attr" + i,
Status = (short)0
});
} //搜索逻辑
if (!string.IsNullOrEmpty(param.ItemId))
{
books = books.Where(b => b.ItemId.Contains(param.ItemId)).ToList();
} if (!string.IsNullOrEmpty(param.ProductId))
{
books = books.Where(b => b.ProductId.Contains(param.ProductId)).ToList();
} total = books.Count();
var result = books.OrderBy(b => b.ItemId)
.Skip(param.PageSize*(param.PageIndex - 1))
.Take(param.PageSize);
return result;
}
}
}
视图
datagrid的toolbar属性能把div与datagrid相关起来,并显示到datagrid的顶部。
dtagrid的queryParams属性能接受查询参数,传递给Controller.
点击查询按钮的时候,需要带上参数。


展开 <link href="~/Content/themes/default/easyui.css" rel="stylesheet" />
<link href="~/Content/themes/icon.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function() {
initData(); $('#btnQuery').click(function() {
initData(initQuery());
});
}); function initData(params) {
$('#tt').datagrid({
url: 'Home/GetData',
width: 500,
height: 350,
title: 'Book列表',
iconCls: 'icon-save',
fitColumns: true,
rownumbers: true, //是否加行号
pagination: true, //是否显式分页
pageSize: 15, //页容量,必须和pageList对应起来,否则会报错
pageNumber: 2, //默认显示第几页
pageList: [15, 30, 45],//分页中下拉选项的数值
columns: [[
//book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1
{ field: 'ItemId', title: '主键', sortable: true },
{ field: 'ProductId', title: '产品编号' },
{ field: 'Attr1', title: '属性' },
{ field: 'UnitCost', title: '成本价' },
{ field: 'ListPrice', title: '零售价' },
{ field: 'Status', title: '状态' }
]],
toolbar: '#tb',//搜索div
queryParams: params //搜索json });
} function changeP() {
var dg = $('#tt');
dg.datagrid('loadData', []); //重新加载数据
dg.datagrid({ pagePosition: $('#p-pos').val() }); //分页位置
dg.datagrid('getPager').pagination({ //分页样式、内容
layout: ['list', 'sep', 'first', 'prev', 'sep', $('#p-style').val(), 'sep', 'next', 'last', 'sep', 'refresh']
});
} //表单查询值组成json对象
function initQuery() {
var queryParams = {
ItemId: $('#itemid').val(),
ProductId: $('#productid').val()
};
return queryParams;
}
</script>
<p>
选择分页显示位置:
<select id="p-pos" onchange="changeP()">
<option>bottom</option>
<option>top</option>
<option>both</option>
</select>
选择分页显示样式
<select id="p-style" onchange="changeP()">
<option>manual</option>
<option>links</option>
</select>
</p> <table id="tt">
</table> <!--搜索开始-->
<div id="tb" style="padding:3px">
<span>主键:</span>
<input id="itemid" style="line-height:22px;border:1px solid #ccc">
<span>产品编号:</span>
<input id="productid" style="line-height:22px;border:1px solid #ccc">
<a href="#" class="easyui-linkbutton" plain="false" id="btnQuery">搜索</a>
</div>
<!--搜索结束—>
Controller


展开using System.Web;
using System.Web.Mvc;
using DataGridInMvc.Helper;
using DataGridInMvc.Models; namespace DataGridInMvc.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
} //
public ActionResult GetData()
{
//接收datagrid传来的参数
int pageIndex = int.Parse(Request["page"]);
int pageSize = int.Parse(Request["rows"]); //接收搜索参数
string itemId = Request["ItemId"];
string productId = Request["ProductId"]; //构建得到分页数据方法所需的参数
var temp = new BookParam()
{
PageIndex = pageIndex,
PageSize = pageSize,
ItemId = itemId,
ProductId = productId
}; //分页数据方法的输出参数
int totalNum = 0; var service = new BookService();
var books = service.LoadPageBookData(temp, out totalNum); var result = from book in books
select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1}; //total,rows是前台datagrid所需要的
var jsonResult = new {total = totalNum, rows = result}; //把json对象序列化成字符串
string str = JsonSerializeHelper.SerializeToJson(jsonResult);
return Content(str);
}
}
}
JsonSerializeHelper静态类在上一篇介绍过。
datagrid在MVC中的运用02-结合搜索的更多相关文章
- datagrid在MVC中的运用05-加入时间搜索条件,枚举填充下拉框
本文主要来体验在搜索区域增加更多的搜索条件,主要包括: ※ 使用jQuery ui的datepicker显示时间,设置显示格式.样式. ※ 设置jQuery ui的onClose事件,使开始和结束时间 ...
- datagrid在MVC中的运用01-基本属性并实现分页
本文体验jQuery EasyUI的datagrid在MVC中的应用.主要涉及到: ※ datagrid的基本属性和方法 ※ datagrid分页在前后台的实现 最终效果: 与视图显示对应的view ...
- datagrid在MVC中的运用07-实现Master-Detail(使用PartialView)
本文主要体验用jQuery Easyui的datagrid来实现Master-Detail主次表.谢谢Kevin的博文,助我打开了思路. 主表显示所有的Category,当点击主表的展开按钮,显示该C ...
- jQuery EasyUI DataGrid在MVC中的运用-基本属性并实现分页
※ datagrid的基本属性和方法 ※ datagrid分页在前后台的实现 最终效果: 与视图显示对应的view model public class Book public strin ...
- datagrid在MVC中的运用10-勾选
本文体验与勾选有关的特性. 需要加载的books.json 展开{ "total": 4, "rows": [ { "productid": ...
- datagrid在MVC中的运用09-实现排序
本文体验datagrid的排序. □ 思路 当点击datagrid的标题,视图传递给Controller的Form Data类似这样:page=1&rows=10&sort=Custo ...
- datagrid在MVC中的运用06-固定连续列
本文主要体验datagrid的frozenColumns属性. □ frozenColumns效果: 在frozenColumns的列将保持不动,而其他列横向滚动. □ frozenColumns效果 ...
- datagrid在MVC中的运用08-实现Master-Detail(使用子datagrid)
本文主要通过一个子datagrid来实现主次表.谢谢Kevin的博文. 代码部分与http://www.cnblogs.com/darrenji/p/3576258.html相似,这里只列出不一样的地 ...
- datagrid在MVC中的运用04-同时添加搜索和操作区域
本文介绍在datagrid上同时添加搜索和操作区域. 仅仅是增加操作区域 □ 方法1 $('#dg').datagrid({ toolbar: '#tb' }); <div id="t ...
随机推荐
- emacs设置了单例模式后无法设定文件关联解决办法
emacs设置单例模式的本质就是使用下列参数启动: C:\emacs-24.5\bin\emacsclientw.exe --no-wait --alternate-editor="C:\e ...
- OutLook中添加Exchange失败问题
问题: 在邮件中添加账户后,打开outlook时报出错误:无法启动 Microsoft Outlook. 无法打开 Outlook 窗口. 无法打开此文件夹集合. 必须先使用当前的配置文件连接到 Mi ...
- 移动端布局 - REM方式
默认以宽度为640px的设计稿为基准页面,然后通过JS获取当前显示设备的尺寸,对应的调整 html 标签的font-size大小,从而实现通过以rem为单位的移动端布局适配. 具体代码 (functi ...
- 企业级Docker Registry —— Harbor搭建和使用
本节内容: Harbor介绍 安装部署Harbor 环境要求 环境信息 安装部署harbor 配置harbor 配置存储 完成安装和启动harbor 访问Harbor 修改管理员密码 启动后相关容器 ...
- 《精通Python设计模式》学习结构型之MVC模式
这个就不需要多评论了, 哪个主流的PYTHON的WEB框架都有这些模式实现哈. quotes = ('A man is not complete until he is married. Then h ...
- 《精通Python设计模式》学习结构型之外观模式
这个我在工作中也有所应用的. 就是在真正的实现层上面,再封装一个函数的调用的. 这样就可以在内层函数作真正实现, 而外层调用函数对外开放, 隔离内外的变化性. from enum import Enu ...
- CCF CSP 201503-1 图像旋转
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201503-1 图像旋转 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆 ...
- 【POJ】1026.Cipher
题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...
- 20169211《Linux内核原理与分析》 第十周作业
一.Linux内核之进程地址空间学习总结 Linux内核除了要管理物理内存还需要管理虚拟内存.用户进程的地址空间就是虚拟内存的一部分.每个用户进程都独有一个地址空间.由于是虚拟化的内存,所以从每个进程 ...
- Am335x SPI 驱动测试
内核版本:3.14.65 CPU:Am335x 1.编译内核: make menuconfig Device Drivers -> <*>SPI support -> < ...