Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

M Angele U. GASANA
M Angele U. GASANA
5,500 Points

I seem to keep missing key points in my project 2

Hello Treehouse community, I have been working on my project for a while now but I haven't been able to get it to run properly, I seem to keep missing some stuffs... I appreciate anyone who can point me in the right direction once you get a chance.. here is a link to my repo https://github.com/Angeleu/Project-two/tree/master/js Thanks

2 Answers

  • Just copy this and replace it with your current script.js
  • Now you should have 10 students per page
  • Since there are 54 Students, there will be 6 pages, last one shows the last 4 Students
  • Pagination buttons are now added dynamically at the desired place
  • Clicking on buttons now retrieves desired page with correct students
  • I added comments where I made/added changed to your code

  • Compare my code to yours and see where you did a few mistakes

const studentsList = document.querySelector('.student-list');
console.log(studentsList);
const studentItems = studentsList.children;
const studentsPerPage = 10;

// Reference so that we can dynamically add the pagination buttons
const page = document.querySelector('.page');

const showPage = (studentItems, page) => {

   let startIndex = (page * 10) - 10;
   let endIndex = page * 10;
   for (let i = 0; i < studentItems.length; i++) {
      if ( i >= startIndex && i < endIndex ) { studentItems[i].style.display = ''; }
      else { studentItems[i].style.display = 'none' }
   }
};

const appendPageLinks = (studentItems) => {

   // Since we have 54 students and we divide studentItems.length(54) by studentsPerPage(10)
   // we have to ceil the number to get a whole number, ex.: 5.4 > 6, meaning there will be 6 pages
   let pageNumber = Math.ceil(studentItems.length/studentsPerPage);
   const div = document.createElement('div');
   div.className = 'pagination';

   // Before you where getting 'page was not defined' because you did not defined it. I went ahead and defined it above.
   page.appendChild(div);

   const ul = document.createElement('ul');
   div.appendChild(ul);
   for (let i = 1; i<= pageNumber; i++) {
      const li = document.createElement('li');
      const a = document.createElement('a');
      a.textContent = i;
      // Since we always start at page 1, might as well make the first pagination button active
      if (i === 1) { a.className = "active" }
      ul.appendChild(li);
      li.appendChild(a);

   a.addEventListener('click', (e) => {
      // We want to clear a element's class, so we want to select all the a elements in the .pagination ul li a
      let paginationLinks = document.querySelectorAll('.pagination ul li a');
      for(i=0; i < paginationLinks.length; i++) {
         paginationLinks[i].className = '';
      }
      // if we click on a element, give that element an 'active' class
      if (e.target.tagName === "A") {
         e.target.className = 'active';
         showPage(studentItems, a.textContent);
      }     
   });

   // Just a little effect to make it feel like a button
   a.addEventListener('mouseover', (e) => {
      e.target.style.cursor = 'pointer';
   });

   }
   };

showPage(studentItems,1)

appendPageLinks(studentItems);
  • If you need help with anything else, I will do my best to assist you :)
M Angele U. GASANA
M Angele U. GASANA
5,500 Points

Stivan Radev thank you very much for all your help.. I had been struggling with a couple stuffs in my code so much that I kept second guessing everything and ended up deleting even what I needed! I am so glad that you added the active class because I had had a hard time figuring out how to add it and now that you did it looks sooo easy! lol thanks again.. let me get back to it..

No problem. It was my pleasure. Good luck with your project!