Adding an item to a repeater with code

Hi guys!

I want to lengthen my repeater by one item with the click of a button, and I can’t figure it out. This is where I’m at so far:

$w . onReady ( function () {

**var**  itemNumber  =  1 ; 
**const**  itemData  = [ 
        { 
    _id :  itemNumber ,  
    name :  '' ,  
    title :  '' ,  
    image :  '' 
} 

];

$w ( "#anecdoteRepeater" ). data  =  itemData ; 

$w ( “#addAnecdote” ). onClick (() => {
console . log ( “Clicked” );
// Increment the itemNumber variable
itemNumber = itemNumber + 1 ;

// Create a new item data object with default values 
**const**  newItemData  = [{ 
    _id :  itemNumber , 
    name :  '' , 
    title :  '' , 
    image :  '' 
}]; 

// Push the new item data to the itemData array 
itemData . push ( newItemData ); 
console . log ( itemData ); 

// Set the updated itemData array as the repeater's data 
$w ( "#anecdoteRepeater" ). data  =  itemData ; 

});
});

In the newItemData assignment, instead of an array with an object inside, try just a plain object.

$w.onReady(function () {

    var itemNumber = 1;
    const itemData = [
            {
        _id: itemNumber, 
        name: '', 
        title: '', 
        image: ''
    }
];

    $w("#anecdoteRepeater").data = itemData;

$w("#addAnecdote").onClick(() => {
    console.log("Clicked");
    // Increment the itemNumber variable
    itemNumber = itemNumber + 1;

    // Create a new item data object with default values
    const newItemData = {
        _id: itemNumber,
        name: '',
        title: '',
        image: ''
    };

    // Push the new item data to the itemData array
    itemData.push(newItemData);
    console.log(itemData);

    // Set the updated itemData array as the repeater's data
    $w("#anecdoteRepeater").data = itemData;
});
});

Same result at before. In the log, itemData keeps getting longer and longer, so you’d think it’s working, but the repeater just stays the same length.

I figured it out! The issue was that _id needed to be a string. I tried converting them to strings by going ‘’ + itemNumber, and suddenly it worked!

//------------ NEW ITEM TO REPEATER

$w.onReady(function () {

    var itemNumber = 1;
    const itemData = [{
            _id: '' + itemNumber,
            name: 'Amy Allen',
            title: 'CEO',
            image: 'https://static.wixstatic.com/media/4e87e18894e24c238e5724b887362eeb.jpg'
        },
    ];

    $w("#anecdoteRepeater").data = itemData;

    $w("#addAnecdote").onClick(() => {
        console.log("Clicked");
        // Increment the itemNumber variable
        itemNumber = itemNumber + 1;

        // Create a new item data object with default values
        const newItemData = {
            _id: '' + itemNumber,
            name: '',
            title: '',
            image: ''
        };

        // Push the new item data to the itemData array
        itemData.push(newItemData);
        console.log(itemData);

        // Set the updated itemData array as the repeater's data
        $w("#anecdoteRepeater").data = itemData;
    });
});


1 Like