Table filtering delay issue

HI,

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

});


Thanks

Fraser

Bump? any takers? i was trying to have it hidden and only show after the filter takes place but cant get it to work

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( function () {
if ($w(“#diettable”).hidden) {
}
filterdiets($w(“#diettable”).show())
});

function filterdiets (parameter) {
$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);
});
});
});
}

You want something like this (not tested):

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

Cheers Yisreal your the saviour of this forum keep up the great work mate… we need you

Im getting a parsing error at the end and i cant seem to close it off can you assist?

@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.

@yisrael-wix Not lazy just wish i could figure it out myself!!! with yours and other experts kind of help we all learn so thanks again.

Works perfectly.

One final question so i understand better? all these lines down and brackets are loops right?

where can i learn a bit more about them and how to close them?

@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.

HI,

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?

Please give me a login that I can use to test. Also, make sure that login has diet data to display.

Thanks

@yisrael-wix

HI mate,

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.

How would i write it to check them as well?