This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamic Columns (Show/Hide), Sorting, Paging, and Filtering

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Matt
Top achievements
Rank 1
Matt asked on 07 Jun 2011, 06:07 AM
I have an MVC 3 Razor solution where I have a View that provides the user with the option to choose what columns to view in a Grid.

When the user selects the columns (checkboxes tied back to an OptionsViewModel) they can update the Grid, which fires a jQuery Ajax call to update a panel with a Partial View that contains the Grid.

The Partial includes a model (ListViewModel) that has an IEnumerable<Items> and an instance of the OptionsViewModel within it. I set up the Grid, using the IEnumerable<Items> as the model and then bind columns using syntax like:
cols.Bound(c => c.Description).Visible(Model.Options.DisplayDescription).Column.Title = "Desc.";

This all works fine. The grid loads and displays the columns as per the user's display selections. Great.

I am now stuck when I want to add Sorting / Paging / Filtering as I believe I need to utilize a GridAction and use the GridModel. Doing this means that my partial won't like the model (expects a ListViewModel, not a GridModel<Items>), and I have no idea how to retain the user's selected columns as I can't pass the OptionsViewModel back in to the GridAction.

Any ideas on how to best get this scenario up and running? I am happy to change my approach all together as long as I can get dynamic/user defined columns, sorting, paging, and filtering all working together in harmony.

Thanks,
Matt

1 Answer, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 08 Jun 2011, 01:27 AM
So I think the trick is to pass objects external to the grid in the ViewBag.

I can then do this in the Grid View:
...
var o = (OptionsViewModel)ViewBag.Options;
...
cols.Bound(c => c.Description).Visible(o.DisplayDescription).Column.Title = "Description";
.DataBinding(dataBinding => dataBinding.Ajax().Select("Filter", "Site", o))
...


And in the Controller:
        [GridAction]
        public ActionResult Filter(OptionsViewModel options)
        {
            ViewBag.Options = options;
            return View(new GridModel<Item>
            {
                Data = repository.GetItems()
            });
        }

Is this a good approach?
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Share this question
or