Textbox in the repeater is connected to database showing items.I want to send that item to another database .My code is sending data but not what item it is showing.Please help me ,I am new in coding.MY Code:
import wixData from ‘wix-data’;
var item;
$w.onReady(function () {
//TODO: write your page related code here…
item = $w(“#text2”).text;
});
export function button1_onclick() {
//Add your code for this event here:;
wixData.insert(‘wishlist’, {test1:item});}
Hi,
I like you way, I have another suggestion.
Here is a code sample.
Note the comments
import wixData from 'wix-data';
import wixUsers from 'wix-users';
export function button1_click(event, $w) {
const user = wixUsers.currentUser;
const userId = user.id;
const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("yourDatabaseCollection") // get the item from the database collection.
.eq("_id", itemId)
.find()
.then((results) => {
const itemToWishlist = results.items[0];
itemToWishlist.userId = userId; // adding a field of the current user to the database in order to distinguish between other users wishlist.
wixData.insert("wishlist", itemToWishlist) // finally, insert it to the wishlish database collection.
.then(() => {
console.log('Done')
});
});
}
Good luck!
Roi
Hi Roi,
Thanks for the Reply.
I try to work with your code but as I am new to coding,can get anything from it and it is not working for my problem.
Here I have a repeater and I want to get the item (showing in the text box which is connected to the menu database) and save the item name in the textbox to another database on the click of the button in the repeater.
Hi Roi,
Thanks for your code. I have successfully implemented it!
How can I submit other static fields from the page into the same entry in the same database (wishlist)?
For example,
let toInsert = {
“CustomerName” : $w(“#text1”).text,
“Country” : $w(“#text2”).text,
“City” : $w(“#text3”).text,
};
wixData.insert(“wishlist”, toInsert)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Thanks.
Ben
Hi,
Just add it to the toInsert object, Make sure it matches to the fieldkey in the database
let toInsert = {
CustomerName: $w("#text1").text,
Country: $w("#text2").text,
City: $w("#text3").text,
fieldkey: $w('staticDataInPage').value,
anotherFieldkey: $w('anotherStaticDataInPage').value,
};
Good luck!
Roi.
Thanks Roi! It works!
I have another question regarding repeaters.
I have already added a filter to my repeater, how can I send all filtered data to another database?
Thank you.
Cheers,
Ben
I have the same question.
Should I collect the filtered data from the repeater or from the original collection to another database?
Hi,
Can you please open a new thread regarding the new issues?
Roi.
Hi @roi-bendet i wonder if you can help me please, my question is along similar lines
I have a repeater displaying data from one collection and like Sailen above also need to insert the data associated with the repeater by clicking on the button in repeater and updating to another collection
There are three field keys in the repeater and i have created three field keys in the new collection.
The new collection already has the three fields populated, i’d just like to change the fields based on the button selection (add to collection item)
The field keys in the repeater are
country, countryFlags and continents
The field keys in the collection i wish data to be updated is
country, countryFlags and continents
I would be very grateful for your help or anyones help, i’ve been trying for three days to do this
Thanks
Adam
Hey adam,
What you need to do is to set “onItemReady” function handler to the repeater.
Inside that handler set an onClick function handler to the button that inserts the data to the other collection.
Take a look at corvid api documentation for the repeater onItemReady examples.
@roi-bendet Thanks Roi, I’ve been deperate for some help so I have this from Corvid,
What do i need to do to get this into the collection?
Do i need the numSelected or should i replace this line with an additional const repeatedText = $item(“#repeatedText”); as i have a second text fieldkey
export function button46_click(event) {
$w(“#repeater1”).onItemReady( ($item, itemData, index) => {
const repeatedText = $item(“#repeatedText”);
const repeatedImage = $item(“#repeatedImage”);
const numSelected = $item(“#nonRepeatedText”);
repeatedImage.src = itemData.img;
repeatedText.text = itemData.description;
repeatedImage.onClick( (event) => {
if(repeatedText.text === “Selected”){
repeatedText.text = itemData.description;
numSelected.text = (Number(numSelected.text) - 1).toString();
}
else {
repeatedText.text = “Selected”;
numSelected.text = (Number(numSelected.text) + 1).toString();
}
} );
} );
Thanks Adam
Bumping old thread from 2018 with duplicate thread already open. closing this one.
https://www.wix.com/corvid/forum/community-discussion/update-or-use-reference-field?origin=member_posts_page
No Thread Hijacking. Don’t post about an issue and then bump other threads with your issue.