How do I customize sorting on a specific column ?

3 posts, 0 answers
  1. Carrie
    Carrie avatar
    27 posts
    Member since:
    Aug 2013

    Posted 05 Sep 2013 Link to this post

    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. Carrie
    Carrie avatar
    27 posts
    Member since:
    Aug 2013

    Posted 05 Sep 2013 Link to this post

    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
  3. Daniel
    Admin
    Daniel avatar
    2226 posts

    Posted 09 Sep 2013 Link to this post

    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!
Back to Top