possibility to access filtered data in the grid from controller

Dec 18, 2013 at 4:07 PM
Dear All,
I would like to process the filtered data which currently display in the grid. Is there anyway to pass this information to the controller ?

Best wishes,
Kenny
Coordinator
Dec 18, 2013 at 4:26 PM
Hi,

You can override GetItemsToDisplay method in the Grid<T> class.
Please see - https://gridmvc.codeplex.com/wikipage?title=Custom%20grid%20properties%20initialization&referringTitle=Documentation
Dec 18, 2013 at 4:52 PM
Hi,
What if I want to get all filtered result not only the current displayed page? By the way, if I overied getitemstodisplay method, how can I tell the page to use my overloaded class to display the grid instead of using the default grid implementation?

Best wishes,
Kenny
Coordinator
Dec 18, 2013 at 5:19 PM
Hi,

Razor api works with Grid<T> same as IEnumerable<T>, you need only specify model like

@model MyGrid

Grid<T> has a property named GridItems that returns items after filters was applied. But there is one nuance: if you call this property before you configure grid columns - filters wil not applied.

Thus if you configure your columns in your custom Grid<T> class ctor, you can get items with GridItems property
    public class OrdersGrid : Grid<Order>
    {
        public OrdersGrid(IQueryable<Order> items)
            : base(items)
        {
                Columns.Add(....);
                Columns.Add(....);
                ....
        }
   }
        public ActionResult Index()
        {
                var grid = new OrdersGrid(repository.GetAll());
                var itemsAfterFiltering = grid.GridItems.ToList(); //database call
                return View(grid);
        }
Otherwise if you configure your columns on the page - columns will added after view engine interprets layout, and you can access to this items in the GetItemsToDisplay method.
        protected override IEnumerable<Order> GetItemsToDisplay()
        {
            if (this._itemsAfterFiltering != null)
            {
                this._itemsAfterFiltering = this.GridItems.ToList();
            }
            return base.GetItemsToDisplay();
        }
Dec 23, 2013 at 12:03 AM
Dear All,
I tried to overload the GetItemsToDisplay as follows


 protected override IEnumerable<Order> GetItemsToDisplay()
        {
            IEnumerable<Order> rc = base.GetItemsToDisplay();
            return rc;
        }
However, I just able to capture the first page's result. Did I miss something or GetitemstoDisplay will just show the visible result only? What if i want to get all filtered result instead of current page only?

Thanks,
Kenny
Coordinator
Dec 23, 2013 at 2:00 AM
Edited Dec 23, 2013 at 2:00 AM
Yes, you need to use GridItems property, as I said above:
        protected override IEnumerable<Order> GetItemsToDisplay()
        {
            if (this._itemsAfterFiltering != null)
            {
                this._itemsAfterFiltering = this.GridItems.ToList();
            }
            return base.GetItemsToDisplay();
        }