Code Queen

Jul 26, 20202 min

Filter Dataset based on Author, Owner, Person who created the data

Updated: Sep 14, 2020

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 or use the dataset settings to filter the dataset.

About Owner

An Owner of a record is the user that created the original record. This person is also called the Author. Regardless if additional data was added to the same record by the same or different person, the Owner remains the same.


Good to Know #1

Filtering data by Dataset settings

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 via code

If you cannot or do not want to filter the data via the dataset settings, then you can add some code to filter by user ID. The following code assumes you have connected your data to the dataset. In our example, we have used a repeater connected to a dataset and have set it to 'hidden on load' via the properties panel.


 
import wixData from 'wix-data';
 
import wixUsers from 'wix-users';
 
let user;
 
var userID;
 

 
$w.onReady(function () {
 
user = wixUsers.currentUser;
 
userID = user.id;
 

 
$w("#pageDataset").setFilter( //this is the name of your dataset
 
wixData.filter()
 
.eq("_owner", userID)
 
);
 
const millisecondsToDelay = 2000; //the milliseconds to delay the repeater from showing, this prevents flickering
 
setTimeout(() => {
 
$w('#repeater1').show(); //we show or expand the repeater after the filter has been complete so that we do not see records that belong to another owner
 
}, millisecondsToDelay);
 
});
 


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

    3620
    10