MudBlazor/MudBlazor

MudTable does not properly switch into edit mode after adding new item to collection

開放

#3,279 建立於 2021年11月6日

 (4 則留言) (9 個反應) (0 位負責人)C# (1,631 個分叉)batch import
buggood first issuehas workaround

倉庫指標

星標
 (10,394 顆星)
PR 合併指標
 (平均合併 6天 6小時) (30 天內合併 108 個 PR)

描述

Bug type

Component

Component name

MudTable

What happened?

When adding an item to a MudTable's bound collection, immediately switching to edit mode on the new item using SetEditingItem fails. From then on, the table is stuck in edit mode on (as far as the editor is concerned) a non-existent item.

The issue is in the FilteredItems getter on MudTable:

private IEnumerable<T> _preEditSort { get; set; } = null;
private bool _hasPreEditSort => _preEditSort != null;

public IEnumerable<T> FilteredItems {
    get {
        if (_isEditing && _hasPreEditSort)
            return _preEditSort;
        if (ServerData != null) {
            _preEditSort = _server_data.Items.ToList();
            return _preEditSort;
        }

        if (Filter == null) {
            _preEditSort = Context.Sort(Items).ToList();
            return _preEditSort;
        }
        _preEditSort = Context.Sort(Items.Where(Filter)).ToList();
        return _preEditSort;
    }
}

If _isEditing is true, which is the case once SetEditingItem is called, it will return _preEditSort, which will contain a stale version of the items collection. This will consistently happen until _isEditing is false, so no new updates to the table will display until edit mode is disabled, which can only happen through code since the UI will not show the edit mode on a non-existent (as far as it is concerned) line item.

Work Around

You can work around the issue by creating an artificial delay between adding an item to the collection and entering edit mode. The attached reproduction link will accomplish this using the red add button by calling a function that introduces a slight delay to let Blazor call the FilteredItems property and have the backend _preEditSort object update with the new list contents before edit mode is entered.

Expected behavior

You should be able to add an item to the bound MudTable collection and immediately enter edit mode without introducing a delay.

Failing this, there should be a built-in function of MudTable that allows a new item to be added to the collection and immediately edits it. I understand this latter suggestion is going to be part of #2842. Nonetheless, the existing issue should still be addressed somehow.

Reproduction link

https://try.mudblazor.com/snippet/mamFvFuAbvICQrvW

Reproduction steps

See reproduction link.

Relevant log output

No response

Version (bug)

5.2.0

Version (working)

No response

What browsers are you seeing the problem on?

Firefox, Microsoft Edge

On what operating system are you experiencing the issue?

Windows

Pull Request

  • I would like to do a Pull Request

Code of Conduct

  • I agree to follow this project's Code of Conduct

貢獻者指南