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

Sort bound field on grid by different property

4 Answers 678 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 01 Jul 2015, 11:35 AM

Hi there,

 I am trying to get an MVC Kendo UI Grid on the sorting event of a particular column to sort by a different property in the bound model class.  Is this possible - I've tried defining the Sortable comparer (http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.sortable.compare) but can't seem to get the syntax working.  Below is my cshtml view code - for the SampleResultsOverview column I need the sort value to be bound to a different property on the site model instead - the property is called SampleResultsOverviewSortOrder.

Many thanks, Mark.

 

 .Columns(columns =>
    {
        columns.Bound(site => site.UPRN).Title("UPRN");
        columns.Bound(site => site.AddressWithPostcode).Title("Address");
        columns.Bound(site => site.Contact).Title("Contact");
        columns.Bound(site => site.Telephone).Title("Telephone");
        columns.Bound(site => site.SampleResultsOverview).Title(@RecActionHeader).Encoded(false).Sortable(
            compare: function (a, b) {
            return numbers[a.name] - numbers[b.name];
        );
        columns.Template(site => { }).ClientTemplate(" ").Title("Site Documents");
    })

4 Answers, 1 is accepted

Sort by
0
Accepted
Dimiter Madjarov
Telerik team
answered on 03 Jul 2015, 06:51 AM

Hello Mark,

Specifying a sortable compare function is not supported in the MVC Grid. In the current case I would suggest to bind the column to the field that it should be sorted by and display what is needed via template.
E.g.

columns.Bound(p => p.ProductID).ClientTemplate("#=ProductName#");

Regards,
Dimiter Madjarov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 03 Jul 2015, 11:52 AM
Thanks, worked perfectly :)
0
Daniel
Top achievements
Rank 1
answered on 30 Jan 2019, 01:44 AM

Hello Dimiter,

The display of the column using the Client Template works however when grouping by this column (dragging it to the grouping section) or exporting to Excel, it uses the ID. How can I display the client template instead of the ID when grouping by that column or exporting to Excel?

Dan

0
Alex Hajigeorgieva
Telerik team
answered on 31 Jan 2019, 03:26 PM
Hi, Daniel,

To group by the field which is being shown to the user, you may intercept the Kendo UI Grid group event and change the group field:

https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/group

.Events(e=>e.Group("onGroup"))
 <script>
     function onGroup(e) {
         var groupsLength = e.groups.length;
         if (groupsLength) {
             if (e.groups[groupsLength - 1].field === "ProductID") {
                 e.groups[groupsLength - 1].field = "ProductName";
             }
         }
     }
 </script>

As far as the excel export is concerned, once the grouping is intercepted and altered, the Excel will also export the current data source view.

Let me know in case further questions arise.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Mark
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Share this question
or