Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1845
Using datasourceurl and local sorting
posted

I would like to update my grid using datasourceurl, but allow local sorting that is maintained after the datasourceurl is updated.  Is this possible?

Here's a basic scenario of what I'd like to happen:

-Grid is initially loaded.

-User sorts grid by clicking on a column header.

-User selects a different option from some dropdown.

-Grid's data is updated, but the sorting of the column is retained.

Here's what I've tried so far:

client-side:

  var url = getNewUrlFromSomeDropDown();

  $myGrid.igGrid('option', 'dataSource', url);

...which causes that url to be hit, which does something like this (MVC / C#):

server:

Example 1 (doesn't work at all, grid is blank):

 public ActionResult _Employee_Grid(int someFilteringInt)
    {
      List<Employee> employees = GetEmployees(someFilteringInt);
 
      return Json(employees, JsonRequestBehavior.AllowGet);
    }

Example 2 (Populates grid with new data, but the data is not sorted in any particular way, and the sorting arrows are lost.)

public ActionResult _Employee_Grid(int someFilteringInt)
    {
      List<Employee> employees = GetEmployees(someFilteringInt);
      GridModel model = new GridModel();
      GridSorting sorting = new GridSorting();
      sorting.Mode = SortingMode.Single;
      model.Features.Add(sorting);
 
      model.DataSource = employees.AsQueryable<Employee>();
      return model.GetData();
    }

Also, in the 2nd example, I must add the sorting feature to the model, or else sorting is lost all together.  This means that the GetData() method generates different JSON data depending on the features and properties of the grid?  If so, this doesn't make sense IMO.  GetData should have nothing to do with the grid's setup.  It sort of defeats the purpose of only reloading the data instead of the entire grid.

Thanks in advance for any help!

Parents
No Data
Reply
  • 24671
    posted

    hi,

    if you'd like to just rebind the grid, you should do:

    $("#grid1").igGrid("dataBind");

    your code tries to change the data source url, after the grid has been created. i am not sure why you need to do this and i wouldn't advise to do this at runtime. a New data source may imply new columns schema, etc. there is no guarantee your grid will function correctly.

    regarding the second scenario, this is related to the fact that MVC is stateless. When you define your grid in the View, and do a subsequent remote request to a controller action, there is no info on the server about how your grid is configured, therefore GetData can only infer features and parameters from the URL. i mean that local sorting is not preserved when you do remote paging/filtering, etc. So if you have local sorting, and remote paging enabled, this is how the url could look like:

    http://localhost:14991/samplesbrowser/grid/PagingGetData?controller=GridLocalSorting?page=1&pageSize=25

    there is no sorting information encoded in the url. If it was set to remote, then the sorted columns would have been encoded in the URL, and GetData would apply sorting, apart from paging, on the server side.

    What you can do, as a workaround, is to manually call sortColumn from the sorting API, after the response comes . You can do this by handling the  dataRendered event. here is how you can sort a column programatically:

    $("#grid1").igGridSorting("sortColumn", "SomeColumnKey");

    Hope it helps. Thanks,

    Angel

Children