Search code examples
autocompleteblazormudblazor

MudAutocomplete, making some items selectable and some others not


I'm creating a blazor component using the MudAutocomplete component. So far it's working as expected, but I need to set some items as selectable and some others not selectable.

For example, I have categories and items, each item belongs to a category. So I need to select only the items in the category no the category itself. Is there a way I can accomplish that? I've been searching in the docs and I haven't found anything yet regarding events on selection, just boolean values.

Basically I created a data structure for modeling the categories and items (n-ary tree), and each item contains a prop called isSelectable besides title and Id. The render of the component is just fine, I have a autocomplete component with nested items, but all of them are selectable. I only need to select the leaves.


Solution

  • You'll have to use the ItemDisabledFunc attribute on the component.

    <MudAutoComplete T="YourType" 
                     Label="Item" 
                     @bind-Value="YourModel.SelectedItem" 
                     ItemDisabledFunc="IsDisabled"></MudAutoComplete>
    
    @code {
        private bool IsDisabled(YourType item)
        {
            return !item.isSelectable;
        }
    }