I have a testimonials page on our site that displays information about our client’s testimony in a repeater connected to a dataset. I would like to add a button (Show Our Reply) that shows up on the specific repeater item based on that items data in the “reply” field in the dataset. Then once that button is clicked, to show that items respective reply. I would also like to hide the button from other items that do not have anything in the “reply” field in the dataset.
I have the below code but each item still shows the button even when that specific items “reply” field is empty. In the future, I also want to add a way to filter the testimonies based on Rating, and display a number of total ratings with an average star rating.
-
#container2 = Testimony Repeater
-
#item205 = “Show Our Reply” Button
-
#item206 = “Reply Data”
import wixData from ‘wix-data’ ;
var myDATABASE = “GenericTestimonials” //–> your collection-name here…
var searchFIELD = “Rating” //–> Search-Field-ID here…
$w . onReady ( function () {
$w ( “#container2” ). onViewportEnter (( event ) => {
// get repeated item scope
const $item = $w . at ( event . context );
// get the ID of the repeated item which fired an event
const itemId = event . context . itemId ;
// get all repeater’s data, it’s stored as an array of objects
const data = $w ( “#repeater1” ). data ;
// use the array methods to find the current itemData and index
const itemData = data . find (( item ) => item . _id === itemId );
const index = data . findIndex (( item ) => item . _id === itemId );
wixData . query ( myDATABASE )
. isNotEmpty ( "reply" )
. find ()
. then ( ( results ) => {
**if** ( results . items . length > 0 ) {
console . log ( "Items-found" )
$item ( "#text205" ). show ();
$item ( "#text205" ). onClick (( event ) => {
$item ( "#text206" ). show ();
});
} **else** {
$item ( "#text205" ). hide ();
// handle case where no matching items found
}
});
});
});