Create Search Bar to Filter Dynamic Content

Hi there, I’m trying to implement a search bar function on my website where the user types in what they are looking for from within the relevant dataset - as opposed to the entire site - and it displays the relevant option. Ideally I’d like it to search “on input” (I believe that’s the correct term - I’m a newbie!), so as they type it filters and displays the content. I’d also like it to filter tags (I think that’s the correct term?) so related things with the same tag also appear.

I’ve tried many different code tutorials but none of them have worked - unsure if they’re out of date perhaps?

I believe I have Wix Editor and Dev Mode.

Hoping someone can help me! Many thanks, Ellie :slight_smile:

Are you able to share the code you have at the moment/have already tried?

It’s easier to build upon, and will help you understand it better than providing everything in one go :slight_smile:

Hi there, thanks for the reply! Here is the latest code I’ve tried:

import wixData from ‘wix-data’;

$w.onReady(() => {

// Real-time search as the user types in the search bar.

$w(“#title”).onInput(() => {

performSearch();

});

});

function performSearch() {

const searchQuery = $w(“#SearchbarHome”).value.trim();

if (searchQuery.length > 0) {

// Apply the filter based on the search query

$w(“#title”).setFilter(

wixData.filter()

.contains(“title”, searchQuery)

.or(wixData.filter().contains(“arraystring”, searchQuery))

)

.then(() => {

console.log(“Dataset filtered”);

// Automatically expand the repeater if there are items to display

updateRepeaterVisibility();

})

.catch((err) => {

console.error(“Failed to filter dataset:”, err);

$w(“#repeaterSearch”).collapse();

// Show the notFound element if there’s an error

$w(“#notFound”).show();

});

} else {

// Clear the filter and collapse the repeater if search query is empty

$w(“#title”).setFilter(wixData.filter())

.then(() => {

console.log(“Filter cleared”);

$w(“#repeaterSearch”).collapse();

// Hide the notFound element when the filter is cleared

$w(“#notFound”).hide();

})

.catch((err) => {

console.error(“Failed to clear filter:”, err);

$w(“#repeaterSearch”).collapse();

// Optionally, hide the notFound element if there’s an error

$w(“#notFound”).hide();

});

}

}

function updateRepeaterVisibility() {

// Check if the dataset has items to display after filtering

$w(“#title”).getItems(0, 1).then((results) => {

if (results.totalCount > 0) {

$w(“#repeaterSearch”).expand();

// Hide the notFound element when there are items to display

$w(“#notFound”).hide();

} else {

$w(“#repeaterSearch”).collapse();

// Show the notFound element when there are no items to display

$w(“#notFound”).show();

}

});

}

just adding your code @user5829 in the preformatted text so its easier to read

import wixData from ‘wix-data’;

$w.onReady(() => {

// Real-time search as the user types in the search bar.

$w(“#title”).onInput(() => {

performSearch();

});

});

function performSearch() {

const searchQuery = $w(“#SearchbarHome”).value.trim();

if (searchQuery.length > 0) {

// Apply the filter based on the search query

$w(“#title”).setFilter(

wixData.filter()

.contains(“title”, searchQuery)

.or(wixData.filter().contains(“arraystring”, searchQuery))

)

.then(() => {

console.log(“Dataset filtered”);

// Automatically expand the repeater if there are items to display

updateRepeaterVisibility();

})

.catch((err) => {

console.error(“Failed to filter dataset:”, err);

$w(“#repeaterSearch”).collapse();

// Show the notFound element if there’s an error

$w(“#notFound”).show();

});

} else {

// Clear the filter and collapse the repeater if search query is empty

$w(“#title”).setFilter(wixData.filter())

.then(() => {

console.log(“Filter cleared”);

$w(“#repeaterSearch”).collapse();

// Hide the notFound element when the filter is cleared

$w(“#notFound”).hide();

})

.catch((err) => {

console.error(“Failed to clear filter:”, err);

$w(“#repeaterSearch”).collapse();

// Optionally, hide the notFound element if there’s an error

$w(“#notFound”).hide();

});

}

}

function updateRepeaterVisibility() {

// Check if the dataset has items to display after filtering

$w(“#title”).getItems(0, 1).then((results) => {

if (results.totalCount > 0) {

$w(“#repeaterSearch”).expand();

// Hide the notFound element when there are items to display

$w(“#notFound”).hide();

} else {

$w(“#repeaterSearch”).collapse();

// Show the notFound element when there are no items to display

$w(“#notFound”).show();

}

});

}
2 Likes

Check out this tutorial by @thewixwiz which will help you get a hang of it:

4 Likes