{Solved} Changing the Value Of Database From Repeater

Hi there,
In this post, I am going to share a query of mine, from my website, for finding an answer.

In my website, there is repeater. And in the repeater, there is a button. When the button is clicked a lightbox will open.

Repeater and the button —

Lightbox –

What I want is, when the button is clicked in repeater I want to get the current item (selected item ) from the repeater and change the value to the value the user submits from this lightbox.

Thanks,
Ajith

Anyone Please Help Here !!

Ajith

Ok, my friend i am back xD.

You need help with your Repeater?

What you will need is this one …

$w.onReady(function () {
    $w("#dataset8").onReady( () => {
        $w("#repeater1").forEachItem( ($item, itemData, index) => {
            $item("#BTNedit").onClick((event) => {  
            console.log("you clicked " + itemData._id)
            $w("#dataset8").getItems(index, 1)
                .then( (result) => {
 let items = result.items;
 let totalCount = result.totalCount;
 let offset = result.offset;
                    console.log(items)
                })
                .catch( (err) => {
 let errMsg = err.message;
 let errCode = err.code;
                });
            });
        });
    });
});

This will surely help you out!

Modify it for your needs and take a look at the console!

I am sure you will be able to complete the rest.

Have fun and happy coding!:grin:

Hai Russian-Dima,

Glad you have helped here !

But there is more !!!

This a part of my code of the page -

export function button4_click(event) {
 let currentItem = $w("#dataset2").getCurrentItem();
    wixWindow.openLightbox('Edit Status', currentItem)
}

Lightbox code -

export function button2_click(event) {
 let receivedData = wixWindow.lightbox.getContext();
    console.log(receivedData);

 if (receivedData) {
 let toUpdate = {
 //update data
      };      



        wixData.update("UserStatuses", receivedData._id)
            .then((results) => {
                console.log("item was removed");
                wixWindow.lightbox.close('ok');
                wixLocation.to(`/statuses-page`);

            })
            .catch((err) => {
                console.log("error: " + err);
                wixWindow.lightbox.close('error');
            });
    }
 else{
        console.log('item was not sent');
    }
}  
  1. How can I update the two values received from the page ???

Thanks ,
Ajith

In the wix documentation there is information that the onClick event does not work when calling the getCurrentItem function. He seeks only the first item. So, the solution for your case is as follows:

