0

I have table like showed below. I want to check/uncheck (for now just trying out check) values in the table based on the type I select. Could this be done somehow like this? Or do I somehow need to send request from javascript to see updated values? Or could this be done by using C# only?

var list = JsonConvert.SerializeObject(@table);
            <form asp-controller="Consumer" asp-action="" method="post">
                <div class="row justify-content-end" style="margin-top: 20px; margin-bottom: 20px;">
                    @foreach (var type in ObjectTypes.All)
                    {
                        <label style="margin-right: 10px;">
                            <input onclick="selectType(@list, @type)" style="margin-right: 5px;" type="checkbox" id="@type" name="type" value="@type">@type
                        </label>
                    }
                </div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th></th>
                        <th>Object Id</th>
                        <th>Object type</th>
                    </tr>
                    </thead>
                    <tbody>
                    @if (table.Count > 0)
                    {
                        @for (int i = 0; i < table.Count; i++)
                        {
                            <input type="hidden" name="@("items[" + i + "].ObjectId")" value="@table[i].ObjectId"/>
                            <input type="hidden" name="@("items[" + i + "].ObjectType")" value="@table[i].ObjectType"/>
                            <tr>
                                <td>@(++id)</td>
                                <td>
                                    <input name="@("items[" + i + "].Checked")" type="checkbox" value="true" @(table[i].Checked ? "checked" : " ")/>
                                </td>
                                <td>@table[i].ObjectId</td>
                                <td>@table[i].ObjectType</td>
                            </tr>
                        }
                    }
                    </tbody>
                </table>
            </form>

I had such js function:

function selectType(items, type)
{
   return items.filter(x => x.ObjectType === type).map(x => x.Checked = true);
}

But I need to add checked attribute to the checkbox input and I have no idea how that should be done

1 Answer 1

1

According to this documentation, I can see you are able to switch the "checked attribute" using:

document.getElementById("myCheck").checked = true;

But I do not get the reason why you want to return something in your selectType method. It is the method called when you will click on a checkbox, so you do not need to return anything. You will just need to change the Checked attribute for this tag. Something like that should do the trick:

function selectType(items, type)
{
   document.getElementById(type).checked = true; // I assume type is the Id of the element
}
Sign up to request clarification or add additional context in comments.

2 Comments

Type is not id of checkboxes I want to select :( I want to select checkboxes from <input name="@("items[" + i + "].Checked")" type="checkbox" value="true" @(table[i].Checked ? "checked" : " ")/> if type of item is selected above
Maybe you can try to use getElementsByName().

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.