How to add chip items dynamically in the Blazor Chips component?
This KB explains how to add chip item dynamically in the Blazor Chip component
- To get started with Blazor Chip component, refer to this link - https://blazor.syncfusion.com/documentation/chip/getting-started
- Render Blazor TextBox and Blazor Button component with click event.
@page "/" @using Syncfusion.Blazor.Buttons @using Syncfusion.Blazor.Inputs <SfTextBox Width="200px" @bind-Value="TextboxValue" Placeholder='Enter the chip text' ShowClearButton="true"></SfTextBox> <SfButton @onclick="onToggleClick" IsToggle="true" IsPrimary="true" Content="Add Chip"></SfButton> @code { string TextboxValue = string.Empty; public void onToggleClick() { } }
- Declare `Chip` class with Value and Text members.
public class Chip { public string Value { get; set; } = string.Empty; public string Text { get; set; } = string.Empty; }
- Render a Chip component using collection.
@page "/" <SfChip EnableDelete="true"> <ChipItems> @foreach (Chip item in ChipItemCollection) { <ChipItem Text="@item.Text" Value="@item.Value"></ChipItem> } </ChipItems> </SfChip> @code { List<Chip> ChipItemCollection = new List<Chip>(); public class Chip { public string Value { get; set; } = string.Empty; public string Text { get; set; } = string.Empty; } }
- In click event, get input from Textbox and render a chip component.
@page "/" @using Syncfusion.Blazor.Buttons @using Syncfusion.Blazor.Inputs <SfTextBox Width="200px" @bind-Value="TextboxValue" Placeholder='Enter the chip text' ShowClearButton="true"></SfTextBox> <SfButton @onclick="onToggleClick" IsToggle="true" IsPrimary="true" Content="Add Chip"></SfButton> <br /> <br /> <SfChip EnableDelete="true"> <ChipItems> @foreach (Chip item in ChipItemCollection) { <ChipItem Text="@item.Text" Value="@item.Value"></ChipItem> } </ChipItems> </SfChip> @code { List<Chip> ChipItemCollection = new List<Chip>(); string TextboxValue = string.Empty; public void onToggleClick() { Chip chips = new Chip(); chips.Text = TextboxValue; chips.Value = TextboxValue; ChipItemCollection.Add(chips); } }
While run the application, the Chip will not be rendered. While you type in a textbox and click a button. The chip will be rendered dynamically based on modified collection.
Refer to our documentation and online samples for more features. If you have any queries, please let us know in the comments below. You can also contact us through our Support forum or Support ticket. We are happy to assist you!
Conclusion
I hope you enjoyed learning about how to add chip items dynamically in the Blazor Chips component.
You can refer to our Blazor Chips’s feature tour page to know about its other groundbreaking feature representations. You can also explore our Blazor Chips documentation to understand how to present and manipulate data.
For current customers, you can check out our Blazor components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Blazor Chips and other Blazor components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!