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
|
@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);
}
}
} |
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.
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