#Datagrid does not update after changing items

1 messages · Page 1 of 1 (latest)

pseudo summit
#

Hey. I am new to Fluent UI and having some difficulty with datagrid.
I have a FluentDataGrid to show list of model. I want to filter the items according to a property. When i query and update the items and use updateDataAsync() method, datagrid does not update and at the end of the method browser gives error.
How am i suppose to update DataGrid when items list changes?

This is my datagrid:

<FluentDataGrid @ref="@MealGrid" TGridItem="Meal" Items="@FilteredMeals" ShowHover="true" ResizableColumns=true Pagination="@pagination" GridTemplateColumns="0.2fr 0.1fr 0.1fr 0.6fr 0.2fr 0.2fr" RowClass="@rowClass" RowStyle="@rowStyle" Style="height: auto;overflow:auto;">

this is the method triggered for filtering:

 private async Task FilterMeals()
  {
      IQueryable<Meal> tempMeals;
      if (!string.IsNullOrEmpty(searchValue))
          tempMeals = FilteredMeals.Where(x => x.Foods != null && x.Foods.Any(food => food.Name.ToLower().Contains(searchValue.ToLower())));
      else
          tempMeals = meals;

      if (tempMeals != meals && tempMeals!=null)
          FilteredMeals = tempMeals;

      searchValue = null;
      mealFilter = "All";
      await MealGrid.RefreshDataAsync();
         
  }

The page is working in interactiveserver mode.All other components works well.
i have debugged the method and the query is working well. FileteredMeals (which datagrid uses as items) updates according to filtering. But the grid does not update.

Thanks.

obtuse sonnet
#

You have to update the @FilteredMeals query set. But I see you are already doing that.. 🤔

pseudo summit
#

this is how i refactored

List<Meal> tempMeals=new();
if (!string.IsNullOrEmpty(searchValue))
{
    foreach (var meal in FilteredMeals){
        foreach (var food in meal.Foods)
        {
            if (food.Name.ToLower().Contains(searchValue.ToLower()))
            {
                tempMeals.Add(meal);
                break;
            }
        }
    }
    FilteredMeals = tempMeals.AsQueryable();

}
else
{
    FilteredMeals = meals;
}