Hi. I want to have a switch that stays on when clicked until the member switches it back off, even if they reload or leave the page. Is there any way someone can help me with this if possible? Thanks
Hello acumenpay,
you can store the switch-status in a database (boolean-value) TRUE/FALSE.
Then you just ask the status of the switch from the database, for each user.
Thanks, that makes more sense but Iām still having a little trouble. Could you just explain how I could do that?
Do you have 1-switch for all member?
Or do you wanna have 1-switch for each member?
I want each member to have an individual switch that is separate from other membersā switches
In this example you have the whole āREAD-OUTā process.
import wixData from 'wix-data';
$w.onReady(function () {
checkStatus()
});
function checkStatus (parameter) {console.log("check started")
wixData.query("Database4")
.find()
.then( (results) => {
if(results.items.length > 0) {console.log("GO")
let firstItem = results.items[0]; //see item below
let currentItem = results.items[currentItemIndex]
let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
let myBoolean=results.items[currentItemIndex].btnStatus
if (myBoolean===true) {$w('#switch1').checked=true} else {$w('#switch1').checked=false}
console.log(currentItemIndex)
console.log(myBoolean)
} else {
console.log("Nothing found.")
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
export function button1_click(event) {checkStatus()}
export function button2_click(event) {checkStatus()}
export function switch1_dblClick(event) {checkStatus()}
You will need following elements to reconstruct itā¦
- Database (mine is called āDatabase4ā wit 2 colums in my example [title & BTN-Status]
- Dataset called ādataset1ā (read-only in this example, but should be read&write at least)
- 1x switchbutton (āswitch1ā)
- 2x normal buttons (ābutton1ā & ābutton2ā)
- 3x text (one of them connected to database (ātitleā)
PART-1 is done!
Now itās your turn, to complete the write-action into the database.
Vielen Dank. Das hat viel geholfen.
If it was helpful, donāt forget to give a like
Thanks, but Iām still a little confused. I know youāre not here to write the code for me but any info at all helps a ton. Could I do something where it inserts the input into a database and then from there it can be saved to memorize which account has set the switch to the on or off position? Like this:
import wixData from āwix-dataā ;
export function button24_onChange(event, $w) {
let toInsert = {
āLockā : āYesā ,
};
wixData.insert( āLockAccountā , toInsert)
.then( (results) => {
let item = results;
} )
. catch ( (err) => {
let errorMsg = err;
} );
(Iām trying to create a Lock Account feature btw)
You forgot to add the useId to the toInsert object.
also Iād prefer that youāll use the userId as the value of the _id field and do something like:
import wixData from'wix-data';
import wixUsers from'wix-users';
let user = wixUsers.currentUser;
let userId = user.id;
$w.onReady(() => {
if(!userLoggedIn){
$w('#switch1').disable();
} else {
wixData.get('LockAccount', userId)
.then(r => {
if(!r){
$w('#switch1').checked = true;//the default if it's a new member.
//show the current status to the
} else {
r.lock ? $w('#switch1').checked = false : $w('#switch1').checked = true;
}
$w('#switch1').enable();
$w('#switch1').onChange(event => {
let toUpdate = {
_id: userId,
lock: $w('#switch1').checked
}
wixData.save("LockAccount", toUpdate)
.catch(err => err );
})
}
})
}
})
I wrote it fast. canāt promise you I donāt have a syntax error somewhere but you got the point.
Ok, i do not know if you already understand the coding of J.D. (for beginner it could be very difficult to understand)
My solution in this case is a little bit another. I prefered to use the dataset.
Look again here⦠(UPDATED-Version)
https://russian-dima.wixsite.com/meinewebsite/switch-safe-function
import wixData from 'wix-data';
$w.onReady(function () {
checkStatus()
});
function checkStatus (parameter) {console.log("check started")
wixData.query("Database4")
.find()
.then( (results) => {
if(results.items.length > 0) {console.log("GO")
let firstItem = results.items[0]; //see item below
let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
let currentItem = results.items[currentItemIndex]
let myBoolean=results.items[currentItemIndex].btnStatus
if (myBoolean===true) {$w('#switch1').checked=true} else {$w('#switch1').checked=false}
console.log(currentItemIndex)
console.log(myBoolean)
} else {
console.log("Nothing found.")
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
export function button1_click(event) {checkStatus()}
export function button2_click(event) {checkStatus()}
export function switch1_dblClick(event) {checkStatus()}
export function button3_click(event) {WriteData()}
function WriteData (parameter) {
$w("#dataset1").onReady( () => {
let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
console.log(currentItemIndex)
console.log($w('#switch1').checked===true)
if ($w('#switch1').checked===true){$w("#dataset1").setFieldValue("btnStatus", true);}
else {$w("#dataset1").setFieldValue("btnStatus", false);}
$w("#dataset1").save();
})
}
Now the function ā> SAVE is active.
I think, now you have enoug stuff, where you can work with and learn