1

I have a table displaying object elements from a dynamic list, hence the table should be able to grow and shrink in size, as the user sees fit. How would i be able to get the specific row index?

I basically have a shopping cart, with price, quantity, name and so forth. And lastly, a delete button, that when clicked should delete the current row. So i need to be able to associate the current list element with its respective delete button.

Below is my table:

<div class="customContainer">
    <table class="customTable">
        <thead class="customHead">
            <tr class="customRow">
                <th class="customHead2">Product</th>
                <th class="customHead2">Quantity</th>
                <th class="customHead2">Price</th>
                <th class="customHead2">Total</th>
                <th class="customHead2">Remove</th>
            </tr>
        </thead>

        <tbody class="customBody">
            @foreach (var itemList in Model)
            {
                <tr class="customRow">
                    <td class="customData">@itemList.Name</td>
                    <td class="customData">@itemList.Quantity</td>
                    <td class="customData">@itemList.Price</td>
                    <td class="customData">@itemList.Total</td>
                    <td class="customData"><button class="btn-dark">Delete</button></td>
                </tr>
            }
        </tbody>
    </table>
</div>

Would there be a way to give the delete button, 'btn-dark' , a dynamic id that increments/decrements as the table grows/shrinks?

4 Answers 4

1

Without changing your foreach loop I would have done something like this:

<div class="customContainer">
    <table class="customTable">
        <thead class="customHead">
            <tr class="customRow">
                <th class="customHead2">Product</th>
                <th class="customHead2">Quantity</th>
                <th class="customHead2">Price</th>
                <th class="customHead2">Total</th>
                <th class="customHead2">Remove</th>
            </tr>
        </thead>

        <tbody class="customBody">
            var model = Model.ToList();
            @foreach (var itemList in model)
            {
                @{
                  var index = model.IndexOf(itemList);
                 }
                <tr class="customRow">
                    <td class="customData">@itemList.Name</td>
                    <td class="customData">@itemList.Quantity</td>
                    <td class="customData">@itemList.Price</td>
                    <td class="customData">@itemList.Total</td>
                    <td class="customData"><button data-delete-id="@index" class="btn-dark">Delete</button></td>
                </tr>
            }
        </tbody>
    </table>
</div>

Then you'd use JQuery to retrieve the data-delete-id parameter value on click of the button and remove the item from the table.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This solution is perfect for what i need.
1

Looks like your dynamic list is been passed into the view so why not have the Unique ID created before you pass it in?

You can then bind it something like below and call a action in the controller passing the ID(or modify to use AJAX or call JS function)

<td><a class="btn-dark" href="@Url.Action("Action", "Controller", new { id = @itemList.Id})">Delete </a>
 </td>

You can then bind that unique ID to the delete button and do what you want with it.

Comments

1

Assuming Model is a List you can do:

@for (int i = 0; i < Model.Count; i++){
    var itemList = Model[i];
    ....
    <button id="Delete@(i)" class="btn-dark">Delete</button>
    ....
}

Generates:

<button id="Delete1" class="btn-dark">Delete</button>
<button id="Delete2" class="btn-dark">Delete</button>
<button id="Delete3" class="btn-dark">Delete</button>
...

Another option less efficient in terms of performance but requiring less code it's using IndexOf() to get the index of an element. If Model is IEnumerable<T> you need to cast it as a List first.

Comments

1

You can simply remove the row from the table using closest function of jquery.

$(".customTable").on('click', '.delete', function (event) {
   $(this).closest(' tr').remove();
});

For the above code your delete button should contain the delete class.

<button class="btn-dark delete">Delete</button>

1 Comment

Quick, easy fix. But unfortunately these changes wont reflect in the list itself, so when the page is refreshed, the table will re-populate itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.