Asp.Net MVC 2 - jqgrid多重搜索示例

发布于 2024-10-15 13:40:11 字数 3738 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2024-10-22 13:40:11

这应该可以帮助其他人寻找同样的东西: http: //codefucius.blogspot.co.uk/2012/11/implementing-jqgrid-search.html

我不确定相关表搜索有什么问题 - 当然这只是简单地使用 linq 查询连接。

 var invoices = from i in db.Invoices
         join c in customer on c.Id = i.Id  // inner join related table
         join ljic in db.InvoiceCalcs 
         on i.Id equals ljic.Id into ljict // left join related table
         from ic in ljict.DefaultIfEmpty()  
         where (i.CustomerId == id && c.Name == "Jones")  // where clause (i.e. search phrase for 2 related tables)

如果问题是动态 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.

 var invoices = from i in db.Invoices
         join c in customer on c.Id = i.Id  // inner join related table
         join ljic in db.InvoiceCalcs 
         on i.Id equals ljic.Id into ljict // left join related table
         from ic in ljict.DefaultIfEmpty()  
         where (i.CustomerId == id && c.Name == "Jones")  // where clause (i.e. search phrase for 2 related tables)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文