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 />

Apply the dynamic class to the table <strong>and</strong> thus hide all the table rows. Call the createPaginationNav() method<br />

<strong>with</strong> a reference to the current table as a parameter to add the “previous” <strong>and</strong> “next” links, <strong>and</strong> call showSection()<br />

<strong>with</strong> the table reference <strong>and</strong> 0 as parameters to show the first result set.<br />

pagination.js (excerpt)<br />

<strong>DOM</strong>help.cssjs('add', ts[i], pn.dynamicClass);<br />

pn.createPaginationNav(ts[i]);<br />

pn.showSection(ts[i], 0);<br />

}<br />

},<br />

The createPaginationNav() method does not contain any surprises; all it does is create the links <strong>and</strong> the<br />

counter <strong>and</strong> add the event h<strong>and</strong>lers pointing to the navigate() method. You start by creating a new paragraph<br />

element <strong>and</strong> adding the pagination menu class to it.<br />

pagination.js (excerpt)<br />

createPaginationNav : function(table){<br />

var navBefore, navAfter;<br />

navBefore = document.createElement('p');<br />

<strong>DOM</strong>help.cssjs('add', navBefore, pn.paginationMenuClass);<br />

Add a new link to the paragraph <strong>with</strong> the previousLabel property value as the text content <strong>and</strong> a new SPAN<br />

element that will display the number of the current result set in between the “previous” <strong>and</strong> “next” links. You preset<br />

the counter <strong>with</strong> 1 as the start value, the number of elements shown on each page as defined in pn.increase as the<br />

end value, <strong>and</strong> the number of all data rows as the total number. The last element to add to the new paragraph is the<br />

“next” link. You add the new paragraph immediately before the table via parentNode <strong>and</strong> insertBefore().<br />

pagination.js (excerpt)<br />

navBefore.appendChild(<strong>DOM</strong>help.createLink('#', pn.previousLabel));<br />

navBefore.appendChild(document.createElement('span'));<br />

counter=pn.counter.replace('_x_', 1);<br />

counter=counter.replace('_y_', pn.increase);<br />

counter=counter.replace('_z_', table.datarowsize-1);<br />

navBefore.getElementsByTagName('span')[0].innerHTML = counter;<br />

navBefore.appendChild(<strong>DOM</strong>help.createLink('#', pn.nextLabel));<br />

table.parentNode.insertBefore(navBefore, table);<br />

It would be good to show the same menu below the table as well. Instead of re-creating all these elements<br />

once more, it is enough to clone the paragraph <strong>and</strong> insert it after the table via parentNode, insertBefore(), <strong>and</strong><br />

nextSibling. Then store the “previous” <strong>and</strong> “next” links of each paragraph as their own table properties to make it<br />

easier to change them in other methods.<br />

pagination.js (excerpt)<br />

navAfter = navBefore.cloneNode(true);<br />

table.parentNode.insertBefore(navAfter, table.nextSibling);<br />

table.topPrev = navBefore.getElementsByTagName('a')[0];<br />

table.topNext = navBefore.getElementsByTagName('a')[1];<br />

table.bottomPrev = navAfter.getElementsByTagName('a')[0];<br />

table.bottomNext = navAfter.getElementsByTagName('a')[1];<br />

www.it-ebooks.info<br />

221

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

Saved successfully!

Ooh no, something went wrong!