Reputation: 1211
In my html table there one column ("IsReserved") is list of Checkboxes some are checked and some not. When user click on IsReserved header than table is sorted by it. Means on first click in asc order and on second click desc order. I want this problem is done by usig javascript or jquery and if any other then please.
In short i want to sort table according checkboxes in "IsReserved" column Thanks.
function sortTable(f, n) {
var rows = $('#tab tbody tr').get();
rows.sort(function (a, b) {
var A = $(a).children('td').eq(n).text().toUpperCase();
var B = $(b).children('td').eq(n).text().toUpperCase();
$("input:checkbox").attr("id")
parseInt($(a).data("sort"));
if (A < B) {
return -1 * f;
}
if (A > B) {
return 1 * f;
}
return 0;
});
$.each(rows, function (index, row) {
$('#tab').children('tbody').append(row);
});
}
var f_sl = 1;
var f_nm = 1;
//sort table by name
$("#name").click(function () {
f_sl *= -1;
var n = $(this).prevAll().length;
sortTable(f_sl, n);
});
//sort table by code
$("#code").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by category
$("#category").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by saleprice
$("#saleprice").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by lead time
$("#leadtime").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by qty per unit
$("#qtu").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by current stock
$("#currentstock").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//sort table by record level
$("#recordlevel").click(function () {
f_nm *= -1;
var n = $(this).prevAll().length;
sortTable(f_nm, n);
});
//end of sort table
<tbody data-bind="foreach: ProductViewList">
<tr>
<td class="calign leftbordernone" style="width:50px">
<a data-bind="attr: { 'href': '@Url.Action("EditProduct", "Inventory")?id=' + Id}" title="Edit Product">
<img src="~/Images/edit.png" height="15" width="15" alt="" />
</a>
</td>
<td class="calign leftbordernone" style="width:50px">
<a data-bind="click: function () { DeleteProduct(Id); }" href="" title="Delete Product">
<img src="~/Images/delete.png" height="15" width="15" alt="" />
</a>
</td>
<td class=" lalign leftbordernone" style="width:90px" data-bind="text: ProductCode"></td>
<td class=" lalign" data-bind="text: ProductName"></td>
<td class=" lalign" style="width:70px" data-bind="text: CategoryName"></td>
<td class=" lalign" style="text-align:center;width:40px">
<input type="checkbox" disabled="disabled" class="chkbox" data-bind="checked:IsActive" />
</td>
<td class=" lalign" style="text-align:right" data-bind="text: formatPrice(SalesPrice)"></td>
<td class=" lalign" style="text-align:right" data-bind="text: QtyperUnit"></td>
<td class=" lalign" style="text-align:right" data-bind="text: LeadTime"></td>
<td class=" lalign" style="text-align:right" data-bind="text: CurrentStock"></td>
<td class=" lalign" style="text-align:right" data-bind="text: ReorderLevel"></td>
<td class=" lalign rightbordernone" style="text-align:center">
<input type="checkbox" disabled="disabled" class="chkbox" data-bind="checked: Reserved" />
</td>
</tr>
</tbody>
</table>
Upvotes: 1
Views: 3753
Reputation: 28437
I want to sort table according checkboxes
In order to do that, you need to compare the checked
property of the input
inside the columns.
In your rows.sort
method use the comparer like this:
$rows.sort(function(a, b) {
var value1= $(a).find('td').eq(idx).find("input")[0].checked,
value2 = $(b).find('td').eq(idx).find("input")[0].checked;
return (value1 > value2);
}
Where idx
is the index of the column of the header which is clicked.
Here is a complete working demo, with sorting on multiple columns (explanation in code comments)...
Snippet:
var $buttons = $('.sort'), $tab = $("#tab");
$buttons.click(function(e) {
var self = this,
$rows = $tab.find("tbody > tr"),
idx = $buttons.index(this); // index of the header which is clicked
$rows.sort(function(a, b) {
var $obj1 = $(a).find('td').eq(idx), // which cell column based on index
$obj2 = $(b).find('td').eq(idx),
value1, value2;
if ($obj1.text().length > 0) { // if the cell contains text
value1 = $obj1.text().toUpperCase(); // use the text value
value2 = $obj2.text().toUpperCase();
} else { // if the cell does not contain text
value1 = $obj1.find("input")[0].checked; // use the checked property..
value2 = $obj2.find("input")[0].checked // .. of the input
}
// check if ascending or not
if ($(self).hasClass('asc')) return (value1 > value2);
else return (value1 < value2);
});
$(this).toggleClass('asc'); // toggle class for next ascending-descending sort
// perform physical reordering
$.each($rows, function(index, row){
$tab.append(row);
});
});
table, th, td { border: 1px solid #ddd; border-collapse: collapse; }
th, td { padding: 4px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tab">
<thead>
<tr>
<th><a href="#" class="sort asc">IsReserved</a></th>
<th><a href="#" class="sort asc">Caption</a></th>
<th><a href="#" class="sort asc">Other</a></th>
</tr>
</thead>
<tr><td><input type="checkbox" /></td><td>One</td><td>1</td></tr>
<tr><td><input type="checkbox" checked /></td><td>Two</td><td>2</td></tr>
<tr><td><input type="checkbox" /></td><td>Three</td><td>3</td></tr>
<tr><td><input type="checkbox" checked /></td><td>Four</td><td>4</td></tr>
<tbody>
</tbody>
</table>
Fiddle: http://jsfiddle.net/abhitalks/m6nn4ved/1/
Upvotes: 1
Reputation: 4230
You can use jquery table sorter for this.
$(document).ready(function() {
$("#tableid").tablesorter();
});
Upvotes: 0