I pre-load a table with all of its rows. However, I only want to show only the top 10 rows that are within the
You can use the selector You need to save the current displayed index in a separate variable, or calculate the current index based on how many elements are selected (use the Check the current count of elements displayed and show/hide the corresponding buttons. I created a jsFiddle demo to see this in action. tag and now every
<
$("#internalActivities tr")
which will select all 's, regardless of a or not
.length
property)Example
HTML
Javascript
for (var i=0;i<21;i++) {
$('#internalActivities').append('
');
}
var trs = $("#internalActivities tr");
var btnMore = $("#seeMoreRecords");
var btnLess = $("#seeLessRecords");
var trsLength = trs.length;
var currentIndex = 10;
trs.hide();
trs.slice(0, 10).show();
checkButton();
btnMore.click(function (e) {
e.preventDefault();
$("#internalActivities tr").slice(currentIndex, currentIndex + 10).show();
currentIndex += 10;
checkButton();
});
btnLess.click(function (e) {
e.preventDefault();
$("#internalActivities tr").slice(currentIndex - 10, currentIndex).hide();
currentIndex -= 10;
checkButton();
});
function checkButton() {
var currentLength = $("#internalActivities tr:visible").length;
if (currentLength >= trsLength) {
btnMore.hide();
} else {
btnMore.show();
}
if (trsLength > 10 && currentLength > 10) {
btnLess.show();
} else {
btnLess.hide();
}
}
my data