Display Search Results
In the last lesson, we made it so that we could search for a term and get the data and log it. Now we will display the results on the page. Let's add to the search() function.
async function search() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
// Add the type and term to the global object
global.search.type = urlParams.get('type');
global.search.term = urlParams.get('search-term');
if (global.search.term !== '' && global.search.term !== null) {
const { results, total_pages, page } = await searchAPIData();
if (results.length === 0) {
showAlert('No results found');
return;
}
displaySearchResults(results);
document.querySelector('#search-term').value = '';
} else {
showAlert('Please enter a search term');
}
}
Now we are destructuring the results from the searchAPIData() function. We are also getting the total_pages and page from the API. We will use these to display the pagination. For now, we only need the results array. We will pass that into the displaySearchResults() function. Let's create that function now.
function displaySearchResults(results) {
results.forEach((result) => {
const div = document.createElement('div');
div.classList.add('card');
div.innerHTML = `
<a href="${global.search.type}-details.html?id=${result.id}">
${
result.poster_path
? `<img
src="https://image.tmdb.org/t/p/w500/${result.poster_path}"
class="card-img-top"
alt="${
global.search.type === 'movie' ? result.title : result.name
}"
/>`
: `<img
src="../images/no-image.jpg"
class="card-img-top"
alt="${
global.search.type === 'movie' ? result.title : result.name
}"
/>`
}
</a>
<div class="card-body">
<h5 class="card-title">${
global.search.type === 'movie' ? result.title : result.name
}</h5>
<p class="card-text">
<small class="text-muted">Release: ${
global.search.type === 'movie'
? result.release_date
: result.first_air_date
}</small>
</p>
</div>
`;
document.querySelector('#search-results').appendChild(div);
});
}
Now we are showing the results on the page in cards. You can remove any hardcoded data from the HTML and CSS. In the next lesson, we will implement the pagination.