Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination for search page #23

Open
exaline-ru opened this issue Jan 22, 2022 · 1 comment
Open

Pagination for search page #23

exaline-ru opened this issue Jan 22, 2022 · 1 comment

Comments

@exaline-ru
Copy link

As the search results is a very long list I'd like to use pagination, but this is not a collection. Is there a method for paging the results of a search?

@jevgenijs-jefimovs
Copy link

jevgenijs-jefimovs commented Jan 5, 2024

@exaline-ru

Add the "search-paging-wrapper" to search.njk:

...
<div id="info" class="text-center mb-5 mx-auto text-lg text-slate-600"></div>
<div id="wrapper" class="flex flex-wrap -mx-2"></div>
<div id="search-paging-wrapper" class="mt-3 flow-root"></div>

Add the "const pagingWrapperEl..." to search.js:

  render(query) {
    const wrapperEl = document.getElementById('wrapper');
    const searchBoxEl = document.getElementById('searchbox');
    const infoEl = document.getElementById('info');
    const pagingWrapperEl = document.getElementById('search-paging-wrapper');

Clear the content of pagingWrapperEl:

    searchBoxEl.value = query;
    wrapperEl.innerHTML = '';
    pagingWrapperEl.innerHTML = '';

Add the Previous and Next buttons to pagingWrapperEl:

    slicedPosts.forEach((post) => {
    ...
    });

    if (offset > 0) {
        const newStart = offset - size + 1 > 0 ? offset - size + 1 : 1;
        const newUrl = `?q=${query}&start=${newStart}&size=${size}`;
        pagingWrapperEl.innerHTML += `<a href="` + newUrl + `" class="float-left bg-white font-semibold py-2 px-4 border rounded shadow-md text-slate-800 cursor-pointer hover:bg-slate-100">Previous</a>`;
    } else {
        pagingWrapperEl.innerHTML += `<a href="javascript:void(0)" class="float-left bg-white font-semibold py-2 px-4 border rounded shadow-md text-slate-800 cursor-default text-opacity-50">Previous</a>`;
    }

    if (lastPostIndex < matchedPosts.length) {
        const newStart = offset + size + 1;
        const newUrl = `?q=${query}&start=${newStart}&size=${size}`;
        pagingWrapperEl.innerHTML += `<a href="` + newUrl + `" class="float-right bg-white font-semibold py-2 px-4 border rounded shadow-md text-slate-800 cursor-pointer hover:bg-slate-100">Next</a>`;
    } else {
        pagingWrapperEl.innerHTML += `<a href="javascript:void(0)" class="float-right bg-white font-semibold py-2 px-4 border rounded shadow-md text-slate-800 cursor-default text-opacity-50">Next</a>`;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants