Connecting repeater to database in condition (IF)

Hello, I would like to know how I do so, after loading a dynamic page, the Repeater connect to the data with a condition.
The condition would be:
If the title is written PPP (connect to Database * X * repeater B)
If the title is written zphp (connect to Database * Y * repeater B)

Sorry for the wrong writing, I do not speak English very well (Google translator)

Hello

You can set the repeater data using code instead of connecting it in the editor. here’s some steps you can follow:

  • Add if statement for the titles

  • For each case get the database required items using query

  • Connect the repeater items to if
    Here’s a code snippet to guide you through the idea:

import wixData from 'wix-data';

$w.onReady(async function () {
 
 let title =  //get the title here 
 if (title === "PPP") {
    let result = await wixData.query("PPP collection").find()
    await connectRepeater(result.items)
 }else if (title === "zphp") {     
    let result = await wixData.query("zphpcollection").find()     
    await connectRepeater(result.items) 
 }
});
function connectRepeater(repeaterItems) {
    $w('#repeater1').forEachItem(($w, itemData, index) => {
 // connect each repeater item elements to their data from repeater data
 // example: $w('#text1') = repeaterItems[index].textField
    })
}


Here’s some useful links:

  • Query

  • Repeater

Good luck with your website!
Massa

Hi,

First, this code is correct if the both databases have the same fields:

import wixData from 'wix-data';

$w.onReady(async () => {

 if ("Insert the condition") {
        $w('#repeater1').data = await getItemsFromDB1();

    } else {
        $w('#repeater1').data = await getItemsFromDB2();

    }
});

export function repeater1_itemReady($item, itemData, index) {
    $item('#text20').text = itemData.title;
    ....
    
}
async function getItemsFromDB1() {
 return wixData.query("The name of the database")
        .find()
        .then((results) => {
              return results.items;       
        })  
}

async function getItemsFromDB2() {
 return wixData.query("The name of the database")
        .find()
        .then((results) => {
             return results.items;       
        })
}

Explanations:

  1. repeater1_itemReady() function is triggered when a data is being inserted to the repeater.
    In order to add this function, go to the repeater’s properties and inside connect each element to the
    correct field from the database.

  2. getItemsFromDB() is a function that return an object that contain all the items from a specific database.
    this function is asynchronies, as a result we need to use await to get the return.

For more informations:

Best of Luck!
Sapir

Okay, that’s exactly it!
Specifying, in this case, the title is "Abaddon ", connected with the database "Abaddon-H "
But the others are not being loaded with their "titles-H "

"Alchemist " = database "Alchemist-H "
"Axe " = database "Axe-H "
"Beastmaster " = database "Beastmaster-H "
And so on.

I’m very grateful for your time!
I got lost in the explanations of Massa Salah and Sapir I ended up confusing everything

Sorry for the spelling mistakes, do not write very well English, I am using Google Translator

import wixData from ‘wix-data’;

$w.onReady( async () => {

let title = $w(‘#Titulo’)
if (title === “Abaddon”); {
$w(‘#Habilidades’).data = await getItemsFromDB1();

}  

});

export function repeater1_itemReady($item, itemData, index) {
$item(‘#Titulo’).text = itemData.title;

}

async function getItemsFromDB1() {
let title = $w(‘#Titulo’)
if (title === “Abaddon”); {
return wixData.query(“Abaddon-H”)
.find()
.then((results) => {
return results.items;
})
}

}

$w.onReady( async () => {

let title = $w(‘#Titulo’)
if (title === “Abaddon”); {
$w(‘#Habilidades’).data = await getItemsFromDB1();

}  

});

Hi,

View the code below you had some mistakes:

$w.onReady(async () => {
 let title =  $w('#Titulo').value (if it's an input element) or .text (if it's a text element)
 if (title === "Abaddon"); {
        $w('#Habilidades').data = await getItemsFromDB1();
      }
  });
 
//Make sure to export this function from the repeater's properties. 
export function repeater1_itemReady($item, itemData, index) {
    $item('#Titulo').text = itemData.title;
    If you have more elements in the repeater you have to write them here! 
    For example:
    $item('#text21').text= itemData.name;
    $item('#text22').text= itemData.age;
}


async function getItemsFromDB1() {
        return wixData.query("Abaddon-H")
        .find()
        .then((results) => {
         return results.items;       
        })  
    }
}

Best,
Sapir

The repeater ligament with the database is OK. However the items do not appear on the repeater

Look CODE

import wixData from ‘wix-data’;

$w.onReady( async () => {

let title = $w(‘#titulo’).text
if (title === “Abaddon”); {
$w(‘#repeater1’).data = await getItemsFromDB1();

}  

});

export function repeater1_itemReady($item, itemData, index) {
$item(‘#habilidadenome’).text = itemData.habilidade;
$item(‘#hicon’).src = itemData.icone;

}

async function getItemsFromDB1() {
let title = $w(‘#titulo’)
if (title === “Abaddon”); {
return wixData.query(“H-Abaddon”)
.find()
.then((results) => {
return results.items;
})
}

}

$w.onReady( async () => {

let title = $w(‘#titulo’)
if (title === “Abaddon”); {
$w(‘#repeater1’).data = await getItemsFromDB1();

}  

});