CheckBox - Delete Item from Database on 'Uncheck'

Hi

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;
		});
						
}

Any ideas, anyone?

Many thanks!

Thomas

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.

I hope this helps,

Yisrael

Hi Yisrael

Many thanks!

It’s worked to delete the most recent item submitted to the dataset:

if ($w("#checkbox1").checked === false){

	wixData.query("features")
  	.find()
  	.then( (results) => {

			let items = results.items;
			let item = items[0]; // This is the problem 
			wixData.remove("features", item._id)
     			.then( (results) => {
         			let item = results; // removed item
     			})
     			.catch( (err) => {
         			let errorMsg = err;
     			});

However, I have a couple of different checkboxes on the page and people may not always check them / uncheck them in the order they submitted them in.

How would I go about changing this:

let item = items[0]; // This is the problem

To find the value assigned to the checkbox, then delete this instead of the item placed at the top of the dataset?

E.G:
Checkbox1 value = A
Checkbox1 value = B
Checkbox1 value = C

Many thanks for the help!

Thomas

You’ll need to perform a query for each checkbox, and then delete the record returned by the query.

Hi Yisrael

I’ve been wracking my brains with this all afternoon, playing around with different combos and I’m struggling to get anything going.

Is there any chance you could give me a little bit more of a nudge regarding the code layout?

Many thanks for the help to date

Thomas

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.

Hi Yisrael

The Site is: www.kreliving.com

It’s on the ‘Manage Properties’ page.

I linked the first 3 tick boxes in the first column: “fitness” “Roof G” and “24 Sec”

Checkbox1 starts on L.79 //Fitness
Checkbox6 starts on L.125 //Roof G
Checkbox12 starts on L.172 //24 Sec

It’s all attached to the dataset “features”

Many thanks!

Thomas

Sorry, but I don’t understand what isn’t working for you. What is it supposed to do? And, what is it doing instead?

Hi Yisrael

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:

    1. checkbox 1 - it will submit to the dataset - works (Fitness Room)
    1. checkbox 2 - it will submit to the dataset - works (Roof Garden)
    1. 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 checkbox 2 anymore and wanted to uncheck checkbox 2:

//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 checkbox 2 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.

Thanks again for the help :slight_smile:

Thomas

Hi Yirsael

Did you manage to have any further thoughts on this? I keep playing around with it, but I’m not getting the results i need :confused:

Many thanks again!

Thomas

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.

Hi Yisrael

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");

}

Many thanks for the help!

:beers: