Add public method to RadzenTabs to enable setting selected tab #897

Closed
opened 2026-01-29 17:45:58 +00:00 by claunia · 5 comments
Owner

Originally created by @0xor1 on GitHub (Jun 18, 2023).

Is your feature request related to a problem? Please describe.
I want to be able to programmatically set which RadzenTab is selected

Describe the solution you'd like
A public method like _tabsRef.SetSelectedIndex(0); that would only do work changing the selected tab if it wasnt already the passed in value.

Describe alternatives you've considered
currently Im workign around this by doing:

            _tabs.SelectedIndex = 1;
            await _tabs.SetParametersAsync(ParameterView.FromDictionary(new Dictionary<string, object?>()
            {
                {"SelectedIndex", 0}
            }));

This forces the behavior I want but its pretty ugly and I might be forcing it to happen when index 0 is already the selected tab. As I have to set the parameter to 1, before calling SetPArametersAsync passing in the desired 0 value.

Originally created by @0xor1 on GitHub (Jun 18, 2023). **Is your feature request related to a problem? Please describe.** I want to be able to programmatically set which RadzenTab is selected **Describe the solution you'd like** A public method like `_tabsRef.SetSelectedIndex(0);` that would only do work changing the selected tab if it wasnt already the passed in value. **Describe alternatives you've considered** currently Im workign around this by doing: ``` _tabs.SelectedIndex = 1; await _tabs.SetParametersAsync(ParameterView.FromDictionary(new Dictionary<string, object?>() { {"SelectedIndex", 0} })); ``` This forces the behavior I want but its pretty ugly and I might be forcing it to happen when index 0 is already the selected tab. As I have to set the parameter to 1, before calling SetPArametersAsync passing in the desired 0 value.
Author
Owner

@akorchev commented on GitHub (Jun 19, 2023):

Have you tried using the SelectedIndex parameter instead?

<RadzenTabs @bind-SelectedIndex=@selectedIndex>

@akorchev commented on GitHub (Jun 19, 2023): Have you tried using the SelectedIndex parameter instead? `<RadzenTabs @bind-SelectedIndex=@selectedIndex>`
Author
Owner

@0xor1 commented on GitHub (Jun 19, 2023):

I just tried that and it has no effect, setting SelectedIndex seems to work when initializing only, Im responding to a user action and want to set the SelectedIndex after the component was initialized and rendered long ago. my work around is to:

_tabs.SelectedIndex = 1;
await _tabs.SetParametersAsync(ParameterView.FromDictionary(new Dictionary<string, object?>()
            {
                {"SelectedIndex", 0}
            }));

as i want it to reset to 0 but the SetParametersAsync method requires the value to actually be different:

public override async Task SetParametersAsync(ParameterView parameters)
    {
      if (parameters.DidParameterChange<int>("SelectedIndex", this.SelectedIndex))
        this.selectedIndex = parameters.GetValueOrDefault<int>("SelectedIndex");
      await base.SetParametersAsync(parameters);
    }

which is why I manually set it to 1 before calling SetParametersAsync with the desired 0 value.

@0xor1 commented on GitHub (Jun 19, 2023): I just tried that and it has no effect, setting SelectedIndex seems to work when initializing only, Im responding to a user action and want to set the SelectedIndex after the component was initialized and rendered long ago. my work around is to: ``` _tabs.SelectedIndex = 1; await _tabs.SetParametersAsync(ParameterView.FromDictionary(new Dictionary<string, object?>() { {"SelectedIndex", 0} })); ``` as i want it to reset to `0` but the `SetParametersAsync` method requires the value to actually be different: ``` public override async Task SetParametersAsync(ParameterView parameters) { if (parameters.DidParameterChange<int>("SelectedIndex", this.SelectedIndex)) this.selectedIndex = parameters.GetValueOrDefault<int>("SelectedIndex"); await base.SetParametersAsync(parameters); } ``` which is why I manually set it to `1` before calling `SetParametersAsync` with the desired `0` value.
Author
Owner

@akorchev commented on GitHub (Jun 19, 2023):

Setting the SelectedIndex seems to work just fine. Not sure why it isn't working for you.
select-tab

Can you provide a simple reproduction where setting SelectedIndex doesn't work?

@akorchev commented on GitHub (Jun 19, 2023): Setting the SelectedIndex seems to work just fine. Not sure why it isn't working for you. ![select-tab](https://github.com/radzenhq/radzen-blazor/assets/454726/0822af9c-ec95-4d69-823f-42f169ac0b43) Can you provide a simple reproduction where setting SelectedIndex doesn't work?
Author
Owner

@0xor1 commented on GitHub (Jun 19, 2023):

interesting, let me try again

@0xor1 commented on GitHub (Jun 19, 2023): interesting, let me try again
Author
Owner

@0xor1 commented on GitHub (Jun 19, 2023):

the two way binding does work, i was setting it on the wrong element in haste.

@0xor1 commented on GitHub (Jun 19, 2023): the two way binding does work, i was setting it on the wrong element in haste.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#897