如何根据上一页选择过滤 Telerik 网格

发布于 2024-10-21 21:13:40 字数 605 浏览 5 评论 0原文

我想做的:-

索引页面将有一个类别下拉菜单,选择 1 然后提交,重定向到 telerik 网格页面,所有记录都来自所选类别的大表。

例如,宠物商店,商店有哪种类型的宠物的下拉列表,然后在下一页上的网格中填充了商店今天可用的该类型的所有宠物。

已经对日期过滤器进行了排序,因为它已应用于数据绑定。

数据库是通过 edmx 连接的,它有 2 个没有关系的表,但有一个类别/宠物表,其中详细介绍了每个类别/宠物,然后有一个记录表,其中有一个类别/宠物列,其中 2表有一个共同的字段。

我一直试图通过使用 ViewData 来实现这一点,它对于下拉 <%: Html.DropDownList("category", (SelectList)ViewData["CategoryList"])%> 效果非常好。但无法填充下一页上的网格。

所以有点像 http://demos.telerik.com/aspnet-mvc/grid/selectionserverside 但如果可能的话,使用下拉菜单并跨 2 页。

What I'm trying to do:-

Index page would have a dropdown of say categories, select 1 then submit, redirected to telerik grid page with all records kept from a big table of selected category.

so for example pet store, dropdown for which type of pets the store has then on next page a grid is populated with all pets of that type which the store has available today.

already got the date filter sorted since that's applied to the databind.

database is connection via an edmx, it has 2 table with no relationships but there is say a category/pet table which goes into details for each category/pet and then there is a record table which has a category/pet column of which the 2 tables have that single field incommon.

I have been trying to get this to work by using ViewData which works perfectly fine for the drop down <%: Html.DropDownList("category", (SelectList)ViewData["CategoryList"])%> but fails to populate the grid on the next page.

so something sorta like http://demos.telerik.com/aspnet-mvc/grid/selectionserverside but if possible with a dropdown and across 2 pages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

遮云壑 2024-10-28 21:13:40

