Asp.Net MVC 2 - jqgrid多重搜索示例
我正在使用 MVC 2 和实体框架实现 jqGrid。我想利用它的多种搜索功能与分页/排序以及 CRUD 交互相结合。该网格将由两个表组成,因此搜索功能也需要能够搜索相关表。我目前已经进行了分页/排序,并且客户端返回了控制器端所需的所有搜索过滤器。我正在寻找一个示例,说明如何针对实体框架实现所有各种搜索运算符,同时仍然支持分页/排序和相关表搜索。
我当前的客户端脚本:
$(document).ready(function () {
$('#grid').jqGrid({
colNames: ['TypeId', 'Type', 'CR Active', 'Category'],
colModel: [
{ name: 'TYPE_ID', index: 'TYPE_ID', hidden: true, search: false },
{ name: 'TYPE', index: 'TYPE', sortable: true, hidden: false },
{ name: 'CR_ACTIVE', index: 'CR_ACTIVE', align: 'right', sortable: true, hidden: false },
{ name: 'description', index: 'description', align: 'right', sortable: true, hidden: false }
],
pager: jQuery('#pager'),
sortname: 'TYPE',
rowNum: 10,
rowList: [10, 20, 50],
sortorder: "asc",
width: 600,
height: 250,
datatype: 'json',
caption: 'Available Types',
viewrecords: true,
mtype: 'GET',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
userdata: "userdata"
},
url: "/Type/GetData"
}).navGrid('#pager', { view: false, del: true, add: true, edit: true },
{}, // default settings for edit
{}, // default settings for add
{}, // delete instead that del:false we need this
{closeOnEscape: true, multipleSearch: true, closeAfterSearch: true }, // search options
{} /* view parameters*/
);
});
我的控制器:
public JsonResult GetData(GridSettings grid)
{
using (IWE dataContext = new IWE())
{
var query = from host in dataContext.LKTYPE
select new
{
TYPE_ID = host.TYPE_ID,
TYPE = host.TYPE,
CR_ACTIVE = host.CR_ACTIVE,
description = host.VWEPICORCATEGORY.description
};
////sorting
query = query.OrderBy(grid.SortColumn, grid.SortOrder);
//count
var count = query.Count();
//paging
var data = query.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray();
//converting in grid format
var result = new
{
total = (int)Math.Ceiling((double)count / grid.PageSize),
page = grid.PageIndex,
records = count,
rows = data.ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
}
}
GridSettings 定义为:
[ModelBinder(typeof(GridModelBinder))]
public class GridSettings
{
public bool IsSearch { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; }
public string SortColumn { get; set; }
public string SortOrder { get; set; }
public Filter Where { get; set; }
}
因此,我将从客户端返回所有分页/排序和搜索子句。
如果有人有一个关于如何实现这一点的好例子,我将不胜感激。我逐步完成了代码项目示例,但它仅包含一些可能的搜索运算符的示例,并且当您打开其他运算符时会出现查询中断。更不用说它似乎不支持在网格相关表上进行搜索(http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=58357&av=73016)。这个网站有一个很好的例子 http://www.trirand.com/blog/jqgrid/jqgrid .html,但服务器端的事情是用 PHP 和 MySQL 完成的。目前我对此了解不多,但如果它能满足我的需要,本地搜索也是一种可能。
提前致谢, 比利
I am in the process of implementing the jqGrid with MVC 2 and the entity framework. I would like to take advantage of it's multiple search functionality combined with paging/sorting as well as CRUD interaction. The grid will consist of two tables so the search functionality would need to be able to search the related table as well. I currently have the paging/sorting down, as well as the client side returning all the search filters needed on the controller side. I am looking for an example of how to implement all of the various search operators against the entity framework while still supporting the paging/sorting and related table search.
My current client side script:
$(document).ready(function () {
$('#grid').jqGrid({
colNames: ['TypeId', 'Type', 'CR Active', 'Category'],
colModel: [
{ name: 'TYPE_ID', index: 'TYPE_ID', hidden: true, search: false },
{ name: 'TYPE', index: 'TYPE', sortable: true, hidden: false },
{ name: 'CR_ACTIVE', index: 'CR_ACTIVE', align: 'right', sortable: true, hidden: false },
{ name: 'description', index: 'description', align: 'right', sortable: true, hidden: false }
],
pager: jQuery('#pager'),
sortname: 'TYPE',
rowNum: 10,
rowList: [10, 20, 50],
sortorder: "asc",
width: 600,
height: 250,
datatype: 'json',
caption: 'Available Types',
viewrecords: true,
mtype: 'GET',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
userdata: "userdata"
},
url: "/Type/GetData"
}).navGrid('#pager', { view: false, del: true, add: true, edit: true },
{}, // default settings for edit
{}, // default settings for add
{}, // delete instead that del:false we need this
{closeOnEscape: true, multipleSearch: true, closeAfterSearch: true }, // search options
{} /* view parameters*/
);
});
My controller:
public JsonResult GetData(GridSettings grid)
{
using (IWE dataContext = new IWE())
{
var query = from host in dataContext.LKTYPE
select new
{
TYPE_ID = host.TYPE_ID,
TYPE = host.TYPE,
CR_ACTIVE = host.CR_ACTIVE,
description = host.VWEPICORCATEGORY.description
};
////sorting
query = query.OrderBy(grid.SortColumn, grid.SortOrder);
//count
var count = query.Count();
//paging
var data = query.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray();
//converting in grid format
var result = new
{
total = (int)Math.Ceiling((double)count / grid.PageSize),
page = grid.PageIndex,
records = count,
rows = data.ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
}
}
GridSettings is defined as:
[ModelBinder(typeof(GridModelBinder))]
public class GridSettings
{
public bool IsSearch { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; }
public string SortColumn { get; set; }
public string SortOrder { get; set; }
public Filter Where { get; set; }
}
So with that i am getting all my paging/sorting and search clauses back from the client.
If anyone has a good example on how to accomplish this i would appreciate it. I stepped through the code project example, but it only has an example of a few of the possible search operators and when you open the others up the query breaks. Not to mention it doesn't appear to support a search on the grids related table(http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=58357&av=73016). This site has a nice example http://www.trirand.com/blog/jqgrid/jqgrid.html, but the server side of things is done in PHP and MySQL. I don't know much about it at this point but local searching is also a possibility if it will do what i need it to.
Thanks in advance,
Billy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以帮助其他人寻找同样的东西: http: //codefucius.blogspot.co.uk/2012/11/implementing-jqgrid-search.html
我不确定相关表搜索有什么问题 - 当然这只是简单地使用 linq 查询连接。
如果问题是动态 linq 中的列名,则在列前加上表名,即 Customer.Name == "Jones" 等。另一种方法是使用 SQL 视图来连接表并进行查询,这也可能带来性能优势。
This should help anyone else looking for the same sort of thing: http://codefucius.blogspot.co.uk/2012/11/implementing-jqgrid-search.html
I'm not sure what problem there is with a related table search - surely that's just simply using linq query joins.
If the problem is the names of the columns in dynamic linq, then prefix the column with the table name, i.e. Customer.Name == "Jones" etc. An alternative is just to use a SQL view to join the tables and query that, which could have performance benefits too.