Hi I am trying to retrieve all hit ids by looping ...
# community-help
p
Hi I am trying to retrieve all hit ids by looping through all pages in pagination. I am using instantsearch.js . But I am stuck. Please help. I am using Laravel and Livewire and wrote below method in javascript but i never get the results.
Copy code
const handleSelectAllCheckboxes = async () => {


    const renderState = search.renderState[indexName];
    const nbPages = renderState?.pagination?.nbPages || 1;
    const currentPage = renderState?.pagination?.currentRefinement || 0;

    let allIds = [];

    for (let page = 0; page < nbPages; page++) {
        search.helper.setPage(page);

        const results = new Promise(resolve => {
            search.helper.once('result', resolve);
            search.helper.search();
        });   // I am stuck here.

        console.log(results);

        allIds.push(...results.hits.map(hit => hit.id));
    }

    // Remove duplicates and store in `selectedIds`
    selectedIds = [...new Set(allIds)];

    // Restore original page
    search.helper.setPage(currentPage).search();

    // Show all checkboxes in current page as ticked
    document.querySelectorAll('#hits .row-checkbox').forEach(checkbox => {
        checkbox.checked = true;
    });

    // Show checkbox to select all in current page as ticked
    const selectAllCurrentPageCheckbox = document.getElementById('select-all-checkboxes-in-current-page');
    if (selectAllCurrentPageCheckbox) {
        selectAllCurrentPageCheckbox.checked = true;
        selectAllCurrentPageCheckbox.indeterminate = false;
    }

    // Sync with Livewire
    $wire.$set('checked', selectedIds);
    console.log('Selected IDs:', selectedIds);
    console.log('Total selected:', selectedIds.length);
};