Dynamically Create and Delete Elements in Blazor
This blog is going to explain how to create and delete elements in Blazor. Let's see step by step
Step 1: Create Dynamic Element
The code for creating dynamic elements is shown below. I created a class named Movie with the fields Name, ReleasedYear, and CoProduction. Inside the for loop, three textboxes were added to collect movie information. I also included a button to generate a new row of text boxes.
The image below is the result of the aforementioned code. When the user clicks the create new button, dynamic textbox elements are created.
Step 2: Set Values for Dynamic Elements
The next step is to set the values for the dynamic components. In the code snippet below the @onchange event is added to all textboxes. Also, a method to update values has been added.
<td><input type="text" value="@movies[index].Name" @onchange="(e) => UpdateName(index, e.Value.ToString())" /> </td>
<td><input type="number" value="@movies[index].ReleasedYear" @onchange="(e) => UpdateYear(index, e.Value.ToString())" /> </td>
<td><input type="text" value="@movies[index].CoProduction" @onchange="(e) => UpdateCoProduction(index, e.Value.ToString())" /> </td>
</tr>
The update methods assign the textbox values to the values of the movies object.
....
....
....
private void UpdateName(int i, string value) => movies[i].Name = value;
private void UpdateYear(int i, string value) => movies[i].ReleasedYear = int.Parse(value);
private void UpdateCoProduction(int i, string value) => movies[i].CoProduction = value;
}
I have displayed the movie values using a foreach loop to ensure that the values are added.
{
<table class="table">
<thead>
<tr>
<th>Movie Name</th>
<th>Release Year</th>
<th>Co Production</th>
</tr>
</thead>
<tbody>
@foreach (var item in movies)
{
<tr>
<td>@item.Name</td>
<td>@item.ReleasedYear</td>
<td>@item.CoProduction</td>
</tr>
}
</tbody>
</table>
}
The image below is the result of the aforementioned code. When a user enters movie information, it is saved in the movies object and displayed.
Step 3: Delete Dynamic Elements
To remove dynamically generated rows. I've included a Remove button.
{
int index = i;
<tr>
<td><input type="text" value="@movies[index].Name" @onchange="(e) => UpdateName(index, e.Value.ToString())" /> </td>
<td><input type="number" value="@movies[index].ReleasedYear" @onchange="(e) => UpdateYear(index, e.Value.ToString())" /> </td>
<td><input type="text" value="@movies[index].CoProduction" @onchange="(e) => UpdateCoProduction(index, e.Value.ToString())" /> </td>
<td>
<button @onclick="() => RemoveRow(index)" class="btn btn-secondary">Remove</button>
</td>
</tr>
}
The code for the remove button is as follows. It deletes a specific indexed item.
...
...
...
private void RemoveRow(int i) => movies.RemoveAt(i);
...
...
...
}
The image below is the final output.
The whole coding is shown below.<h4>Dynamic Elements</h4>
<style>
body {
font-size: 12px;
}
table {
width: 50% !important;
}
</style>
@if(movies.Count>0)
{
<table class="table">
<thead>
<tr>
<th>Movie Name</th>
<th>Release Year</th>
<th>Co Production</th>
</tr>
</thead>
<tbody>
@foreach (var item in movies)
{
<tr>
<td>@item.Name</td>
<td>@item.ReleasedYear</td>
<td>@item.CoProduction</td>
</tr>
}
</tbody>
</table>
}
<hr />
<table class="table">
<thead>
<tr>
<th>Movie Name</th>
<th>Release Year</th>
<th>Co Production</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < movies.Count; i++)
{
int index = i;
<tr>
<td><input type="text" value="@movies[index].Name" @onchange="(e) => UpdateName(index, e.Value.ToString())" /> </td>
<td><input type="number" value="@movies[index].ReleasedYear" @onchange="(e) => UpdateYear(index, e.Value.ToString())" /> </td>
<td><input type="text" value="@movies[index].CoProduction" @onchange="(e) => UpdateCoProduction(index, e.Value.ToString())" /> </td>
<td>
<button @onclick="() => RemoveRow(index)" class="btn btn-secondary">Remove</button>
</td>
</tr>
}
</tbody>
</table>
<button @onclick="AddNewRow" class="btn btn-primary">Create new row</button>
@code {
public class Movie
{
public string? Name { get; set; }
public int? ReleasedYear { get; set; }
public string? CoProduction { get; set; }
}
private List<Movie> movies = new();
private void RemoveRow(int i) => movies.RemoveAt(i);
private void AddNewRow() => movies.Add(new Movie { });
private void UpdateName(int i, string value) => movies[i].Name = value;
private void UpdateYear(int i, string value) => movies[i].ReleasedYear = int.Parse(value);
private void UpdateCoProduction(int i, string value) => movies[i].CoProduction = value;
}
I hope this helps you. Keep coding.
Comments
Post a Comment