我遇到了同样的事情,所以我只是使用 jQuery 来获取每个页面的数据(将 #category 替换为类别下拉列表的 ID):

    function onDataBinding(e) {
        showWaitDialog();
        var grid = $('#Grid').data('tGrid');

        var args = 'page=' + e.page + '&category' + $('#category').val();

        $.ajax({
            url: "/Search/AjaxBinding/",
            type: "POST",
            data: args,
            dataType: "json",
            success: function (data) {
                grid.total = data.total;
                grid.dataBind(data.data);
                hideWaitDialog();
            }
        });

    }

将其添加到您的网格代码中:

.ClientEvents(x => x.OnDataBinding("onDataBinding"))

控制器代码:

    [GridAction(EnableCustomBinding = true)]
    public ActionResult AjaxBinding(int page, int category)
    {

        var searchResultsViewModel = //Code to get search results

        return View(new GridModel
                        {
                            Data = searchResultsViewModel.SearchResults,
                            Total = searchResultsViewModel.TotalCount
                        });
    }

将类别传回,并过滤将结果放入网格之前。

I ran into the same thing, so I just use jQuery to get the data for each page (Replace #category with the ID of your category dropdown):

    function onDataBinding(e) {
        showWaitDialog();
        var grid = $('#Grid').data('tGrid');

        var args = 'page=' + e.page + '&category' + $('#category').val();

        $.ajax({
            url: "/Search/AjaxBinding/",
            type: "POST",
            data: args,
            dataType: "json",
            success: function (data) {
                grid.total = data.total;
                grid.dataBind(data.data);
                hideWaitDialog();
            }
        });

    }

Add this to your Grid code:

.ClientEvents(x => x.OnDataBinding("onDataBinding"))

Controller code:

    [GridAction(EnableCustomBinding = true)]
    public ActionResult AjaxBinding(int page, int category)
    {

        var searchResultsViewModel = //Code to get search results

        return View(new GridModel
                        {
                            Data = searchResultsViewModel.SearchResults,
                            Total = searchResultsViewModel.TotalCount
                        });
    }

Pass the category back, and filter the results before putting it into the grid.

夜雨飘雪 2024-10-28 21:13:40

来自文档。

Telerik Grid for ASP.NET MVC 使用其内置的基于 Linq 的表达式引擎来执行网格操作 - 分页、排序和过滤。然而在某些情况下,开发人员可能希望绕过表达式引擎和页面,自己对网格数据进行排序或过滤。这称为“自定义绑定”。

链接如下:

http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-data-binding-custom-binding.html

根据例子;如果这是您加载页面的控制器:

[GridAction]
public ActionResult Index(GridCommand command)
{
    IEnumerable<Order> data = GetData(command);
    var dataContext = new NorthwindDataContext();
    //Required for pager configuration
    ViewData["total"] = dataContext.Orders.Count();

    return View(data);
}

那么您的回发将是您可以过滤此内容的地方:

[GridAction]
[HttpPost]
public ActionResult Index(GridCommand command)
{
    desiredCategory = this.myDropDownList.SelectedCategory.ToString();

    //Change the query here using post back variables
    IEnumerable<Order> data = GetData(command);
    data = from x in data.[entity name]
           where x.category = desiredCategory
           select x;

    var dataContext = new NorthwindDataContext();

    //Required for pager configuration
    ViewData["total"] = dataContext.Orders.Count();

    return View(data);
}

From the docs.

Telerik Grid for ASP.NET MVC is using its built-in Linq-based expression engine to perform the grid operations - paging, sorting and filtering. However in some cases the developer may want to bypass the expression engine and page, sort or filter the grid data by himself. This is called "Custom binding".

Heres the link:

http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-data-binding-custom-binding.html

as per the example; if this was your controller that loaded your page:

[GridAction]
public ActionResult Index(GridCommand command)
{
    IEnumerable<Order> data = GetData(command);
    var dataContext = new NorthwindDataContext();
    //Required for pager configuration
    ViewData["total"] = dataContext.Orders.Count();

    return View(data);
}

Then your post back would be a place you could filter this:

[GridAction]
[HttpPost]
public ActionResult Index(GridCommand command)
{
    desiredCategory = this.myDropDownList.SelectedCategory.ToString();

    //Change the query here using post back variables
    IEnumerable<Order> data = GetData(command);
    data = from x in data.[entity name]
           where x.category = desiredCategory
           select x;

    var dataContext = new NorthwindDataContext();

    //Required for pager configuration
    ViewData["total"] = dataContext.Orders.Count();

    return View(data);
}
溺孤伤于心 2024-10-28 21:13:40

我可以在没有任何代码的情况下给出一些指示(我的 MVC 有点生疏,而且我没有时间编写示例代码)。

所以基本上你有一个页面是下拉列表,然后是另一个页面是网格。
您需要的是:

  • 在网格页面上将第一页提交到网格页面(表单操作指向网格页面而不是其自身),
  • 查看是否从下拉列表中收到输入值,如果是,则根据该值过滤数据在绑定网格之前,
  • 在此页面上发出一个隐藏字段,您可以在其中存储从下拉列表接收的过滤器值,这样在分页网格时它就不会丢失,
  • 基本上检查下拉值或隐藏字段值以过滤网格

HTH

I can give a few directions without any code (my MVC is a bit rusty and I lack time to code samples).

So basically you have one page where the dropdown is and then another page where the grid will be.
What you need is:

  • have the first page submit to the grid page (form action pointing to grid page instead of itself)
  • on the grid page see if you receive the input value from the dropdown, if so then filter your data based on that value before binding the grid
  • emit a hidden field on this page where you store the filter value received from the dropdown so it is not lost when paging the grid
  • basically check for either dropdown value or hidden field value in order to filter the grid

HTH

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