I have a table:
<table id="Equipment-Table" class="table table-bordered table-hover">
<thead>
<tr>
<th class="text-center">
@Html.DisplayNameFor(model => model.TypeOfEquipment)
</th>
<th class="text-center">
@Html.DisplayNameFor(model => model.Deleted)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="text-center">
<td>
@Html.DisplayFor(modelItem => item.TypeOfEquipment)
</td>
<td>
@Html.DisplayFor(modelItem => item.Deleted)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</tbody>
</table>
I am using jQuery DataTables to style it and give it functionality.
This produces this:
My goal:
Order by the Deleted column. I want deleted items to be at the bottom of the list.
Here is what I have so far:
$(document).ready(function() {
var equipmentTable = $("#Equipment-Table").DataTable({
"order": [1, "asc"],
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [1, 2] },
{ "bSearchable": false, "aTargets": [1, 2] }
]
});
});
How can I make this happen?
Update
<tr class="text-center">
<td>
@Html.DisplayFor(modelItem => item.TypeOfEquipment)
</td>
@if (item.Deleted)
{
<td data-order="1">
@Html.DisplayFor(modelItem => item.Deleted)
</td>
}
else
{
<td data-order="0">
@Html.DisplayFor(modelItem => item.Deleted)
</td>
}
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
$(document).ready(function() {
var equipmentTable = $("#Equipment-Table").DataTable({
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [1, 2] },
{ "bSearchable": false, "aTargets": [1, 2] }
]
});
$('#Equipment-Table input[type="checkbox"]').on('change', function () {
// Update data-sort on closest <td>
$(this).closest('td').attr('data-order', this.checked ? 1 : 0);
// Store row reference so we can reset its data
var $tr = $(this).closest('tr');
// Force resorting
equipmentTable
.row($tr)
.invalidate()
.order([1, 'asc'])
.draw();
});
});
