Hello,
I am making pagination and have a function goToPage(pagenum).
I'm curious if anyone knows what algorithm the gt and lt selectors use because I want to know if using them is not just more elegant code but also better. Compare the two snippets:
The HTML looks something like this:
<div id="row_wrapper_0" class="search_result_row_wrapper">(ROW CONTENTS)</div>
<div id="row_wrapper_1" class="search_result_row_wrapper">(ROW CONTENTS)</div>
<div id="row_wrapper_2" class="search_result_row_wrapper">(ROW CONTENTS)</div>
...
<div id="row_wrapper_99" class="search_result_row_wrapper">(ROW CONTENTS)</div>
My (old) way loops through all DIVs and hides and shows them selectively:
for (var i = 0; i < sr_numResults; i++) {
var rangeLow = ((page - 1) * sr_resultsPerPage);
if (rangeLow <= i && i <= (rangeLow + (sr_resultsPerPage - 1)))
$("#row_wrapper_" + i).show();
else
$("#row_wrapper_" + i).hide();
}
The new way is to use gt and lt selectors like so:
if (page == 1) {
$(".row_wrapper:gt(" + sr_resultsPerPage * page + ")").hide();
$(".row_wrapper:lt(" + sr_resultsPerPage * page + ")").show();
}
else {
$(".row_wrapper:gt(" + (sr_resultsPerPage * page) + ")").hide();
$(".row_wrapper:lt(" + ((sr_resultsPerPage * (page - 1)) - 1) + ")").hide();
$(".row_wrapper:lt(" + (sr_resultsPerPage * page) + "):gt(" + ((sr_resultsPerPage * (page - 1)) - 1) + ")").show();
}
Any insight on this is appreciated.
Thanks,
Dan