Why dataset getCurrentItem returns diff item from rowselect?

I have a simple table which I use code as below to populate/refresh a table in (1) page $w.onReady, (2) after add button (add an item) and (3) delete button (Delete an item)

export function tableDisplay(userEmail) {
wixData.query(“DateTimeTest”)
.eq(“title”, userEmail)
.ascending(“dataDateTime”) // this line for sorting
.find()
.then((results) => {

  		let tableRows = results.items; 
  		$w("#table1").rows = tableRows; 
  		$w("#table1").refresh(); 
	});	 

}

Problem is, although the row that’s selected in table1_rowSelect matches what the user clicked/selected on the screen, in buttonDelete_click, the row that’s returned from $w(“#dataset1”).getCurrentItem() is sometimes a completely different item.

This causes major problem as user will then delete the wrong item from what’s actually selected.

Suspect it’s because of the sorting which causes the displayed order to be different from the actual order in the database. And if I remove that " .ascending(“dataDateTime”) " line then the $w(“#dataset1”).getCurrentItem() inside buttonDelete_click returns the correct item. But I need the sorting to make the table easy to read.

Any idea how to keep the sorting and also make the $w(“#dataset1”).getCurrentItem() inside buttonDelete_click always returns the same item as what’s selected on the table?

Hi.

Can you please share the link to your site?

Hi Al.

From my understanding, table1 is bounded to a collection. If so, can you try an alternative of filtering the dataset1 directly?

Regards,
Genry.

Hi, thanks for the advice and help. I worked out how to fix the problem.
I found that the only option in dataset is a “filter” to restrict what’s been returned, plus another “sort” function. So instead of “query”, I use “filter” and that fixed the problem.