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
alborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsNeed help loading first 10 elements on page when page loads
I'm not sure how to hide all but the first 10 elements on this page when the page first loads. I have a list of 54 students; I want to load the first 10 when the page loads. The first 10 students load on the page that has the 1st pagination button.
Included is a snippet from the HTML and JS.
<ul class="student-list">
<li class="student-item cf">
<div class="student-details">
<img class="avatar" src="https://randomuser.me/api/portraits/thumb/women/67.jpg">
<h3>iboya vat</h3>
<span class="email">iboya.vat@example.com</span>
</div>
<div class="joined-details">
<span class="date">Joined 07/15/15</span>
</div>
</li>
</ul>
console.log("hi!");
// DOM list of divs containing each student detail
var studentDetails = document.querySelectorAll('.student-details');
console.log(studentDetails);
var students = [];
for (var i = 0; i < studentDetails.length; i++) {
// Single div containing a student detail
var studentDetail = studentDetails[i]; // div
// storing the names and emails for search
var studentEmail = studentDetail.getElementsByTagName('span')[0].innerText;
var studentName = studentDetail.getElementsByTagName('h3')[0].innerText;
// creating a new student object for search
var student = {
name: studentName,
email: studentEmail
};
students.push(student);
}
console.log(students);
// Search
var searchHTML = '<input placeholder="Search for students...">' + ' <button>Search</button>';
document.getElementById("student-search").innerHTML = searchHTML;
// pagination functionality
var studentsPerPage = 10 // Number of students per page to render
var allStudents = document.querySelectorAll('.student-item');
var numberOfPages = Math.ceil(allStudents.length / studentsPerPage);
console.log(numberOfPages);
// HTML for pagination
var paginationHTML = '<ul>';
for (var i = 0; i < numberOfPages; i++) {
paginationHTML += '<li onclick="onPageClick(' + (i + 1) + ')"><a class="pagination">' + (i + 1) + '</a></li>\n';
}
paginationHTML += '</ul>';
document.getElementById("pagination").innerHTML = paginationHTML;
// create for loop adding order of pagination classes to each student
var currentPageNum = 0;
for (var i = 0; i < allStudents.length; i++) {
var student = allStudents[i];
console.log(student);
// every 10 iterations of loop bump up currentPageNum by 1
if (i % 10 === 0) {
currentPageNum += 1;
}
student.className += " page-" + currentPageNum;
}
function onPageClick(pageNum) { // When we click on a pagination link, do this
$('.student-item').hide(); // hide all of the students here using jquery
console.log(pageNum);
// if user clicks on a pagination link with a certain class, show students with a certain class
$('.page-' + pageNum).show();
}
console.log(paginationHTML);
$(document).ready(function() {
// on page load, show only the first 10 students
console.log("ready!");
});
Thanks!
1 Answer
Chris Mason
Full Stack JavaScript Techdegree Graduate 24,526 PointsHi Alborz, you may want to look at the :nth-of-type() Selector in jQuery. See: https://api.jquery.com/nth-of-type-selector/
This page helps to explain the concept in a clear way: http://nthmaster.com/
Hope that helps, good luck!
Chris Mason
Full Stack JavaScript Techdegree Graduate 24,526 PointsChris Mason
Full Stack JavaScript Techdegree Graduate 24,526 PointsAlso this video explaining the concept in CSS is useful: https://teamtreehouse.com/library/css-selectors/advanced-selectors/nthchild
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Pointsalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsThanks!