Dataset - Can't set field value

I’m trying this almost 2 weeks for now, the following simple code just won’t work:

$w("#dataset1").setCurrentItemIndex(15);
	$w("#dataset1").onCurrentIndexChanged(() => {
        	$w("#dataset1").setFieldValue("name", "new name")
			$w("#dataset1").onItemValuesChanged(() => {
			$w("#msg").text = "Name changed."
							});
						});

I think the onItemValuesChanged won’t fire, PLEASE HELP ME!!

Hi, you need to register for the value change before you make the actual change. Let us know if it works.

Hi Tomer, how do I do that?

Code is executed from top to bottom, so you need to switch the order of your code to have the event handler first, and only then the change itself.

Liran.

That doesn’t make sense… how should be the code? Send me please

Ok… I get why you’re confused, please read here about javascript callbacks.
Also, see this (read comments):

$w("#dataset1").setCurrentItemIndex(15); //Index changes, No log here.
$w("#dataset1").onCurrentIndexChanged((index) => {
    //this is a function that will be execute EVERY TIME an index is changed:
    console.log('Index changed to', index);
});
$w("#dataset1").setCurrentItemIndex(16); // Index changes, will log "Index changed to 16"

Ok this made me even more confused, I just want to change the field value of the item 15 field “name”.

Hi,
I think this is the missing part for you.
Don’t forget to set permissions:
https://support.wix.com/en/article/how-to-set-permissions-for-a-database-collection
Good luck!
Roi

Hi,
I already have that on my code

 $w("#dataset1").setCurrentItemIndex(15);
 $w("#dataset1").onCurrentIndexChanged(() => {
 $w("#dataset1").setFieldValue("name", "new name");
 $w("#dataset1").onItemValuesChanged(() => {
 $w("#msg").text = "Name changed.";
 });
 }); 

Did you set permissions ?
If it still doesn’t work, please share a link to your site
Roi

Yeah, I have set all permissions. In this page I used the code but with small changes…

Hi,
This should work:

$w.onReady(function () {
	$w("#dataset1").onReady(() => {
		$w("#dataset1").onItemValuesChanged(() => {
			$w("#msg").text = "Name changed.";
		});
		$w("#dataset1").setCurrentItemIndex(15)
			.then(() => {
				$w("#dataset1").setFieldValue("nome", "new name");
			});
	});
});

Roi

Thank you! It worked, when I need more help I’ll ask you

:slight_smile: