We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to do default sorting in MVC Grid

Hey guys,

we are using your MVC grid and after a lot of complications and try-and-error-handling (because in my opinion there is way to less API / Description from your side) I have handled the most problems.
Neither then less - your control looks beautiful and is very fast and responsive.

What I really miss here in ym MVC Grid is the option to have a default sorting - how to do that?

At the moment this is my sorting algorythm of my controller:
            if (String.IsNullOrEmpty(orderby))
                allDataForList = allDataForList.OrderByDescending(x => x.EndDate);
            else
            {
                bool desc = false;
                if (orderby.IndexOf("desc") > 0)
                {
                    desc = true;
                    orderby = orderby.Remove(orderby.IndexOf(" desc"));
                }
                allDataForList = LinqExtensions.OrderByField(allDataForList, orderby, desc);
            }
which means: If there is no sorting, sort on EndDate. But the problem here is, that there is no icon displayed on the front end for the sorting then and if I click on EndDate Coloum there will be the exact sorting I have done allready. Feels buggy.

I allready tried to do this by javascript with something like:
     $("#Grid").ejGrid("sortColumn", "ProjectID", "Descending");
But this seems complete buggy as hell - the sort works, but the sorting-icon will be displayed wrong (at first it will be displayed correct but on a click on this coloum there is a second sorting-icon which seems to be a obvious bug).

Do you have a solution for this?

3 Replies

AS Alan Sangeeth S Syncfusion Team July 27, 2015 01:09 PM UTC

Hi Rene,

Query 1: “Sorted Column at initial Grid render”

We are glad to let you know that we have achieved your requirement using “SortSettings” Grid property. Please refer the following code snippets.

@(Html.EJ().Grid<object>("FlatGrid")

        .
SortSettings(sort=>sort.SortedColumns(col=> col.Field("CustomerID").Direction(SortOrder.Descending).Add()))

)



For your convenience we have created a sample and the same can be downloaded from below location.

Sample: http://www.syncfusion.com/downloads/support/forum/119706/ze/EJGrid-859647982

Query 2: “two sort icons issue with sortColumn Grid method”

We have analyzed your code snippets and found that you have used “Descending” string with first character “D” capitalized which is the cause of the issue.

We suggest you to pass the direction value in small case as “descending” to resolve the issue. Please refer the following code snippets.

$("#Grid").ejGrid("sortColumn", "ProjectID", "descending");



Please refer the following API document for further reference.
http://helpjs.syncfusion.com/js/api/ejgrid#methods:sortcolumn

Please let us know if you need any further assistance.

Regards,
Alan Sangeeth S


RK Rene Koch July 27, 2015 01:10 PM UTC

Thank you very much


MS Madhu Sudhanan P Syncfusion Team July 28, 2015 11:52 AM UTC

Hi Rene,

Thanks for the update. Please get back to us if you require further assistance. We will be happy to help you out.

Regards,
Madhu Sudhanan. P

Loader.
Live Chat Icon For mobile
Up arrow icon