I have a table that consists of all my clients diets.
When my client is logged in the table filters against only their name, thus not showing everyone elses diets. This works ok however theres a 1 or 2 second delay before the filter kicks in where the client can see all diets which i dont want.
How do i add to the code so that the table only loads once filtered to clients name?
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(“#mydiet”).onReady( () => {
wixUsers.currentUser.getEmail()
.then((email) => {
console.log(email, ‘here is the email being sent to filter’);
$w(“#mydiet”).setFilter(wixData.filter()
.eq(“email”, email)
)
.then( () => {
console.log(“Dataset is now filtered”);
})
. catch ( (err) => {
console.log(err);
});
});
});
$w.onReady(function () {
$w("#diettable").hide(); // hide to start
$w("#mydiet").onReady( () => {
wixUsers.currentUser.getEmail()
.then((email) => {
console.log(email, 'here is the email being sent to filter');
$w("#mydiet").setFilter(wixData.filter()
.eq("email", email)
)
.then( () => {
console.log("Dataset is now filtered");
$w("#diettable").show(); // show after dataset is filtered
})
.catch( (err) => {
console.log(err);
});
});
}
@fraser Hey, sorry, I was being lazy before. Still not tested, but at least I made sure that brackets are balanced (I hope):
$w.onReady(function () {
$w("#diettable").hide();
$w("#mydiet").onReady(() => {
wixUsers.currentUser.getEmail()
.then((email) => {
console.log(email, 'here is the email being sent to filter');
$w("#mydiet").setFilter(wixData.filter()
.eq("email", email)
)
.then(() => {
console.log("Dataset is now filtered");
$w("#diettable").show();
})
.catch((err) => {
console.log(err);
});
});
});
});
Basically, the idea is that the table is hidden when the page loads (you can also set this in the component’s property panel), and the table is set to show when the dataset is filtered.
@Fraser (www.FMPhysiques.com) Well, first of all, the idea is if you start with one bracket, then you have to close it. The idea is that they have to be balanced. They’re not necessarily loops, but they’re “entities”. That is, it’s a chunk that begins and ends. An open bracket at the beginning, a closing bracket at the end.
To learn more about Javascript, take a look at javascript.info. It’s a good site to learn from the “beginning”. It’s not really all that hard, it’s just you have to be very precise when telling a machine what to do. Although my wife probably says the same thing about me.
I now have a strange issue that when a user clicks the filtered table page they want it loads fine no issues however when they click the back button on a mobile device the table doesnt refilter and shows confidential information.
Any idea how i make the table refilter when they press a back button?
I think i have fixed i cant make it do it any more here is what i changed the code too.
import wixUsers from ‘wix-users’; import wixData from ‘wix-data’;
$w.onReady( function () { let user = wixUsers.currentUser; let userId = user.id; // “r5cme-6fem-485j-djre-4844c49” let isLoggedIn = user.loggedIn; // true let userRole = user.role; // “Member”
user.getEmail()
.then( (email) => { let userEmail = email; // “user@something.com”
console.log(‘Here is the email being sent to filter’, email);
$w(“#mydiet”).setFilter(wixData.filter()
.eq(“email”, email)
)
.then(() => {
console.log(“Dataset is now filtered”);
$w(“#diettable”).show();
console.log(“Table now shown”);
})
. catch ((err) => {
console.log(err);
});
});
});
Is that how you would write it? basically added a few more checks like User id and is loggedin
@fraser
You added the variables userId and isLoggedIn but you aren’t checking them. Those extra checks would help ensure that the user is in fact logged in and has the proper role assigned.