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

Selective Custom Binding

2 Answers 73 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 Meleski
Top achievements
Rank 1
Matt Meleski asked on 16 Mar 2012, 09:36 PM
Hi,

 I have implemented custom binding  as described in this example:

http://demos.telerik.com/aspnet-mvc/razor/grid/custombinding

It works for me, but I have one question:

I have implemented Custom code for paging.

I would also like to Sort, but do not want to write any custom code to do this.
i.e. I would like to take advantage of the built in sorting for this.

Therefore in my method decorated with the below attribute:

 [GridAction(EnableCustomBinding = true)]
 
public ActionResult _CustomBinding(GridCommand command)
Can I do something to invoke the built in/default sorting
Or:
Can I do something in my view to indicate I want to do the default sorting?

Thanks.



2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 21 Mar 2012, 01:19 PM
Hello Matt,

I am afraid that there is no way to just implement one of the binding responsibilities. You should either implement all of them or you should disable custom binding.

All the best,
Petur Subev
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.
0
Dadv
Top achievements
Rank 1
answered on 21 Mar 2012, 03:50 PM
Hi,

You could use generic sorter to simplify the job :

    public static class GenericSorter<T>
    {
        public static IQueryable<T> Sort(IQueryable<T> query, IEnumerable<SortDescriptor> sorters)
        {
            int i = 0;
            foreach (var sort in sorters)
            {
                if (i++ == 0)
                    query = GenericSorter<T>.Sort(query, sort.Member, (int)sort.SortDirection);
                else
                    query = GenericSorter<T>.ThenSortBy(query as IOrderedQueryable<T>, sort.Member, (int)sort.SortDirection);
            }
 
            return query;
        }
 
        public static IQueryable<T> Sort(IQueryable<T> query, string sortMember, int sortDirection)
        {
            Type tp = typeof(T).GetProperty(sortMember).PropertyType;
 
 
            Type sorterType = typeof(GenericSorterDirection<,>).MakeGenericType(typeof(T), tp);
 
            var sorterObject = Activator.CreateInstance(sorterType);
 
            string direction = "Ascending";
 
            if (sortDirection != 0)
            {
                direction = "Descending";
            }
 
            return (IQueryable<T>)sorterType.GetMethod(direction, new Type[] { typeof(List<T>), typeof(string) })
            .Invoke(sorterObject, new object[] { query, sortMember });
        }
 
        public static IQueryable<T> Sort(IEnumerable<T> query, string sortMember, int sortDirection)
        {
            Type tp = typeof(T).GetProperty(sortMember).PropertyType;
 
 
            Type sorterType = typeof(GenericSorterDirection<,>).MakeGenericType(typeof(T), tp);
 
            var sorterObject = Activator.CreateInstance(sorterType);
 
            string direction = "Ascending";
 
            if (sortDirection != 0)
            {
                direction = "Descending";
            }
 
            return (IQueryable<T>)sorterType.GetMethod(direction, new Type[] { typeof(List<T>), typeof(string) })
            .Invoke(sorterObject, new object[] { query, sortMember });
        }
 
 
        public static IOrderedQueryable<T> ThenSortBy(IOrderedQueryable<T> query, string sortMember, int sortDirection)
        {
            Type tp = typeof(T).GetProperty(sortMember).PropertyType;
 
 
            Type sorterType = typeof(GenericSorterDirection<,>).MakeGenericType(typeof(T), tp);
 
            var sorterObject = Activator.CreateInstance(sorterType);
 
            string direction = "Ascending";
 
            if (sortDirection != 0)
            {
                direction = "Descending";
            }
 
            var temp = sorterType.GetMethod("ThenBy" + direction, new Type[] { typeof(IOrderedQueryable<T>), typeof(string) });
            var temp2 = temp.Invoke(sorterObject, new object[] { query, sortMember });
 
            return (IOrderedQueryable<T>)temp2;
        }
    }
 
    internal sealed class GenericSorterDirection<T, PT>
    {
        public IEnumerable<T> Ascending(IEnumerable source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.OfType<T>().AsQueryable<T>().OrderBy<T, PT>(sortLambda);
        }
 
        public IEnumerable<T> Ascending(IEnumerable<T> source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.AsQueryable<T>().OrderBy<T, PT>(sortLambda);
        }
 
        public IEnumerable<T> Descending(IEnumerable source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.OfType<T>().AsQueryable<T>().OrderByDescending<T, PT>(sortLambda);
        }
 
        public IEnumerable<T> Descending(IEnumerable<T> source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.AsQueryable<T>().OrderByDescending<T, PT>(sortLambda);
        }
 
        public IEnumerable<T> ThenByDescending(IOrderedQueryable<T> source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.ThenByDescending<T, PT>(sortLambda);
        }
 
        public IEnumerable<T> ThenByAscending(IOrderedQueryable<T> source, string sortMember)
        {
            var param = Expression.Parameter(typeof(T), "item");
 
            var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortMember), typeof(PT)), param);
 
            return source.ThenBy<T, PT>(sortLambda);
        }
    }
}
Tags
Grid
Asked by
Matt Meleski
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Dadv
Top achievements
Rank 1
Share this question
or