HelpMyHospital
HelpMyHospital

Reputation: 13

Clickable button inside of a table

I am writing a View of a composite list of employee information. The view will display information such as first name, last name, job code etc... My one problem is that I would like to have the boolean cells be clickable buttons.

For example: The HasHealthcare column will contain boolean values.

<thead>
<tr>
<th><label> FirstName</label></th>
<th><label> LastName</label></th>
<th><label> HasHealthcare</label></th>
</tr>
</thead>

<tbody>
@for (int i=0; i < Model.Employees.Count; i++)
{
<tr>
<td>@Model.Employees[i].FirstName</td>
<td>@Model.Employees[i].LastName</td>
<td>@Model.Employees[i].HasHealthcare</td>
</tr>
</tbody>

Table:

First Name | Last Name | Has Healthcare

Bob        |  Smith    | True

I would like "True" to be a clickable button so that if I click it the opposite boolean value appears.

Upvotes: 1

Views: 111

Answers (2)

Sgedda
Sgedda

Reputation: 1531

If you need to autosave at each click you could use Ajax and JQuery like below. And you would need a matching [HttPost] actionmethod as well.

<thead>
<tr>
<th><label> FirstName</label></th>
<th><label> LastName</label></th>
<th><label> HasHealthcare</label></th>
</tr>
</thead>

<tbody>
@for (int i=0; i < Model.Employees.Count; i++)
{
<tr>
<td>@Model.Employees[i].FirstName</td>
<td>@Model.Employees[i].LastName</td>
<td><a class="healthcare" data-id="@Model.Employees[i].Id" href="#">@Model.Employees[i].HasHealthcare</a></td>
</tr>
}
</tbody>

<script>
$().ready(function(){
     $(".healthcare").click(function(){
      var element = this;
      $.ajax({
        type: "POST",
        url: url,
        data: {isHealthcare:$(element).text().toLowerCase()==='true', id:$(this).data("id") },
        success: function(result){
           $(element).text(result);
        },
        dataType: "json"
     });
   });
})
</script>

Upvotes: 0

Isma
Isma

Reputation: 15200

If you don't mind using a checkbox, the following may work for you:

<table>
     <thead>
            <tr>
                <th><label> FirstName</label></th>
                <th><label> LastName</label></th>
                <th><label> HasHealthcare</label></th>
            </tr>
        </thead>

    <tbody>
        @for (int i = 0; i < Model.Employees.Count; i++)
        {
            <tr>
                <td>@Model.Employees[i].FirstName</td>
                <td>@Model.Employees[i].LastName</td>
                <td>@Html.CheckBoxFor(x => x.Employees[i].HasHealthCare, false)</td>
            </tr>
        }
    </tbody>
</table>

Upvotes: 4

Related Questions