web-dev-qa-db-fra.com

Blazor - afficher la boîte de dialogue de confirmation avant de supprimer / mettre à jour?

Dans les extraits de code Blazor (côté serveur) suivants. Comment inviter la boîte de dialogue de confirmation?

<tbody>
    @foreach (var r in lists)
    {
        var s = r.ID;
        <tr>
            <td>@s</td>
            <td><button class="btn btn-primary" @onclick="() => DeleteSymbol(s)">Delete</button></td>
        </tr>
    }
</tbody>

@code {
    async Task DeleteSymbol(string id)
    {
        // Get confirmation response from user before running deletion?
        // Delete!
    }
}
5
ca9163d9

J'ai créé un simple composant popconfirm.

 <div class="pop-container">
        @if (Show)
        {
            <div class="popconfirm">
                @Message
                <button type="button" class="btn btn-warning" @onclick="() => Confirmation(false)">No</button>
                <button type="button" class="btn btn-primary" @onclick="() => Confirmation(true)">Yes</button>
            </div>

        }
        <button type="button" class="@Class" @onclick="ShowPop">@Title</button>
    </div>
    @code {
        public bool Show { get; set; }
        [Parameter] public string Title { get; set; } = "Delete";
        [Parameter] public string Class { get; set; } = "btn btn-danger";
        [Parameter] public string Message { get; set; } = "Are you sure?";
        [Parameter] public EventCallback<bool> ConfirmedChanged { get; set; }

        public async Task Confirmation(bool value)
        {
            Show = false;
            await ConfirmedChanged.InvokeAsync(value);
        }

        public void ShowPop()
        {
            Show = true;
        }
    }

CSS

.pop-container{
    position: relative;
}

.popconfirm{
    background-color: white;
    border-style: solid;
    border-width: 1px;
    border-color: lightblue;
    width: 250px;
    position: absolute;
    top: -50px;
    padding: 10px;
    border-radius: 8px;
}

Usage

<Popconfirm ConfirmedChanged="Test" />
@code{

    public void Test(bool test)
    {
        Console.WriteLine(test);
    }
}
2
Egill Benediktsson