14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 ■ <strong>JavaScript</strong> <strong>and</strong> User Interaction: Navigation <strong>and</strong> Forms<br />

pagination.js (excerpt)<br />

if(t == table.topNext || t == table.bottomNext){<br />

start = table.current + pn.increase;<br />

} else if (t == table.topPrev || t == table.bottomPrev){<br />

start = table.current - pn.increase;<br />

}<br />

pn.showSection(table, start);<br />

},<br />

The showSection() method calls the changePaginationNav() method to update the links <strong>and</strong> the counter <strong>and</strong><br />

tests whether there is already a current parameter on the table. If there is one, this means data rows exist that need to<br />

be removed. You get rid of them by looping through the section of the data rows stored in the datarows property <strong>and</strong><br />

removing the CSS class defined in showClass().<br />

pagination.js (excerpt)<br />

showSection : function(table, start){<br />

var i;<br />

pn.changePaginationNav(table, start);<br />

if(table.current != null){<br />

for(i=table.current; i < table.current+pn.increase; i++){<br />

if(table.datarows[i]) {<br />

<strong>DOM</strong>help.cssjs('remove', table.datarows[i], pn.showClass);<br />

}<br />

}<br />

}<br />

You then loop from start to start plus the predefined increase, <strong>and</strong> you add the CSS class to show these rows<br />

in the table. Notice that you need to test whether these rows exist; otherwise, you might try to reach rows that aren’t<br />

there on the last page. (Imagine a list of 22 elements; clicking the “next” link on the 16–20 page would try to show<br />

elements 21 to 25.) To make sure the right slice gets shown the next time the method gets called, all that is left is to<br />

define the current property as the start value.<br />

pagination.js (excerpt)<br />

for(i = start; i < start + pn.increase; i++){<br />

if(table.datarows[i]) {<br />

<strong>DOM</strong>help.cssjs('add', table.datarows[i], pn.showClass);<br />

}<br />

}<br />

table.current = start;<br />

},<br />

As hinted earlier, the changePaginationNav() method renders the “previous” link on the first page <strong>and</strong> the “next”<br />

link on the last page inactive. The trick to making links appear but not clickable is to remove the href attribute.<br />

On the first page, the start value minus the predefined increase would result in a negative number, which is easy<br />

to test. When the number is larger than 0, you add the href attribute again.<br />

www.it-ebooks.info<br />

223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!