Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to integrate "items per page" and "pagination"
    primarykey
    data
    text
    <p>I've written the following codes. The idea is to pass the selected value of "Reports per page" into javascript pagination functions to display the pages accordingly.</p> <p>Problem: value seemingly passed correctly but pages not displaying properly. E.g. value for "reports per page" = 2. But instead of displaying 2 reports per page, all/total reports were displayed and 2 reports were deducted when "NEXT" is clicked each time / moving from page 1 to 2, etc.</p> <p>Can anyone advise what is wrong with my codes? Thanks!</p> <pre><code> &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;label id="lDisplayPerPg" for="lDisplayPerPg"&gt;Reports per page&lt;/label&gt; &lt;select name="listDisplayPerPg" id="listDisplayPerPg"&gt; &lt;option value="2" selected="selected"&gt;2&lt;/option&gt; &lt;option value="4"&gt;4&lt;/option&gt; &lt;option value="6"&gt;6&lt;/option&gt; &lt;option value="8"&gt;8&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;form method="post" action=""&gt; &lt;table id="tadminViewReport" border="0" width="960px" cellpadding="0" cellspacing="0"&gt; &lt;tr&gt; &lt;th width="15%"&gt;Username&lt;/th&gt; &lt;th width="15%"&gt;Report ID&lt;/th&gt; &lt;th width="40%"&gt;Report Title&lt;/th&gt; &lt;th width="20%"&gt;Date submitted&lt;/th&gt; &lt;th width="10%"&gt;Status&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username1&lt;/td&gt; &lt;td&gt;reportID1&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 1&lt;/td&gt; &lt;td&gt;Date 1&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Received&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username2&lt;/td&gt; &lt;td&gt;reportID2&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 2&lt;/td&gt; &lt;td&gt;Date 2&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;In Queue&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username3&lt;/td&gt; &lt;td&gt;reportID3&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 3&lt;/td&gt; &lt;td&gt;Date 3&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Completed&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username4&lt;/td&gt; &lt;td&gt;reportID4&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 4&lt;/td&gt; &lt;td&gt;Date 4&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;In Queue&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username5&lt;/td&gt; &lt;td&gt;reportID5&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 5&lt;/td&gt; &lt;td&gt;Date 5&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;In Queue&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username6&lt;/td&gt; &lt;td&gt;reportID6&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 6&lt;/td&gt; &lt;td&gt;Date 6&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Completed&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username7&lt;/td&gt; &lt;td&gt;reportID7&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 7&lt;/td&gt; &lt;td&gt;Date 7&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Completed&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username8&lt;/td&gt; &lt;td&gt;reportID8&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 8&lt;/td&gt; &lt;td&gt;Date 8&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Received&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username9&lt;/td&gt; &lt;td&gt;reportID9&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 9&lt;/td&gt; &lt;td&gt;Date 9&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Received&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;username10&lt;/td&gt; &lt;td&gt;reportID10&lt;/td&gt; &lt;td class="reportDoc"&gt;Document 10&lt;/td&gt; &lt;td&gt;Date 10&lt;/td&gt; &lt;td&gt;&lt;a href="admin_report_details.html"&gt;Received&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div id="pageNavPosition"&gt;&lt;/div&gt; &lt;br /&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt;&lt;!-- reportsPerPage = listDisplayPerPg.options[listDisplayPerPg.selectedIndex].value; var pager = new Pager('tadminViewReport', reportsPerPage); pager.init(); pager.showPageNav('pager', 'pageNavPosition'); pager.showPage(1); //--&gt;&lt;/script&gt; function Pager(tableName, itemsPerPage) { //function Pager(tableName, itemsPerPage) { this.tableName = tableName; // this.itemsPerPage = itemsPerPage; this.currentPage = 1; this.pages = 0; this.inited = false; this.showRecords = function(from, to) { var rows = document.getElementById(tableName).rows; // i starts from 1 to skip table header row for (var i = 1; i &lt; rows.length; i++) { if (i &lt; from || i &gt; to) rows[i].style.display = 'none'; else rows[i].style.display = ''; } } this.showPage = function(pageNumber) { if (! this.inited) { alert("not inited"); return; } var oldPageAnchor = document.getElementById('pg'+this.currentPage); oldPageAnchor.className = 'pg-normal'; this.currentPage = pageNumber; var newPageAnchor = document.getElementById('pg'+this.currentPage); newPageAnchor.className = 'pg-selected'; var from = (pageNumber - 1) * itemsPerPage + 1; var to = from + itemsPerPage - 1; this.showRecords(from, to); var pgNext = document.getElementById(this.pagerName + 'pgNext'); var pgPrev = document.getElementById(this.pagerName + 'pgPrev'); if (pgNext != null) { if (this.currentPage == this.pages) pgNext.style.display = 'none'; else pgNext.style.display = ''; } if (pgPrev != null) { if (this.currentPage == 1) pgPrev.style.display = 'none'; else pgPrev.style.display = ''; } } this.prev = function() { if (this.currentPage &gt; 1) this.showPage(this.currentPage - 1); } this.next = function() { if (this.currentPage &lt; this.pages) { this.showPage(this.currentPage + 1); } } this.init = function() { var rows = document.getElementById(tableName).rows; var records = (rows.length - 1); this.pages = Math.ceil(records / itemsPerPage); this.inited = true; } this.showPageNav = function(pagerName, positionId) { if (! this.inited) { alert("not inited"); return; } var element = document.getElementById(positionId); var pagerHtml = '&lt;span id="' + pagerName + 'pgPrev" onclick="' + pagerName + '.prev();" class="pg-normal"&gt; &amp;#171 Prev &lt;/span&gt; &amp;nbsp; '; for (var page = 1; page &lt;= this.pages; page++) pagerHtml += '&lt;span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');"&gt;' + page + '&lt;/span&gt; &amp;nbsp; '; pagerHtml += '&lt;span id="' + pagerName + 'pgNext" onclick="'+ pagerName+'.next();" class="pg-normal"&gt; Next &amp;#187;&lt;/span&gt;'; element.innerHTML = pagerHtml; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload