Pagination Code
// Build Pagination var pagination = paginate (tmpData.ItemSet.pagenum, tmpData.ItemSet.TotalResults, tmpData.ItemSet.category, tmpData.ItemSet.sort, tmpData.ItemSet.keywords); sa_listTop.appendChild(pagination);
Pagination Function Code
// Pagination function function paginate (cur, tres, cat, srt, key) { cur = Number(cur); tres = Number(tres); var buffer = ""; var tot = Math.ceil(tres / 10); var low = Math.max(1, (cur - 2)); if (cur { var high = Math.min((cur + (5 - cur)), tot); } else if (cur > 3) { var high = Math.min(tot, (cur + 2)); } var pagination = cel('div'); var paginationul = cel('ul'); var pageli1 = cel('li'); var pageli2 = cel('li'); var pageli3 = []; var pageli4 = cel('li'); var pageli5 = cel('li'); var pageanchor1 = cel('a'); var pageanchor2 = cel('a'); var pageanchor3 = []; var pageanchor4 = cel('a'); var pageanchor5 = cel('a'); var clearing = cel('div'); var pageright = cel('div'); pagination.setAttribute("id", "pagination"); if (cur == 1) { pageanchor1.setAttribute('href', 'javascript:void(0);'); pageanchor1.setAttribute('className', 'off'); pageanchor1.appendChild(ctn('First')); pageli1.appendChild(pageanchor1); paginationul.appendChild(pageli1); pageanchor2.setAttribute('href', 'javascript:void(0);'); pageanchor2.setAttribute('className', 'off'); pageanchor2.appendChild(ctn('Prev')); pageli2.appendChild(pageanchor2); paginationul.appendChild(pageli2); } else if (cur > 1) { pageanchor1.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + "1');"); pageanchor1.appendChild(ctn('First')); pageli1.appendChild(pageanchor1); paginationul.appendChild(pageli1); pageanchor2.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + (cur-1) + "');"); pageanchor2.appendChild(ctn('Prev')); pageli2.appendChild(pageanchor2); paginationul.appendChild(pageli2); } for (i = low; i { pageli3[i] = cel('li'); pageanchor3[i] = cel('a'); if (i == cur) { pageli3[i].setAttribute('className', 'selected'); pageanchor3[i].appendChild(ctn(i)); pageanchor3[i].setAttribute('href', 'javascript:void(0);'); pageanchor3[i].setAttribute('className', 'off'); pageli3[i].appendChild(pageanchor3[i]); paginationul.appendChild(pageli3[i]); } else { pageanchor3[i].appendChild(ctn(i)); pageanchor3[i].setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + i + "');"); pageli3[i].appendChild(pageanchor3[i]); paginationul.appendChild(pageli3[i]); } } if ((tot > 1) && (cur < tot)) { pageanchor4.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + (cur + 1) + "');"); pageanchor4.appendChild(ctn('Next')); pageli4.appendChild(pageanchor4); paginationul.appendChild(pageli4); } else { pageanchor5.setAttribute('href', 'javascript:void(0);'); pageanchor5.setAttribute('className', 'off'); pageanchor5.appendChild(ctn('Next')); pageli5.appendChild(pageanchor5); paginationul.appendChild(pageli5); } pagination.appendChild(paginationul); clearing.setAttribute('className', 'clearing'); clearing.appendChild(ctn(' ')); pageright.setAttribute('className', 'paginationRight'); pagination.appendChild(clearing); pagination.appendChild(pageright); return pagination; }
Due to its length and complexity, I isolated the pagination section of the callback function in a separate function: paginate. The paginate function takes five arguments: cur, tres, cat, srt, key. Respectively, current page, total results, category (SearchIndex), Sort and Keyword(s). All of these arguments are values in the JSON object response. We will need the first two to calculate forward and backward page movement and the last three are required for the search function.
All the paginate function really does is resubmit the original request to amazon with one parameter change in the page number. The current page and total results are used to calculate a five page navigation range relative to the current page; links with function calls to the search function are then generated and appended to a <UL> element in a <DIV> element. The resulting <DIV> element is then returned to the callback function and appended to the result list container.