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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Grid Custom Sort

Thread ID:

Created:

Updated:

Platform:

Replies:

125943 Sep 13,2016 02:24 PM UTC Sep 15,2016 09:07 AM UTC ASP.NET MVC 3
loading
Tags: Grid
Chris Smith
Asked On September 13, 2016 02:24 PM UTC

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

Ragavee U S [Syncfusion]
Replied On 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. 


Chris Smith
Replied On September 14, 2016 08:39 AM UTC

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


Ragavee U S [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;