How to use Grid.GetFilteredRecordsAsync();

I'm trying to get a list or array of currently filtered rows in a grid with remote data. I presume that the method GetFilteredRecordsAsync() will do the trick.

var selected = await Grid.GetFilteredRecordsAsync();

But the return value is an "object" and not an array and not even a Task. How to iterate over the result

Into what to cast that return object? (I unsuccessfully tried to cast to array, List, Task, ArrayList).

The method documentation tells us something that looks like the documentation of the JavaScript Grid component: "Get all filtered records from the Grid and it returns array of objects for the local dataSource, returns a promise object if the Grid has remote data."

It would be good to have an example of how to programmatically manipulate filtering and getting filtered data.


Regards,

Slobodan


4 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team March 14, 2022 01:41 PM UTC

Hi Slobodan, 

Greetings from Syncfusion. 

Query: How to use Grid.GetFilteredRecordsAsync() for remote data - But the return value is an "object" and not an array and not even a Task. How to iterate over the result 

You want to iterate the filtered data for remote data(by getting the records from GetFilteredRecordsAsync method). You can achieve your requirement by using below way. Find the below code snippets and sample for your reference. 

@using Syncfusion.Blazor 
@using Syncfusion.Blazor.Data 
@using Syncfusion.Blazor.Grids 
@using Newtonsoft.Json 
 
<button @onclick="Click">GetFiltered</button> 
<SfGrid @ref="Grid" TValue="EmployeeData" ID="Grid" AllowPaging="true" AllowFiltering="true"> 
    <SfDataManager Url=https://services.odata.org/V4/Northwind/Northwind.svc/Orders/ Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager> 
    <GridColumns> 
        <GridColumn Field=@nameof(EmployeeData.OrderID) TextAlign="TextAlign.Center" HeaderText="Order ID" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.CustomerID) TextAlign="TextAlign.Center" HeaderText="Customer Name" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) TextAlign="TextAlign.Center" HeaderText="Employee ID" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<EmployeeData> Grid; 
    List<int> ids = new List<int>(); 
    public class EmployeeData 
    { 
        public int OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public int EmployeeID { get; set; } 
    } 
 
    public async Task Click() 
    { 
        var filtered = await Grid.GetFilteredRecordsAsync(); 
        List<EmployeeData> filteredList = JsonConvert.DeserializeObject<List<EmployeeData>>(JsonConvert.SerializeObject(filtered)); 
        int count = filteredList.Count(); 
        for (var i = 0; i < count; i++) 
        { 
            ids.Add(filteredList[i].OrderID); 
        } 
    } 
} 


Please let us know if you have any concerns. 

Regards, 
Rahul 



BU bugun March 14, 2022 02:21 PM UTC

I have a same problem. 

var test = await Grid.GetFilteredRecordsAsync();


Error: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[xxxxDto]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'result', line 1, position 10.



SM Slobodan Mumovikj replied to Rahul Narayanasamy March 14, 2022 08:10 PM UTC

Hi Rahul,

This example does not work for me. I have an Syncfusion autogenerated scaffolding (a Controller and a page). The controller does not use the " ODataV4Adaptor", but the "UrlAdaptor" which apparently marshals .net objects a bit differently. 

So by trial and error and some guess work I managed to work out a way to extract the list (it was via an IEnumerable):


public async Task ActionCompletedHandler(ActionEventArgs<MyDataModelClass> args)
{
var selected = (IEnumerable)await Grid.GetFilteredRecordsAsync();
var theList = new List<MyDataModelClass>();
foreach (var item in selected)
{
theList.Add((MyDataModelClass)item);
}
SelectedDailyReports = theList.ToArray();
}

I found a hint in the Controller class itself as the post method returned an object that was actually an IEnumerable collection


        // POST api/<controller>
        [HttpPost]
        [Route("api/[controller]")]
        public Object Post([FromBody] DataManagerRequest dm)
        {
            IEnumerable DataSource MyDataModelClass().ToList();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search); //Search
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.Cast<SmenskiIzveshtajStrumica>().Count();
            if (dm.Skip != 0)
            {
                DataSource = DataOperations.PerformSkip(DataSource, dm.Skip); //Paging
            }
            if (dm.Take != 0)
            {
                DataSource = DataOperations.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? new { result = DataSource, count = count } : DataSource as object;
        }

So thank you for your answer but it was not the answer I was looking for. Hopefully this will help others that are wrestling with this on their own. 


What would really help if there is more documentation and more examples about this specific topic: how to deserialize objects returned by all out of the box adaptors (so that we don't have to experiment to find it).


Regards,

Slobodan


Marked as answer

RN Rahul Narayanasamy Syncfusion Team March 15, 2022 01:22 PM UTC

Hi Slobodan, 

Thanks for the update. 

We are happy to hear that you have found solution for your requirement. We will improve our documentation in any of our upcoming release. Please get back to us if you need further assistance. 

Regards, 
Rahul 


Loader.
Up arrow icon