I’ve put together my code so it will add an entry to a database on ’ check '. - Works Great
I also then want it to delete the entry from the database on ’ uncheck '. - Doesn’t Work
// second half that doesn't work
if ($w("#checkbox6").checked === false){
let title = $w("#text153").text; // generates unique page code
let propfeatures = $w("#checkbox6").value // feature to record
wixData.query('features')
.eq('title', title) // gets match to key gen code
.eq('feature', propfeatures) // gets matches to feature
.find() // Runs the query
.then(results => {
$w("#dataset3").remove(); // deletes match
})
.catch((err) => {
let errorMsg = err;
});
}
You are doing a query on the collection without using the database, but then you are trying to use a dataset to delete the record. The dataset isn’t pointing the row returned by the query.
You should remove the record something like this:
let items = results.items;
let item = items[0];
wixData.remove("myCollection", item._id)
.then( (results) => {
let item = results; // removed item
} )
.catch( (err) => {
let errorMsg = err;
} );
Disclaimer: not tested, but shouldn’t raise your blood pressure.
Sorry, but I’m confused. I thought that you just needed to query the collection based on a checkbox, and then delete the returned item. Should be simple.
Please post the URL of your site so I can take a look. Only authorized Wix personnel can get access to your site in the editor.
I apologize, on a re-read, I realized I really didn’t explained the issue properly!
So… if i checked the boxes in the following order:
checkbox 1 - it will submit to the dataset - works (Fitness Room)
checkbox 2 - it will submit to the dataset - works (Roof Garden)
checkbox 3 - it will submit to the dataset - works (24 hr Security)
//This is what the dataset would look like
In the dataset it will record the most recent submission/check checkbox3 as being the most recent entry :
1 (top of the list) Will be checkbox 3
2 (second in the list) Will be checkbox 2
3 (bottom of the list) Will be checkbox 1
However… If for some reason I decided I didn’t want checkbox2 anymore and wanted to uncheck checkbox2:
//deletes checkbox2
it deletes the item at the top of the dataset (the most recent) (which is the check box 3 entry ).
//result on dataset
But un-checking checkbox2 should delete the entry for roof garden . (the value of checkbox 2)
Just ignore the differences in the key gen codes in field ‘title’. I needed to keep dipping in and out to take screen shots! Ordinarily, all this checking / unchecking would happen on the same ‘submit screen’ and therefore all the feature checks would have the same ‘title’ code for each property.
When I filter the dataset later on the dynamic pages it’s filtered using this as a reference.
I’m looking at the logic and IMHO you’re taking the wrong approach. Adding or deleting a record each time a checkbox is changed is not the best way way to do this. It would be easier and more efficient to handle all input once the Submit Property button is clicked. This would trigger a routine that would then insert the necessary data into the database collection.
I was way over-complicating things! I really shouldn’t be allowed near computers… I did the check on ‘aftersave’ (and repeated for all the checkboxes).
export function dataset2_afterSave() {
if ($w('#checkbox6').checked === true){
let item = $w("#checkbox6").value;
let ID = $w("#text153").text;
$w("#dataset3").setFieldValue("feature", item);
$w("#dataset3").setFieldValue("title", ID);
$w("#dataset3").save();
}
else{
console.log("item not submitted");
}
wixLocation.to("/manage-properties");
}