exportasyncfunction pickupButton_click ( event ){ let $item = $w . at ( event . context ); console . log ( $item ( " #dataset1 " ). getCurrentItem ());

I did not test all this, just some theoretical thougts. This should be the way how to update, i think. Take also a look, what you get in your console-logs!

export function button2_click(event) {
 let receivedData = wixWindow.lightbox.getContext();
    console.log(receivedData);
    
let toUpdate = {
 "_id":          receivedData._id,
 "title":        receivedData.title,
 "first_name":   receivedData.firstName,
 "last_name":    receivedData.lastName,
};

  wixData.update("UserStatuses", toUpdate)
	.then( (results) => {
		let item = results;
		console.log(results)
		console.log(results.items)
		console.log(results.items[0])
		wixWindow.lightbox.close('ok');
                wixLocation.to(`/statuses-page`);
	})
	.catch( (err) => {
		let errorMsg = err;
	});
}

Edit: Forgot to show you the example, for my first CODE… here it is…

https://russian-dima.wixsite.com/meinewebsite/repeater-problem

When you take a look into CONSOLE, you will the what my first showed code does.

Hello Everyone,

What is was actually asking is when the user clicks the button on the repeater , I want to get the items and transfer that items to lightbox. In the lightbox I want to update the data to what to user types in the lightbox

So Thanks
Ajith

Did you see my example?

Yeah.
But the code is of updating a field to the current item.
What I am trying to do is to update the current item to user input value like–

currentItem = $w('#textBox1').value

Exactly !!!

To do your wished step, you have first to know where you want to UPDATE your data, right ?

My code should give you the right choosen “ROW” in your DATABASE, where to UPDATE your data and also gives you all informations of the selected data.

How i see the situation:

  1. You click on your button in the repeater.
  2. The current selected data from DATABASE appears in a lightbox.
  3. Current data-text already loaded in the text-input-box-field.
  4. Now a user can make some changes/EDITING in this text-input-field.
  5. Then the user clicks on the save/edit-button to save changes.
  6. Data will be written back to DATABASE (TO THE SAME ROW, WHICH WAS ALREADY SELECT/ FOUND!)

RIGHT ?

Is this how it works?

Exactly ! Accurate …(no other words)!!!

It is right !!!

Then i would say you know already everything to solve your problem.

  • you have the ID of the current selected item (that’s the main-problem here)
    But with mine code you have already the current-ID of the selected item.

Now you can work an do what ever you want with it.

I see no other difficult problems.

You are not able to open a light-box?
You are not able to send the right data to your light-box?
You do not know how to save it back to database, because you do not know in which row you have to save your data, when clicking the SAVE-Button?

But wait! You know which is the selected row :grin::sweat_smile: (you got this informations already).

  • you know the current ID

  • you know the current index (by doing a query of your data and searching for ID)
    What do you need to know more???:grin:

What I need is the save (update ) the current item function code

import wixData from 'wix-data';
2
3// ...
4
5let toUpdate = {
6  "_id":          "00001",
7  "title":        "Mr.",
8  "first_name":   "John",
9  "last_name":    "Doe"
10};
11
12wixData.update("myCollection", toUpdate)
13	.then( (results) => {
14		let item = results; //see item below
15	} )
16	.catch( (err) => {
17		let errorMsg = err;
18	} );

YOU KNOW THE ID!

Ok! Let;s get to an end to this. :grin::grin:

Here the code I require is like this

import wixData from 'wix-data';
2
3// ...
4
5let toUpdate = {
6  "currentItem.fullname":          "00001",
7  "currentItem.title":        "Mr.",
8  "currentItem.bio":   "John"
9 
10};
11
12wixData.update("myCollection", toUpdate)
13	.then( (results) => {
14		let item = results; //see item below
15	} )
16	.catch( (err) => {
17		let errorMsg = err;
18	} );

((6TH LINE))

6.th -line -----> should be the ID!

It should be:

"currentItem._id":          "founded_ItemID_here",

You have found the ID already before (at the beginning)

let itemID = result.items._id (for example)

And then all the rest of your data…

6  "currentItem.fullname":          "Ajit Kumar",
7  "currentItem.title":             "Total-Beginner,
8  "currentItem.bio":               "BIO"

:grin::grin::grin:

So at the end your UPDATE-DATA will be …

let toUpdate = {
5  "currentItem._id":          "12342Dreew1603ID", //EXAMPLE!
6  "currentItem.fullname":     "Ajit Kumar",
7  "currentItem.title":        "Total-Beginner,
8  "currentItem.bio":          "Bio,
};

This should now normaly update in the right place.

Thank You very much Russian-Dima,

This is what I was trying to achieve!!!

Just look in your followers - There will be a name ‘Ajith’

Thanks!!!

I will comment if there is any error
Thanks !!:grinning:

:grin:

Hai everyone,

The following is my code of page and lightbox respectively …
But the below code is not working !

Can you point out the mistake ??? :grinning:

Page -

export function button4_click(event) {
   wixWindow.openLightbox("EditStatus", {
 "pageSend1": $w("#dataset2").getCurrentItem.status(),
 "pageSend2": $w('#dataset2').getCurrentItem.image(),
 "pageSend3": $w('#dataset2').getCurrentItem.userId(),
 "pageSend4": $w('#dataset2').getCurrentItem.fullName()
  })

  .then( (data) => {
    $w('#text16').text = data.lightboxSend1
    $w('#image2').src = data.lightboxSend2;
    $w('#text18').text = data.lightboxSend3;
    $w('#text19').text = data.lightboxSend4;

   $w("#dataset2").setFieldValues( {
 "status":  $w('#text16').text,
 "image":   $w('#image2').src,
 "userId":   $w('#text18').text,
 "fullName": $w('#text19').text

} ); 
  } );

 
 
}

Lightbox →

export function button4_click(event) {
 let $item = $w.at(event.context);
   wixWindow.openLightbox("EditStatus", {
 "pageSend1":$item("#dataset2").getCurrentItem.status(),
 "pageSend2": $item("#dataset2").getCurrentItem.image(),
 "pageSend3": $item("#dataset2").getCurrentItem.userId(),
 "pageSend4": $item("#dataset2").getCurrentItem.fullName()
  })

  .then( (data) => {
    $w('#text16').text = data.lightboxSend1
    $w('#image2').src = data.lightboxSend2;
    $w('#text18').text = data.lightboxSend3;
    $w('#text19').text = data.lightboxSend4;

   
  } );

 
 
}

Thanks ,
Ajith