c# - MudBlazor: MudTable filtering not working - Stack Overflow

Project created through VS2022's "Blazor Web App" template and I've installed MudBl

Project created through VS2022's "Blazor Web App" template and I've installed MudBlazor.

I've copied this filterable Table example from the MudBlazor docs almost exactly:

Still can't get the filter event to trigger on key presses in the text field, event is only raised on page refresh. Starting to suspect it could be related to the render mode? The pagination isn't working either.

@page "/"
@rendermode InteractiveServer
@inject StockService StockService
@using StockSim.API.Models
@using StockSim.API.Services

<MudTable Items="@Stocks" Dense="true" Hover="true" Striped="true" Filter="new Func<Stock,bool>(FilterFunc1)">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Stocks</MudText>
        <MudSpacer />
        <MudTextField @bind-Value="searchString1" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
    </ToolBarContent>
    <HeaderContent>
        <MudTh>Symbol</MudTh>
        <MudTh>Name</MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd DataLabel="Symbol">@context.Symbol</MudTd>
        <MudTd DataLabel="Name">@context.Name</MudTd>
    </RowTemplate>
    <PagerContent>
        <MudTablePager />
    </PagerContent>
</MudTable>


@code {
    private string searchString1 = "";
    private IEnumerable<Stock> Stocks = new List<Stock>();

    protected override async Task OnInitializedAsync()
    {
        Stocks = await StockService.GetStocksAsync();
    }

    private bool FilterFunc1(Stock Stock) => FilterFunc(Stock, searchString1);

    private bool FilterFunc(Stock Stock, string searchString)
    {
        if (string.IsNullOrWhiteSpace(searchString))
            return true;
        if (Stock.Symbol.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (Stock.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        return false;
    }
}

Project created through VS2022's "Blazor Web App" template and I've installed MudBlazor.

I've copied this filterable Table example from the MudBlazor docs almost exactly: https://mudblazor/components/table#table-with-pagination-and-filtering

Still can't get the filter event to trigger on key presses in the text field, event is only raised on page refresh. Starting to suspect it could be related to the render mode? The pagination isn't working either.

@page "/"
@rendermode InteractiveServer
@inject StockService StockService
@using StockSim.API.Models
@using StockSim.API.Services

<MudTable Items="@Stocks" Dense="true" Hover="true" Striped="true" Filter="new Func<Stock,bool>(FilterFunc1)">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Stocks</MudText>
        <MudSpacer />
        <MudTextField @bind-Value="searchString1" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
    </ToolBarContent>
    <HeaderContent>
        <MudTh>Symbol</MudTh>
        <MudTh>Name</MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd DataLabel="Symbol">@context.Symbol</MudTd>
        <MudTd DataLabel="Name">@context.Name</MudTd>
    </RowTemplate>
    <PagerContent>
        <MudTablePager />
    </PagerContent>
</MudTable>


@code {
    private string searchString1 = "";
    private IEnumerable<Stock> Stocks = new List<Stock>();

    protected override async Task OnInitializedAsync()
    {
        Stocks = await StockService.GetStocksAsync();
    }

    private bool FilterFunc1(Stock Stock) => FilterFunc(Stock, searchString1);

    private bool FilterFunc(Stock Stock, string searchString)
    {
        if (string.IsNullOrWhiteSpace(searchString))
            return true;
        if (Stock.Symbol.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (Stock.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        return false;
    }
}

Share Improve this question edited Mar 4 at 1:40 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Feb 23 at 11:55 SoroushAUSoroushAU 132 bronze badges 2
  • I've answered but If that doesn't work then my follow up question is what options did you choose when you used the MudBlazor template to create the project. – RBee Commented Feb 23 at 17:42
  • @RBee Thanks, just reviewed my options and realized I had to adjust the Routes element in App.razor to <Routes @rendermode="InteractiveServer" />. Working now. – SoroushAU Commented Feb 24 at 4:16
Add a comment  | 

1 Answer 1

Reset to default 0

I reviewed the docs (https://learn.microsoft/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#apply-a-render-mode-to-the-entire-app) and apparently had to add @rendermode="InteractiveServer" to the Routes and HeadOutlet elements in App.razor. I'm not entirely sure why this fixed it since my .razor was already using @rendermode InteractiveServer locally. Perhaps the rendermode needed to be set globally like that to support MudBlazor's components too? Not sure.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745155400a4614089.html

相关推荐

  • c# - MudBlazor: MudTable filtering not working - Stack Overflow

    Project created through VS2022's "Blazor Web App" template and I've installed MudBl

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信