On the page there is a repeater with one template connected to the database "# dataset1", which allows you to display the entire collection. Next, I would like to click on the image and show more detailed information on the selected element of the database. In one of the fields in the database "# dataset1" I wrote down the sequence numbers of the elements of the database, and the text of the repeater pattern "# text1" was connected to this field. In processing the image click, I wrote this script
let n = Number.parseInt ($ w ("# text1"). text);
console.log (n);
$ w ("# dataset1"). setCurrentItemIndex (n);
console.log ($ w ("# dataset1"). getCurrentItemIndex ());
But in spite of the fact that "n" is always transmitted correctly, $ w ("# dataset1"). setCurrentItemIndex (n); always returns "0". The database always starts with the first element. What am I doing wrong? Tell me please
Glad to hear it worked out.
Thank you! Now it is clear.
Hi,
I am unable to run your site in Preview - it locks up no matter what page I try to run from.
However, your problem with access to the dataset is that the $w context selector of the onClick() function refers to the repeater item and does not have access to the page context. Therefore $w does not "see" the page's dataset.
You need to change the context selector of your onClick() function, sort of like this:
export function button36_click(event, $somethingelse) { let n = Number($somethingelse("#text155").text); console.log(n); $w("#dataset1").setCurrentItemIndex(n) .then(function () { console.log($w("#dataset1").getCurrentItemIndex()); }); $somethingelse("#text160").text = "front"; $somethingelse("#image36").src = $somethingelse("#image15").src; $somethingelse("#columnStrip12").scrollTo(0, 300); $somethingelse("#columnStrip12").show(); }
As you can see, I changed the context selector to $somethingelse, and then I access the dataset with the $w global context selector: $w(#dataset1").
Another way would be to define the onClick() function in the onItemReady() function. You can see how that's done in the onItemReady() API.
Good luck,
Yisrael
https://editor.wix.com/html/editor/web/renderer/edit/c91d4d06-b092-4c92-8302-3d4abc3086c4?metaSiteId=d3045c22-e30d-45d1-bddb-f5f41d4fa1d6&editorSessionId=15A0B141-B5C9-4DC3-B7DB-09495BA38688
On the page strip there is a repeater connected to the collection of clothes "BOP" including 18 images for each element of the collection. Now there are 10 elements in the collection, but expansion is expected. The repeater gives the basic data for the collection. To display detailed information on a collection item on another strip, a viewer is placed, and a questionnaire in the form of a drop-down list for items of clothing. Upon completion of information input, an order with a calculated price that can be sent by mail. The viewer consists of one detailed image and 3 icons specifying a further output front, side, back on detailed image. Manage the movement of the collection can be using the buttons, linked by reference to the "next" and "previous."
What I want to achieve.
When the page is called, the repeater displays the main elements of the collection. The viewer is hidden first. By clicking on one of the repeater images, the viewer is shown with the selected collection item. Now this does not happen. Base "BOP" begins with the first element of the collection.
Mikhail, I express my gratitude for your readiness to help me.
I believe problem is because you re trying to set index inside a repeater's container scope.. It doesn't have sense in this context
anyway, i need context to help you with this
Please, share a link toyour site where you expirience problems and try to describe what you want to achieve
Hi, Mikhail
Many thanks, but it did not help. When I connected the error handling, I received a message:
DatasetError: The "setCurrentItemIndex" function cannot be called on the dataset because the dataset was selected using a repeated item scope selector. Read more about repeated item scope selectors: http://wix.to/94BuAAs/$w.Repeater.html#repeated-item-scope
Do not tell me the next steps? I'm just starting to program in codes.
Hi!
Answer is promises.
https://www.wix.com/code/reference/wix-dataset.html#setCurrentItemIndex
This operation is asynchronic. Meaning that both setCurrent... and getCurrent... are executed IN PARALLEL.
For you that means that when you try to read this index, it's not yet set to any number, it's default - and that is why it's "0"
It should work with this:
let n = Number.parseInt($w("#text1").text); console.log (n); $w("#dataset1").setCurrentItemIndex(n).then(function(){ console.log($w("#dataset1").getCurrentItemIndex()); });