I want to make a todo list where when I select a row, that item will delete automatically. I tried .onRowSelect and roData but they don’t work because I need the _id to delete. Any type of method works!
Hi Balaram,
You can delete a database entry by either calling $w(‘#mydataset1’).remove() to remove the current item or wixData.remove(“myCollection”, _id) , which requires the item’s _id.
You can see an example of the latter below:
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);
});
}
}
Hi, thanks for posting the code, but the use case is a bit limited. When I select a row in the table and click the button, the row in the table is deleted. Then, if you click the button again (and don’t select a row), the second raw will be deleted from the database again. The function when is really selected raw is missing than delete the raw. If not, then nothing happens. Any idea how improve the code?
Old post from 2017 being closed.
Please add a new forum post if you are wanting help with your own issue.