Search field on dataset with filters set

I have a keyword search that is searching over three fields in a table. The search function is working as expected and displays the results in the table, however it is cancelling out the filters I have set on the dataset (settings). When I delete the text from the search box, the table is then showing all results.

How can I have a search field and still maintain the dataset filters? Do I need to include these filters in my code?

The filters:

The code:

import wixData from 'wix-data';
    $w.onReady(function (){
});

let debounceTimer;
export function keywordSearch_keyPress(event) {
    if (debounceTimer) {
        clearTimeout(debounceTimer);
        debounceTimer = undefined;
    }
        debounceTimer = setTimeout(() => {
        filter($w('#keywordSearch').value);
        }, 200);
}

function filter(title, jobContact, postCode) {
        $w('#dataset1').setFilter(wixData.filter()
            .contains('title', $w('#keywordSearch').value)
                .or(wixData.filter()
                .contains('jobContact', $w('#keywordSearch').value))
                    .or(wixData.filter()
                    .contains('postCode', $w('#keywordSearch').value))
                )
}

I’m new to coding, so any help would be greatly appreciated!

Thanks

Instead of setting the filter in the dataset settings, you could put those filtering conditions in a separate function and invoke it when you need it. You would call that function in the onReady and when the user clears the search textbox, testing for the length of the textbox value being 0 in the keyPress event.

Thanks @tony-brunsman

I tried your suggestion of setting the dataset filters in a function, but the search kept reverting back to all items in the collection as soon as I started typing in the search field. So, I tried a different approach and loaded my dataset filters into the search function - this seems to be working nicely! I can’t believe I spent so many hours trying to fix this yesterday - sometimes you just need to sleep on it :slight_smile:

function filter(title, jobContact, postCode, availability, removePost, resubmit, category) {
$w('#dataset1').setFilter(wixData.filter()
    .contains('title', $w('#keywordSearch').value)
       .or(wixData.filter()
       .contains('jobContact', $w('#keywordSearch').value))
          .or(wixData.filter()
          .contains('postCode', $w('#keywordSearch').value))
            .and(wixData.filter()
            .eq('availability', true))
               .not(wixData.filter()
               .eq('removePost', true))
                  .not(wixData.filter()
                  .eq('resubmit', true))
                     .and(wixData.filter()
                     .eq('category', 'ffc8531a-5bcf-41ce-b208-   077022786dd4')
         )
      )
}