Hi @, I’d like to improve the code which can delete raw from the database. In fact I need little bit different use case. The following code works, but the issue is that in table work little bit strange.
Let me explain what is wrong.
- When I select a row in the table and click the button, the row in the table is deleted.
- Then, if you click on the button again (the row in the table is not selected anymore), the last row from the table will delete.
I need it different use case which will always wait when the user choose the row. If the user doesn’t choose the row, the delete function will not be enable (or show a message : The row has to be selected!). Any idea how improve the code?
here is the code:
import wixData from 'wix-data';
let idToDelete = '';
$w.onReady(function () {
$w("#dataset1").onCurrentIndexChanged(() => {
//Gets the selected row
let itemData = $w("#dataset1").getCurrentItem();
//Gets the _id of the selected row
idToDelete = itemData._id;
});
});
//A 'delete' button is placed next to the table
export function deleteButton_click() {
//Check that idToDelete has info in it
if (idToDelete !== '') {
//Removes the entry from the 'test' database
wixData.remove("test", idToDelete)
.then(() => {
console.log("Removed from database: " + idToDelete);
//Resets the variable
idToDelete = '';
//Refreshes the dataset so the changes are shown immediately
$w("#dataset1").refresh();
})
.catch((err) => {
console.log(err);
});
}
}