This JavaScript function may be used to create the link groups for search results or for a view. For example:
Previous | 1 | 2 | 3 | Next
Code
Input parameters:
total - the total number of documents in the view or search result
format - the format of the generated links
0 (or omitted) generates page numbers, ex. Previous | 1 | 2 | 3 | Next
1 generates groups, ex.. Previous | 1 - 4 | 5 - 8 | 9 - 10 | Next
when you call the original view the URL should include two parameters:
&count will be used to calculate the intervals
&start must be the last parameter in the URL
defaults &count=30&start=1
place call to this function on the $$...template at the spot where you want the
links to appear
set the total passed to this function to TotalHits or
use @DBLookup to set it to the number of documents in the view
Place it on any $$viewtemplate or $$searchtemplate and pass the total to it.
/* ----------------------------------------------
------------------------------
return middle of the string
example:
middle('&count=25&start=1&',
'&count=', '&')
will return:
'25'
------------------------------------------------
-------------------------------*/
function middle(str, ina, inb){ // return
middle of the string
//alert('middle function');
var startix = str.indexOf(ina);
var endix = str.indexOf(inb,
startix + 1);
var newStr = '';
if (startix >= 0) {
if (endix < 0) {endix = str.length;}
newStr = str.substring(startix +
ina.length, endix);
} else {newStr = ''; }
return newStr;
} // end of function middle()
/* --------------------------------------------
---------------------------------
Generate links in groups
place this script on a $$viewtemplate
or $$searchtemplate
Input parameters:
total - the total number of documents
in the view or search result
format - the format of the generated links
0 (or omitted) generates page
numbers, ex. Previous | 1 | 2 | 3 | Next
1 generates groups, ex..
Previous | 1 - 4 | 5 - 8 | 9 - 10 | Next
when you call the original view the
URL should include two parameters:
&count will be used to calculate the
intervals
&start must be the last parameter
in the URL
defaults &count=30&start=1
place call to this function on
the $$...template at the spot where
you want the links to appear
set the total passed to this function
to TotalHits or
use @dbLookup to set it to the n
umber of documents in the view
-----------------------------------------------
-------------------------------*/
function generateLinks(total, format)
{ // generate a range of links
//alert('generateLinks, total = ' + total);
function makeLink(curStart, linkText)
{ // create the <a> tag (internal
function)
var tmpLink = '';
tmpLink = '<a '+ style + ' href="'
+ shortLoc + '&start=' + curStart + '" >' ;
tmpLink = tmpLink + linkText + '</a>';
return(tmpLink );
} // end of internal function makeLink
var loc = String(window.location.href);
//alert('location=' + loc);
loc = loc.toLowerCase();
//alert('location.lowercase=' + loc);
var nextLink = 'Next';
var prevLink = 'Previous';
var style = '';
var links = '';
var ix = loc.indexOf('&start=');
// &start= must be the last parameter
//alert('ix=' + ix);
var shortLoc = loc;
if (ix < 0) { start = 1; }
else {
shortLoc = loc.substring(0,ix);
start = parseInt(middle(loc, '&start=',
'&'),10);
}
ix = loc.indexOf('&count=');
if (ix < 0) {
count = 30 ;
shortLoc = shortLoc + '&count=' +
count;
}
else {
count = parseInt(middle(loc,
'&count=', '&'),10);
}
nextStart = start + count;
prevStart = start - count;
if (nextStart < total) { nextLink =
makeLink(nextStart, nextLink); }
if (prevStart > 0) { prevLink =
makeLink(prevStart, prevLink); }
ix = total/count;
if (total % count == 0) {ix = ix - 1};
// set number of links
for (var j = 0; j <= ix; j++) { // create
all the links
thisStart = j*count + 1;
thisEnd = thisStart + count - 1;
if (thisEnd > total) {thisEnd = total }
if (thisStart == start) {style = 'style=
"font-weight: bold" '; }
else { style = ''}
if (format) {thisText = '' + thisStart + ' -
' + thisEnd; }
else {thisText = String(j + 1); }
links = links + ' | ' + makeLink(thisStart,
thisText);
}
document.writeln(prevLink + links +
' | ' + nextLink);
} // end of function generateLinks()
Do you have comments on this tip? Let us know.