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
close icon

Grid Custom Sort

We are evaluating you ASP.net MVC controls and are wondering if there is a way to implement custom sorting. We have a grid bound to a datatable that has columns of type CellValue (Which is a custom class). Basically, we have a display value, and an actuall value for the cell so the cell might say,Red, Blue, Green etc but the value we want to use for sorting is the numeric value stored in the object. In our existing win forms app we just implement iComparable and the sorting works in this way. For the MVC grid you provide, it creates a column for each property of the cell value, e.g. Column.Value and Column.Display. These sort seperatly using numeric sorting on the value and alpa sorting on the string which is not what we are looking for. I am hoping someone might be able to point me in the right direction

3 Replies

RU Ragavee U S Syncfusion Team September 14, 2016 06:48 AM UTC

Hi Chris, 

Thanks for your interest in Syncfusion products. 

Before proceeding on your requirement, could you please share the following details for our reference? 

1.       Grid rendering code and code corresponding to column definition. 
2.       Share the data structure and first JSON array of the CellValue custom class. 
3.       Are you defining the CellValue column as foreign key column? 
4.       If possible, share an issue reproducible sample. 

Regards, 
Ragavee U S. 



CS Chris Smith September 14, 2016 08:39 AM UTC

  I have a sample project but it is too large to upload here Sample Project



RU Ragavee U S Syncfusion Team September 15, 2016 09:07 AM UTC

Hi Chris, 

Thanks for sharing the sample. 

From the shared sample, we could see that you have bound complex column to grid and would like to perform custom sorting on the column. We have achieved your requirement using the ActionBegin and ActionComplete event of the Grid. Please refer to the below code example. 

@(Html.EJ().Grid<object>("FlatGrid") 
                . . . . 
             .ClientSideEvents(eve=>eve.ActionBegin("actionBegin").ActionComplete("actionComplete")) 
) 
 
<script type="text/javascript"> 
    function actionBegin(args) { 
        if (args.requestType == "sorting" && args.columnName == "Grade.Display") { 
            var sortCol = args.model.sortSettings.sortedColumns; 
            sortCol[sortCol.length - 1]["field"] = "Grade.Value";//update the sortedColumn field to "Grade.Value" 
        } 
    } 
 
    function actionComplete(args) { 
        if (args.requestType == "sorting" && args.columnName == "Grade.Display") {             
 
            //code to update the sort icon in header 
            var index = this.getColumnIndexByField(args.columnName);//get the column index 
            //get the headerCellDiv element 
            var $headerCellDiv = this.getHeaderTable().find("thead tr:not('.e-stackedHeaderRow')").find(".e-headercell").not(".e-detailheadercell").eq(index).find(".e-headercelldiv"); 
            var direction = args.columnSortDirection;//get the sorting direction 
            var spantag = ej.buildTag('span.e-icon', "&nbsp;"); 
            $headerCellDiv.find(".e-ascending,.e-descending").remove();//remove the previously appended icons from the headercellDiv             
            imageDirection = direction != "descending" ? "e-rarrowup-2x" : "e-rarrowdown-2x"; 
            $headerCellDiv.append(spantag.addClass("e-" + (direction || "ascending") + " " + imageDirection));//append the sort icon 
        } 
    } 
</script> 

For your convenience, we have modified the sample with the above solution, which can be downloaded from the below location. 


Regards, 
Ragavee U S. 


Loader.
Live Chat Icon For mobile
Up arrow icon