Can not get current itemIndex on dynamic page

:persevere: Hello everybody.

Just can’t get my code to work.
I have created a dynamic page, put on it 2 buttons and connected everything.
Everything works fine, but just one thing do not wanna get to work —> getCurrentItemIndex()
no matter what i do, the result is ===> 0

Here is the code-snipet for this issue.

import wixData from 'wix-data';

var Item
var ItemIndex

$w.onReady(() => {
  $w('#dynamicDataset').onReady(() => {
        Item = $w('#dynamicDataset').getCurrentItem();
        ItemIndex = $w('#dynamicDataset').getCurrentItemIndex();
        console.log(Item)
        console.log(ItemIndex)
    });
});

What do i wrong, or did i forget something?

  1. Database-permissions checked (full access).
  2. Connections checked.

Its because there is only 1 item in the dataset. Dynamic datasets only contain 1 item hence the index is 0. See the documentation here .

The getCurrentItemIndex() function returns the index of current item within the items of the dataset .

One way of getting the item’s index from its position within the database will be to query the database based on the item id and then check the index within the returned array of items but this may prove difficult if its a large database.

Ok, thanks, so my code is good, only didn’t knew this fact.
How can i handle it ?

In this case i need always the curent index of the item.

Any suggestion?

And THX for reply! :laughing:

I solved already my problem (not realy my problem) in another way, but want to know if there are still more possibilities?

Here is the post, why i needed this piece of code…
https://www.wix.com/corvid/forum/community-discussion/dynamic-page-background-color-change
And here the example, what it was needed for…
https://russian-dima.wixsite.com/meinewebsite-5/database1/%D0%B4%D0%B2%D1%83%D1%85%D0%B5%D1%82%D0%B0%D0%B6%D0%BD%D1%8C%D0%B9-%D0%B4%D0%BE%D0%BC2