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

[Solved] Need To Set Initial Sort

4 Answers 170 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.
Kevin Watkins
Top achievements
Rank 1
Kevin Watkins asked on 11 Jan 2010, 04:03 PM
Hi,

I'm using the grid with AJAX and sorting enabled. However I cannot find a way to specify the initial sort. When my user arrives on the page I want the grid to be sorted by the name column. Currently they have to click on the Name column heading to sort by name. I've added a default sort in the server code (i.e. if no sort is specified it sorts by name) but that's not great usability wise; both no sort and sort by name are then the same. Ideally you would be able to specify this when you define the columns, e.g.:

 

 

<% = Html.Telerik().Grid<OfficialSummary>()   
    .Name("Grid")   
    .Columns(columns =>  
    {  
        columns.Add(o => o.Name).Sorted(ListSortDirection.Ascending);  
 

 

Although not sure how easy that will be as I don't believe the GridColumn class has a reference back to the parent Grid?

On a related note it would also be nice to be able to specify that a grid should always have a sort, i.e. if you're sorted by name descending and click on the column header it changes to sorted by name ascending rather than no sort.

And on a further related note the Component property of ViewComponentBuilderBase is protected internal. Perhaps make this public? That way people could write extension methods to do extra stuff with the components, e.g. I could add something like:

 

 

public static GridBuilder<T> Sort<T>(this GridBuilder<T> gridBuilder, string member, ListSortDirection sortDirection)   
    where T : class   
{  
   gridBuilder.Component.DataProcessor.SortDescriptors.Add(new SortDescriptor { Member = member, SortDirection = sortDirection });   
   return gridBuilder;   
}  
 

 


(Not suggesting you add this method of course, merely using it as an example of how you might want to add extra builder methods)

Thanks,

Kev

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 12 Jan 2010, 09:38 AM
Hi Kevin Watkins,

Having the ability to sort the columns beforehand has been requested before and we have it logged as a feature suggestion. We will consider increasing its priority. As you suggested in order to implemented we need the other suggested feature - clicking the header to sort ascending or descending only without making the column unsorted.

Indeed the Component property is not public however you can use the ToComponent method. Unfortunately the latter was marked with EditorBrowsable(Never) which hid him from intellisense. I have fixed that.

As a workaround you can use the following approach:
<% Grid<Order> grid = Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Add(o => o.OrderID).Width(120);
            columns.Add(o => o.ShipAddress);
        })
        .Pageable()
        .Sortable();
     
        var descriptor = grid.DataProcessor.SortDescriptors.Where(s => s.Member == "OrderID").FirstOrDefault();
        if (descriptor == null)
        {
            grid.DataProcessor.SortDescriptors.Add(new Telerik.Web.Mvc.SortDescriptor
            {
                Member = "OrderID",
                SortDirection = System.ComponentModel.ListSortDirection.Descending
            });
        }
 
        grid.Render();
%>

And you need to modify the NextSortDirection method in GridRenderer.cs to this:
private static ListSortDirection? NextSortDirection(ListSortDirection? direction)
{
    if (direction == ListSortDirection.Ascending)
    {
        return ListSortDirection.Descending;
    }
 
    return ListSortDirection.Ascending;
}

I hope this helps,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Kevin Watkins
Top achievements
Rank 1
answered on 12 Jan 2010, 12:19 PM
Hi,

Don't think you need to make a change to stop the column being unsorted to implement an initials sort; I think the two are quite separate. I've decided not to care about the unsorted for now, but presumably I'd need a change to the JavaScript as well as the static method you describe?

I've taken the code for the initial sort and wrapped it up into some extension methods; you or anyone reading this might find them useful:

public static GridBuilder<T> SortBy<T, TProperty>(this GridBuilder<T> gridBuilder, Expression<Func<T, TProperty>> propertyExpression)  
    where T : class 
{  
    return gridBuilder.SortBy(propertyExpression, ListSortDirection.Ascending);  
}  
 
public static GridBuilder<T> SortBy<T, TProperty>(this GridBuilder<T> gridBuilder, Expression<Func<T, TProperty>> propertyExpression, ListSortDirection direction)  
    where T : class 
{  
    return gridBuilder.SortBy(GetPropertyName(propertyExpression), direction);   
}  
 
private static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> propertyExpression)  
{  
    Validate.Argument(propertyExpression, "propertyExpression").IsNotNull();  
 
    MemberExpression memberExpression = propertyExpression.Body as MemberExpression;  
    if ((memberExpression == null) || (memberExpression.Member.MemberType != MemberTypes.Property))  
    {  
        throw new ArgumentException("Value is not a property access expression.""propertyExpression");  
    }  
 
    return memberExpression.Member.Name;  
}  
 
public static GridBuilder<T> SortBy<T>(this GridBuilder<T> gridBuilder, string member)  
    where T : class 
{  
    return gridBuilder.SortBy(member, ListSortDirection.Ascending);  
}  
 
public static GridBuilder<T> SortBy<T>(this GridBuilder<T> gridBuilder, string member, ListSortDirection direction)  
    where T : class 
{  
    Validate.Argument(gridBuilder, "gridBuilder").IsNotNull();  
    Validate.Argument(member, "member").IsNotNull().IsNotEmpty();  
 
    // Use the implicit cast to get a reference to the grid.  
    Grid<T> grid = gridBuilder;  
 
    var sortDescriptor = grid.DataProcessor.SortDescriptors.Where(s => s.Member == member).FirstOrDefault();  
    if (sortDescriptor == null)  
    {  
        grid.DataProcessor.SortDescriptors.Add(new SortDescriptor  
        {  
            Member = member,  
            SortDirection = direction  
        });  
    }  
 
    return gridBuilder;  

However a change needs to be made to the grid as well. The sort information isn't written out to the JavaScript call to tGrid, so although the column is initially rendered correctly it behaves as if it's unsorted, i.e. you still have to click on the column to sort. Add the following code to Grid<T>.WriteInitializationScript just before the "columns.Add(column);" line:

if (Sorting.Enabled)  
{  
    var sortDescriptor = DataProcessor.SortDescriptors.Where(s => s.Member == c.Name).FirstOrDefault();  
    if (sortDescriptor != null)  
    {  
        if (sortDescriptor.SortDirection == ListSortDirection.Ascending)  
        {  
            column["sortDirection"] = "asc";  
        }  
        else 
        {  
            column["sortDirection"] = "desc";   
        }  
    }  

Note that none of the above has any validation that the sorts are valid and compatible with the grid. (E.g. you could set multiple sorts using the code above for a single sort grid) And yes I know I could use the ternary operator in the above snippet, I just don't like it. 8o)

Cheers,

Kev
0
Jeffery Svendson
Top achievements
Rank 1
answered on 10 Mar 2010, 09:46 PM
> Having the ability to sort the columns beforehand has been requested before and we have it logged as a feature suggestion
One would think sorting columns - for a grid - would be baseline/fundamental functionality.
Furthermore, *keeping* the sort order after leaving the grid and then coming back to it would be baseline, too.
We may have to toss Telerik Grid<T> because of this very limitation.
0
Atanas Korchev
Telerik team
answered on 11 Mar 2010, 08:57 AM
Hi Jeffery Svendson,

Please check my response to your other reply. Initial sorting is supported since the Q1 2010 release (2010.3.309). You can check the online demo here.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Kevin Watkins
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Kevin Watkins
Top achievements
Rank 1
Jeffery Svendson
Top achievements
Rank 1
Share this question
or