Default Sorting With Remote DataSource

Hello,
In my project I'm using a Grid with a remote datasource:
 @(Html.EJS().Grid("DataGrid")
.DataSource(dataManger =>{
dataManger.Url("/API/GetData").Adaptor("UrlAdaptor");
})
.Columns(col =>
{
col.Field("Column1").HeaderText("C1").Add();
col.Field("Column2").HeaderText("C2").Add();
})
.Render()

No matter if I set 
.SortSettings(sort => sort.Columns(new { field = "Data", direction = "Ascending" }))
The API always gets called with a sorting option for "Column1" Ascending, at start.

The only workaround I've found is to add the event:
.Created("created")
and the sort the column manually in JS:
function created() {
var grid = document.getElementById("DataGrid").ej2_instances[0];
grid.sortColumn("Column2", "Descending", false);
}

Is this the only way to achieve this?
Thank you for your support

Daniele

3 Replies

PS Pavithra Subramaniyam Syncfusion Team March 28, 2018 12:59 PM UTC

Hi Daniele, 

We have checked your query and we could not reproduce the issue at our end. We have prepared a simple sample based on your code example in which we have set the initial sort in the Grid. Please refer to the below sample link. 


Could you please share the following details that will be helpful for us to provide a better solution as early as possible. 

  1. Share your sample code
  2. Share the stack trace if any script error has been thrown
  3. Please reproduce the issue in the above sample, if possible
  4. Share your controller code
 
Regards, 
Pavithra S. 



DA Daniele March 28, 2018 10:31 PM UTC

Hello,
Thank you for your reply. After a bit of extra investigation, I've found that the extra sort at start, was caused by a grouped column, so that this code:
.GroupSettings(group => group.Columns(new string[] { "Column1" }))
causes a request with a initial sort column for Column1 ascending

Also I was adding a SortSetting by doing this:
.SortSettings(sort => sort.Columns(new { field = "Data", direction = "Ascending" }))
but looks like it doesn't works as I need to define a List<object> to pass in. 

Thanks


PS Pavithra Subramaniyam Syncfusion Team March 29, 2018 11:22 AM UTC

Hi Daniele, 

Query #1: “After a bit of extra investigation, I've found that the extra sort at start, was caused by a grouped column” 
Yes, your suspect is correct. By default when grouping, the column will be sorted in the Ascending order even if we disabled the sorting feature and this is the default behavior. 

Query #2: 
We have checked your snippets given in the previous update, you have tried to apply initial sorting for the `Data` column, but this column is not available in the column settings, so that initial sorting not worked this. We suggest to you to set initial sorting for the specified columns like the following code snippets.  

[index.chtml] 
@Html.EJS().Grid("check").DataSource(dataManger =>{dataManger.Url("https://js.syncfusion.com/demos/ejServices/Wcf/Northwind.svc/Products").CrossDomain(true).Adaptor("ODataAdaptor"); 
}).Columns(col => 
{ 
 
    col.Field("ProductID").HeaderText("Product ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("ProductName").HeaderText("Product Name").Width("150").Add(); 
    .   .    .                                               //intial group which will sort the column by default 
}).AllowPaging().AllowGrouping(). 
 
GroupSettings(group => group.Columns(new string[] { "ProductID" })).AllowSorting() 
// initial Sorting of Column ‘ProductName’ 
.SortSettings(sort => sort.Columns(new { field = "ProductName", direction = "Ascending" })).Render() 


You can also sort the column at initial render by using the sortColumn() method as you have mentioned in JS. 

                               https://ej2.syncfusion.com/16.1.24/documentation/grid/api-grid.html?lang=typescript#sortcolumn  


Regards, 
Pavithra S. 


Loader.
Up arrow icon