简体   繁体   English

Blazor eventcallback 仅在设置 eventcallback 时执行一次

[英]Blazor eventcallback only executes once when eventcallback is set

In bazor wasm I am making a paginator component, to add pagination to a table component.在 bazor wasm 中,我正在制作一个分页器组件,以向表格组件添加分页。 The paginator component takes an event callback ( PageChanged ) which is executed when the user changes the page.分页器组件接受一个事件回调 ( PageChanged ),该回调在用户更改页面时执行。

Here is the view这是视图

<div id="paginator-container">
<div id="paginator-controls">
    <button type="button"
            class="btn btn-action"
            @onclick="@this.GoToFirstPage"
            disabled="@this.IsFirstPage">
        <i class="fas fa-angle-double-left"></i>
    </button>
    <button type="button"
            class="btn btn-action"
            @onclick="@this.GoToPreviousPage"
            disabled="@this.IsFirstPage">
        <i class="fas fa-angle-left"></i>
    </button>
    <span>
        page @this.CurrentPage of @this.PageCount
    </span>
    <button class="btn btn-action"
            @onclick="@this.GoToNextPage"
            disabled="@this.IsLastPage">
        <i class="fas fa-angle-right"></i>
    </button>
    <button class="btn btn-action"
            @onclick="@this.GoToLastPage"
            disabled="@this.IsLastPage">
        <i class="fas fa-angle-double-right"></i>
    </button>
</div>

And the associated code以及相关的代码

public partial class Paginator
{
    [Parameter]
    public int Lenght { get; set; }

    [Parameter]
    public EventCallback<InnosysPaginatorContext> PageChanged { get; set; }

    [Parameter]
    public int PageSize { get; set; } = 50;

    private int CurrentPage { get; set; }

    private int PageCount { get; set; }

    private bool IsFirstPage
        => this.CurrentPage == 1;

    private bool IsLastPage
        => this.CurrentPage == this.PageCount;

    protected override void OnParametersSet()
    {
        this.SetPageCount();

        this.CurrentPage = 1;

        base.OnParametersSet();
    }

    private void SetPageCount()
    {
        decimal pageCount = (decimal)this.Lenght / this.PageSize;

        if (pageCount != Math.Floor(pageCount))
        {
            pageCount = Math.Floor(pageCount + 1);
        }

        this.PageCount = (int)pageCount;
    }

    private async Task GoToFirstPage()
        => await this.GoToPage(1);

    private async Task GoToPreviousPage()
        => await this.GoToPage(this.CurrentPage - 1);

    private async Task GoToNextPage()
        => await this.GoToPage(this.CurrentPage + 1);

    private async Task GoToLastPage()
        => await this.GoToPage(this.PageCount);

    private Task GoToPage(int pageIndex)
    {
        this.CurrentPage = Math.Clamp(pageIndex, 1, this.PageCount);

        return this.ExecutePageChangedEvent();
    }

    private async Task ExecutePageChangedEvent()
        => await this.PageChanged.InvokeAsync(new InnosysPaginatorContext
        {
            CurrentPage = this.CurrentPage,
            PageSize = this.PageSize
        });
}

When I use the paginator without setting PageChanged , like this:当我使用分页器而不设置PageChanged时,如下所示:

<Paginator Lenght="300" />

I can move through the pages just fine.我可以很好地浏览页面。

However, if I set the callback, like this但是,如果我设置回调,就像这样

<Paginator Lenght="300"
       PageChanged="@this.OnPageChangedTest" />

<p>Page: @pageIndex</p>

<p>page size: @pageSize</p>

@code{

    int pageIndex = 1;

    int pageSize = 50;

    void OnPageChangedTest(InnosysPaginatorContext innosysPaginatorContext)
    {
        pageIndex = innosysPaginatorContext.CurrentPage;

        pageSize = innosysPaginatorContext.PageSize;
    }

}

The buttons only work once and I can only get to page 2. Subsequent clicks on the buttons for the next page execute the callback, but the CurrentPage of the paginator is stuck on page 2. Why does this happen?按钮只能工作一次,我只能到第2页。随后点击下一页的按钮执行回调,但是分页器的CurrentPage卡在第2页。为什么会出现这种情况?

The issue must be here:问题应该在这里:

this.CurrentPage = 1; 

is executed whenever OnParametersSet is executed;每当执行 OnParametersSet 时执行; actually, you don't need to set CurrentPage to 1. And if you need to do it only once, you should do it in the OnInitialized(Async) pair实际上,您不需要将 CurrentPage 设置为 1。如果您只需要执行一次,您应该在 OnInitialized(Async) 对中执行

 protected override void OnParametersSet()
    {
        this.SetPageCount();

      //  this.CurrentPage = 1;

        base.OnParametersSet();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM