Mud Blazor 数据网格过滤器用于日期时间列

问题描述 投票:0回答:1

当日期选择器的工具栏在日期时间列过滤器的数据网格中显示为简单过滤模式时,有没有办法关闭它的工具栏?如何使其也可编辑?基本上是想设置datepicker的属性。

数据网格的过滤模式设置为简单。当数据网格有模板列时,过滤器不会显示。如何使过滤器适用于模板列? 谢谢

blazor blazor-server-side mudblazor
1个回答
0
投票

可以将

<CellTemplate>
放置在
PropertyColumn
中而不是
TemplateColumn
中,以使过滤器可用。根据您的需求,内置过滤器日期时间无法更改为“可编辑”文本格式。但您可以自定义过滤器以满足您的要求。 (请注意,自定义过滤器必须使用 DataGridFilterMode.ColumnFilterMenu,这与简单模式类似。)如下所示:

<MudDataGrid Items="@employees" Filterable="true" SortMode="@SortMode.None" Groupable="false" FilterMode="DataGridFilterMode.ColumnFilterMenu">
    <Columns>
        <PropertyColumn Property="x => x.Name" />
        <PropertyColumn Property="x => x.date" >
            <FilterTemplate>
                <MudTextField @bind-Value="filterDate" Placeholder="Enter date" />
                <MudButton Color="@Color.Primary" OnClick="@(() => ClearFilterAsync(context))">Clear</MudButton>
                <MudButton Color="@Color.Primary" OnClick="@(() => ApplyFilterAsync(context))">Filter</MudButton>
            </FilterTemplate>
            <CellTemplate>
                @context.Item.date
            </CellTemplate>
        </PropertyColumn>
    </Columns>
</MudDataGrid>

@code {  
    public record Employee(string Name, DateTime date);
    public IEnumerable<Employee> employees;

    string filterDate = string.Empty;
    FilterDefinition<Employee> _filterDefinition;

    protected override void OnInitialized()
    {
        _filterDefinition = new FilterDefinition<Employee>
        {
            //The filtering logic
            FilterFunction = x =>
            {
                return x.date.ToString().Contains(filterDate);
            }
        };

        employees = new List<Employee>
        {
            new Employee("Sam" ,new DateTime(2024,11,2)),
            new Employee("Alicia",new DateTime(2024,11,3)),
            new Employee("Ira",new DateTime(2024,12,3)),
            new Employee("John",new DateTime(2024,12,4)),
        };
    }

    private async Task ApplyFilterAsync(FilterContext<Employee> context)
    {
        await context.Actions.ApplyFilterAsync(_filterDefinition);
    }

    private async Task ClearFilterAsync(FilterContext<Employee> context)
    {
        await context.Actions.ClearFilterAsync(_filterDefinition);
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.