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

[Solved] Set initial sort and group settings based on property name

3 Answers 99 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.
Josh Anderson
Top achievements
Rank 1
Josh Anderson asked on 17 Oct 2011, 10:16 PM
I'm trying to set the initial sorting of an MVC Grid using property names passed through the Model. Here's how my model looks:

public class RenderViewModel
{
    public RenderViewModel()
    {
        VisibleProperties = new List<string>();
        GroupByProperties = new List<string>();
        Orders = new List<RenderViewOrderByModel>();
    }
 
    public string Name { get; set; }
    public int Id { get; set; }
    public List<string> VisibleProperties { get; set; }
    public int PageSize { get; set; }
    public List<string> GroupByProperties { get; set; }
    public List<RenderViewOrderByModel> Orders { get; set; }
}
 
public class RenderViewOrderByModel
{
    public string PropertyName { get; set; }
    public bool Ascending { get; set; }
}

The problem is that I can't figure out how to get the grid initial sorting to work. Here's what I've tried:

<div class="body corners noTR noTL">
    @(Html.Telerik().Grid<ItemsViewModel>()
        .Name("ItemsGrid")
        .Columns(columns =>
        {
            columns.Bound(x => x.Id)
                .Visible(!Model.VisibleProperties.Any() || Model.VisibleProperties.Contains("Id"))
                .Title("Number")
                .Width(50)
                .HeaderHtmlAttributes(new { @style = "text-align: center;" })
                .HtmlAttributes(new { @style = "text-align: center;" });
            columns.Bound(x => x.Name)
                .Visible(!Model.VisibleProperties.Any() || Model.VisibleProperties.Contains("Name"))
                .Title("Name");
        })
        .Groupable(grouping =>
            grouping.Groups(groups =>
                Model.GroupByProperties.ForEach(x =>
                    groups.Add(c => typeof(ItemsViewModel).GetProperty(x)))))
                .DataBinding(db => db.Ajax().Select("RenderItems", "Views", new { id = Model.Id }))
        .Pageable(pager => pager.PageSize(Model.PageSize))
        .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn)
            .OrderBy(ordering =>
                         {
                             Model.Orders.Where(x => x.Ascending).ToList().ForEach(x => ordering.Add(c => c.GetType().GetProperty(x.PropertyName)).Ascending());
                             Model.Orders.Where(x => !x.Ascending).ToList().ForEach(x => ordering.Add(c => c.GetType().GetProperty(x.PropertyName)).Descending());
                         }))
        .ClientEvents(events => events.OnDataBound("grid_onDataBound"))
    )
</div>

You can see I've tried two methods of reflection to get the property instance. In the grouping, I tried retrieving the property by name by a typeof(ItemsViewModel) call. In sorting, I tried using the class instance passed in the lambda expression (c.GetType()). Neither work.

Sorting/grouping after that initial rendering work, so it's not a problem with that core functionality.

Is there any way to set the initial sorting and grouping by using the property name only?

3 Answers, 1 is accepted

Sort by
0
Josh Anderson
Top achievements
Rank 1
answered on 17 Oct 2011, 10:35 PM
Looks like in the case of sorting I paid too much attention to the examples and didn't simply look at the method definition. You can add sorting and grouping by string as it is.

.Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn)
    .OrderBy(ordering => //ordering.Add(c => c.Id)))
                 {
                     Model.Orders.Where(x => x.Ascending).ToList().ForEach(x => ordering.Add(x.PropertyName));
                     Model.Orders.Where(x => !x.Ascending).ToList().ForEach(x => ordering.Add(x.PropertyName).Descending());
                 }))

However, the grouping doesn't work the same way. Any guidance on how to accomplish this for grouping?
0
Josh Anderson
Top achievements
Rank 1
answered on 18 Oct 2011, 12:58 AM
I'm solving my own problems here...

After looking through the source I added the following method to the GridGroupFactory class definition:

public GridGroupDescriptorBuilder<TModel> Add(string memberName, ListSortDirection sortDirection)
{
    var descriptor = new GroupDescriptor();
    descriptor.Member = memberName;
    descriptor.SortDirection = sortDirection;
    descriptor.MemberType = typeof (TModel).GetProperty(memberName).GetType();
     
    settings.Groups.Add(descriptor);
 
    return new GridGroupDescriptorBuilder<TModel>(descriptor);
}

Once I compiled and replaced the Telerik.Web.Mvc.dll in my project, I was able to load initial group settings in this way:

@(Html.Telerik().Grid<ViewModel>()
    .Name("MyGrid")
    .Groupable(grouping =>
        grouping.Groups(groups => Model.GroupByProperties
                                 .ForEach(x => groups.Add(x.PropertyName, x.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending))))
    .DataBinding(db => db.Ajax().Select("RenderGrid", "Views", new { id = Model.Id }))
    .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn)
        .OrderBy(ordering => //ordering.Add(c => c.Id)))
                     {
                         Model.Orders.Where(x => x.Ascending).ToList().ForEach(x => ordering.Add(x.PropertyName));
                         Model.Orders.Where(x => !x.Ascending).ToList().ForEach(x => ordering.Add(x.PropertyName).Descending());
                     }))
)

It would be nice for this to make it into a future version of the controls, but in the meantime if anyone else can benefit from this here you go.
0
Rosen
Telerik team
answered on 18 Oct 2011, 10:05 AM
Hello Josh Anderson,

Thanks for your suggestion. We have added similar overloads to the group descriptor factory, which will be available with the next release of the component.

Regards,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Josh Anderson
Top achievements
Rank 1
Answers by
Josh Anderson
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or