Dynamically Create and Delete Elements in Blazor

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.

@page "/"
<h4>Dynamic Elements</h4>
<style>
    body {
        font-size12px;
    }
 
    table {
        width50% !important;
    }
</style>
 
<table class="table">
    <thead>
        <tr>
            <th>Movie Name</th>
            <th>Release Year</th>
            <th>Co Production</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0i < movies.Counti++)
        {
            int index = i;
            <tr>
                <td><input type="text" value="@movies[index].Name" /> </td>
                <td><input type="number" value="@movies[index].ReleasedYear" /> </td>
                <td><input type="text" value="@movies[index].CoProduction" /> </td>
            </tr>
        }
    </tbody>
</table>
 
<button @onclick="AddNewRow" class="btn btn-primary">Create new row</button>
 
@code {
    public class Movie
    {
        public stringName { getset; }
        public intReleasedYear { getset; }
        public stringCoProduction { getset; }
    }
 
    private List<Moviemovies = new();
 
    private void AddNewRow() => movies.Add(new Movie { });
}

The image below is the result of the aforementioned code. When the user clicks the create new button, dynamic textbox elements are created.

Dynamically Create and Delete Elements in Blazor

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.

<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>
</tr>

The update methods assign the textbox values to the values of the movies object.

@code {
....
....
....

    private void UpdateName(int i, string value) => movies[i].Name = value;
    private void UpdateYear(int istring value) => movies[i].ReleasedYear = int.Parse(value);
    private void UpdateCoProduction(int istring value) => movies[i].CoProduction = value;
}

I have displayed the movie values using a foreach loop to ensure that the values are added.

@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>
}

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.

Dynamically Create and Delete Elements in Blazor

Step 3: Delete Dynamic Elements

To remove dynamically generated rows. I've included a Remove button.

@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>
}

The code for the remove button is as follows. It deletes a specific indexed item.

@code {
...
    ...
    ...
    private void RemoveRow(int i) => movies.RemoveAt(i);
    ...
    ...
    ...
}

The image below is the final output.

Dynamically Create and Delete Elements in Blazor
The whole coding is shown below.
@page "/"
<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 = 0i < movies.Counti++)
    {
        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 stringName { getset; }
        public intReleasedYear { getset; }
        public stringCoProduction { getset; }
    }
    private List<Moviemovies = new();
    private void RemoveRow(int i) => movies.RemoveAt(i);
    private void AddNewRow() => movies.Add(new Movie { });
    private void UpdateName(int istring value) => movies[i].Name = value;
    private void UpdateYear(int istring value) => movies[i].ReleasedYear = int.Parse(value);
    private void UpdateCoProduction(int istring value) => movies[i].CoProduction = value;
}

I hope this helps you. Keep coding.


Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Creating a C# Azure Function with Visual Studio: Step-by-Step Guide

Exploring EventCallback in Blazor: Building Interactive Components