Checkbox filter - Setting Default value at start

Is it possible to set the default value of the Filter at the start, whenever I try I get an error of: 

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
  at Syncfusion.Blazor.Grids.GridFilterColumn.OnInitializedAsync () <0x4a2e120 + 0x000e4> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x3bfc598 + 0x0013a> in <filename unknown>:0 

This is my code:

@page "/centrelist"
@layout DashLayout
@inject HttpClient httpClient
@inject IMatToaster matToaster
@attribute [Authorize(Policy = Policies.IsAdmin)]

<h3>Centre List</h3>
@if(records == null) {
    <LoadingSpinner SpinnerOption="ELoadingSpinner.Circle" />
} else {
    <SfGrid DataSource="@records" AllowPaging="true" AllowFiltering="true" AllowSorting="true" EnablePersistence="true">
        <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu">
            <GridFilterColumn Field="@nameof(CentreListDto.Status)" MatchCase="false" Operator="Operator.Equal" Predicate="and" Value="@defaultFilter"></GridFilterColumn>            
        </GridFilterSettings>       
        <GridPageSettings PageSize="15"></GridPageSettings>
        <GridColumns>
            <GridColumn Field="@nameof(CentreListDto.Id)" HeaderText="Unique ID" IsPrimaryKey="true" Visible="false"></GridColumn>
            <GridColumn Field="@nameof(CentreListDto.CentreNumber)" HeaderText="Centre Number" TextAlign="TextAlign.Right" Width="150" AllowFiltering="false"></GridColumn>
            <GridColumn Field="@nameof(CentreListDto.Name)" HeaderText="Name" AllowFiltering="false" Width="300"></GridColumn>
            <GridColumn Field="@nameof(CentreListDto.Telephone)" HeaderText="Phone Number" Width="200" AllowFiltering="false"></GridColumn>
            <GridColumn Field="@nameof(CentreListDto.Status)" HeaderText="Status" FilterSettings="@(new FilterSettings { Type = Syncfusion.Blazor.Grids.FilterType.CheckBox})" Width="200">                
            </GridColumn>
            <GridColumn Field="@nameof(CentreListDto.ActionAgainst)" HeaderText="Any Action" Width="1550" AllowFiltering="false">
                <Template>
                    @{
                        var centre = (context as CentreListDto);
                        if (centre.ActionAgainst) {
                            <div class="centrestatusdiv e-inactivecolor">
                                <span class="centrestatustext e-inactivecolor">Action Taken Against</span>
                            </div>
                        } else {
                            <div class="centrestatusdiv e-activecolor">
                                <span class="centrestatustext e-activecolor">No Action Against</span>
                            </div>
                        }
                    }
                </Template>
            </GridColumn>

        </GridColumns>
    </SfGrid>
}


@code {
    public List<CentreListDto> records { get; set; }
    private long StatusId = 0;
    public string defaultFilter = "Approved";

    protected override async Task OnInitializedAsync() {
        await GetCentreListByStatus();
    }

    private async Task GetCentreListByStatus() {
        try {
            var apiResponse = await httpClient.GetFromJsonAsync<ApiResponseDto<List<CentreListDto>>>($"{BaseURLs.GetCentreListByStatus}/{StatusId}");
            if (apiResponse.IsSuccessStatusCode) {
                records = apiResponse.Result;
            } else {
                matToaster.Add(apiResponse.Message + " : " + apiResponse.StatusCode, MatToastType.Danger, "Centre List Retrieval failed.");
            }
        } catch (Exception ex) {
            matToaster.Add(ex.GetBaseException().Message, MatToastType.Danger, "Centre List Retrieval Error");
        }

    }
}


1 Reply 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team September 3, 2020 05:48 AM UTC

Hi Simon,  
 
Greetings from Syncfusion support.  
 
Query: “Is it possible to set the default value of the Filter at the start, whenever I try I get an error of:  
 
We have analyzed your query and we understand that you are facing issue when trying to render Grid with Initial Filter. From your code example we found that you have defined the GridFilterSettings wrongly (i.e.) Not defined the GridFilterColumns tag. Hence the reported issue has occurred.  
 
Please find the modified code example from below  
 
<SfGrid DataSource="@Orders" AllowPaging="true" AllowFiltering="true" AllowSorting="true"> 
    <GridPageSettings PageSize="15"></GridPageSettings> 
    <GridFilterSettings> 
        <GridFilterColumns> 
            <GridFilterColumn Field="CustomerID" MatchCase=false Operator="Operator.StartsWith" Predicate="and" Value="@defaultFilter"></GridFilterColumn> 
        </GridFilterColumns> 
    </GridFilterSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="@nameof(Order.CustomerID)" HeaderText="Name" FilterSettings="@(new FilterSettings { Type = Syncfusion.Blazor.Grids.FilterType.CheckBox})" Width="150"> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
 
For your convenience we have prepared a sample using above code example which can be downloaded from below  
 
 
Refer our UG documentation for your reference 
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 


Marked as answer
Loader.
Up arrow icon