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

How do I customize sorting on a specific column ?

2 Answers 18 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carrie
Top achievements
Rank 1
Carrie asked on 05 Sep 2013, 05:10 PM
Hello,

I have the need to sort a column of strings using a subset of the string parsed into an integer.    For example:

AA1
AA2
AA3
AB4
CC5

I need the sorting to strip out all alpha's and sort by the integer.   Please advise how I can do this using the following implementation:
@(Html.Kendo().Grid(Model)   
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Id).Width(60);
        columns.Bound(p => p.Title).Width(250); 
    })
    .Sortable()
    .DataSource(dataSource => dataSource
               .Ajax()
               .PageSize(10)
               .Read(read => read.Action("Get", "Grid", new {view = "MyActive", showAll = false}))
               )
      )

2 Answers, 1 is accepted

Sort by
0
Carrie
Top achievements
Rank 1
answered on 05 Sep 2013, 05:45 PM
I have accomplished what I need by checking the DataSourceRequest.SortDescriptors in the controller and modifying any Sort on the "Id" column to use a hidden "NumericId" column.   I am not sure this is the best workaround please let me know if there is a cleaner/easier way.
public JsonResult Get([DataSourceRequest] DataSourceRequest request, string view, bool showAll)
       {
           foreach (var sortItem in request.Sorts)
           {
               if (sortItem.Member == "Id")
                   sortItem.Member = "NumericId";
           }
 
          var items = GetData();
 
          return Json(items.ToDataSourceResult(request));
       }
Thanks,
Carrie
0
Daniel
Telerik team
answered on 09 Sep 2013, 10:50 AM
Hello Carrie,

If the column should display the Id field but the operations should be performed on the "NumericId" property then an alternative solution would be to bind the column to the "NumericId" property and use a template to show the "Id" value:

columns.Bound(p => p.NumericId).ClientTemplate("#:Id#").Width(60);
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Carrie
Top achievements
Rank 1
Answers by
Carrie
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or