Starts in:
2 DAYS
3 HRS
34 MIN
23 SEC
Starts in:
2 D
3 H
34 M
23 S
Open Closed

Blazor Toolbar Event Callback for Complex Components #6623


User avatar
0
dhill created
  • ABP Framework version: v8.0.2
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)
  • Steps to reproduce the issue:

We built a button dropdown component designed to control visibility of columns on the Blazorise DataGrid.

This component works fine when embedded in the body of the page. We would like to move the button to the Toolbar to keep the look and feel consistent.

We are having trouble figuring out how to access event callbacks from the toolbar contributor.

We try to add the component like this but the event callbacks don't work.

        var arguments = new Dictionary<string, object?>();
        arguments.Add("SelectAllCheckedValue", SelectAllCheckedValue);
        arguments.Add("TestDateCheckedValue", TestDateCheckedValue);
        arguments.Add("AttachmentsCheckedValue", AttachmentsCheckedValue);
        arguments.Add("AttachmentsCheckedValueChanged", AttachmentsCheckedValueChanged);
        Toolbar.AddComponent<SubstanceAbuseTestsDataGridColumnVisibilityComponent>(arguments);

Here is the component referenced on the razor page.

    <DataGridColumnVisibilityComponent @bind-SelectAllCheckedValue=SelectAllCheckedValue
                                       @bind-TestDateCheckedValue=TestDateCheckedValue
                                       @bind-AttachmentsCheckedValue=AttachmentsCheckedValue />

Here is the code behind c#

protected bool TestDateCheckedValue { get; set; }

protected bool AttachmentsCheckedValue { get; set; }

protected bool SelectAllCheckedValue { get; set; }

protected async Task SetColumnVisibilityAsync()
{
    // ToDo bind cookie here
    SelectAllCheckedValue = true;
    TestDateCheckedValue = true;
    AttachmentsCheckedValue = true;
}        protected bool TestDateCheckedValue { get; set; }

protected bool AttachmentsCheckedValue { get; set; }

protected bool SelectAllCheckedValue { get; set; }

protected async Task SetColumnVisibilityAsync()
{
    // ToDo bind cookie here
    SelectAllCheckedValue = true;
    TestDateCheckedValue = true;
    AttachmentsCheckedValue = true;
}

Here is the component.

@page "/DataGridColumnVisibility"

<Dropdown>
    <DropdownToggle Color="Color.Primary">
        <Icon Name="IconName.Eye" /> Field Visibility
    </DropdownToggle>
    <DropdownMenu >
        <DropdownItem ShowCheckbox Checked="SelectAllCheckedValue" CheckedChanged="UpdateSelectAllCheckedValue">Select All</DropdownItem>
        <DropdownDivider />
         <DropdownItem ShowCheckbox Checked="TestDateCheckedValue" CheckedChanged="UpdateTestDateCheckedValue">Test Date</DropdownItem>
         <DropdownItem ShowCheckbox Checked="AttachmentsCheckedValue" CheckedChanged="UpdateAttachmentsCheckedValue">Attachments</DropdownItem>
    </DropdownMenu>
</Dropdown>

 @code {
    [Parameter]
    public bool SelectAllCheckedValue { get; set; }

    [Parameter]
    public EventCallback<bool> SelectAllCheckedValueChanged { get; set; }

    async Task UpdateSelectAllCheckedValue()
    {
        SelectAllCheckedValue = !SelectAllCheckedValue;
        TestDateCheckedValue = SelectAllCheckedValue;
        AttachmentsCheckedValue = SelectAllCheckedValue;

        await SelectAllCheckedValueChanged.InvokeAsync(SelectAllCheckedValue);
        await TestDateCheckedValueChanged.InvokeAsync(TestDateCheckedValue);
        await AttachmentsCheckedValueChanged.InvokeAsync(AttachmentsCheckedValue);
    }


    [Parameter]
    public bool TestDateCheckedValue { get; set; }

    [Parameter]
    public EventCallback<bool> TestDateCheckedValueChanged { get; set; }

    protected async Task UpdateTestDateCheckedValue()
    {
        TestDateCheckedValue = !TestDateCheckedValue;
        await TestDateCheckedValueChanged.InvokeAsync(TestDateCheckedValue);
    }

    [Parameter]
    public bool AttachmentsCheckedValue { get; set; }

    [Parameter]
    public EventCallback<bool> AttachmentsCheckedValueChanged { get; set; }

    protected async Task UpdateAttachmentsCheckedValue()
    {
        AttachmentsCheckedValue = !AttachmentsCheckedValue;
        await AttachmentsCheckedValueChanged.InvokeAsync(AttachmentsCheckedValue);
    }
}

We use this on the Displayable property of the DataGrid column

<DataGridColumn TItem="ExampleDto"
                 Field="TestDate"
                 Displayable=TestDateCheckedValue
                 Editable="true"
                 Caption="@L["TestDate"]">
     <DisplayTemplate>
         @context.TestDate.ToShortDateString()
    </DisplayTemplate>
</DataGridColumn>
    [Parameter]
    public EventCallback<bool> AttachmentsCheckedValueChanged { get; set; }

    protected async Task UpdateAttachmentsCheckedValue()
    {
        AttachmentsCheckedValue = !AttachmentsCheckedValue;
        await AttachmentsCheckedValueChanged.InvokeAsync(AttachmentsCheckedValue);
    }
}

2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    ABP uses the following method to dynamically render components

    Can you check this? https://stackoverflow.com/questions/59848957/pass-function-as-parameter-to-dynamically-created-component-blazor https://github.com/dotnet/AspNetCore.Docs/issues/16479

  • User Avatar
    0
    dhill created

    That worked. Thanks.

    Here's my final code for reference.

                var arguments = GetToolbarComponentArguments();
                Toolbar.AddComponent<DataGridColumnVisibilitySelector>(arguments);
                
    
    
            private Dictionary<string, object?> GetToolbarComponentArguments()
            {
                var arguments = new Dictionary<string, object?>();
    
                EventCallback<bool> NotesCheckedValueChanged = EventCallback.Factory.Create<bool>(this, UpdateNotesCheckedValue);
                arguments.Add(nameof(NotesCheckedValueChanged), NotesCheckedValueChanged);
    
    
                return arguments;
            }
            
    
            private async Task UpdateNotesCheckedValue(bool checkedValue)
            {
                NotesColumnVisible = checkedValue;
                await Task.CompletedTask;
            }
            
                  <DataGridColumn TItem="ScreeningWithNavigationPropertiesDto"
                          Field="Screening.Notes"
                                    Caption="@L["Notes"]"
                                    Displayable=NotesColumnVisible
                                    Editable="true">
                  </DataGridColumn>
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06