About Filtering
Depending what method you used to connect your data, will determine the best way to query or filter that data. For example, if you connected your data directly to a dataset, then you can write some code to filter the dataset so that anything connected to that dataset will display data based on the filter you wrote. Another example, if you connected your data by query then you may possibly need to trigger another query and then set data again.
If you are not sure when you would need to filter data based on user logged in, here are some scenarios:
Filtering a Table
Filtering a Repeater
Filtering a Form to edit content
Filtering records added by an Admin
Filtering records added by other Users
Good to Know #1
Filtering data created by the person logged in
If you want to display data that was created by the person who is currently logged in, then chances are you may be able to do this without writing additional code to the page. (Of course, this may not be true in all instances as it all depends on your page logic and how you have the site and pages configured.)
If you have 1 or more datasets connected to data on your page(s) then repeat this step for all datasets:
Click on the dataset 'Settings' icon
Click on 'add filter'
Search for the 'Owner' field
After you select 'Owner' field then the 'is logged in user' filters will automatically populate
Click on 'add filter' to save the setting
After you publish your changes, every time a user logs into that page you configured they will only see the records they have personally saved into the custom database.
Good to Know #2
Filtering data created by someone else
If you want to display data that was created by the someone else who is NOT currently the person logged in, then you need to write a little bit of code. In order to accomplish this, you will need to use some time of reference point to determine what content is the correct content to filter for this person who is logged in. For example:
Use an ID reference / text
Use an email address
Use a code or identifying number
Use a unique identifier that is not shared by any other person
In our example code, we use a simple email address text field as the reference point because it is the easiest, most convenient to implement with the least amount of code since the person logging in can be identified by their email address. Our example assumes there is a field in the database collection called "Email" with the field key of "email". Our code will get the current user that is logged in and then filter the dataset for any matching records with the same email address.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
$w.onReady(function () {
$w("#myDataset").onReady(() => {
user.getEmail()
.then((email) => {
let userEmail = email; // "user@something.com"
$w("#myDataset").setFilter(wixData.filter()
.eq("email", userEmail)
);
});
});
});
Author
by Code Queen
Stuck on a project? Hire Code Queen, LLC!
Schedule a phone call or video call directly online. In a different time zone? No problem! Code Queen currently has clients around the world.
Online Booking: Discovery Session
Contact Form: Send project details
Comments