Search code examples
user-interfacewidgetreportgoogle-app-maker

Show records per user in table widget in App Maker


I'm almost finished with my first App Maker app (learned a lot, thanks for the help so far!). The app currently collects timestamp data when users log in and out of the work site. As the time between leaving and returning could be a few hours, and because App Maker is browser based, I originally had issues where the user was unable to complete the record and tap the Time_IN button. I solved this by moving the Time IN Button to a pop-up attached to the records page, so when the user came back to work they could open the record, tap Time IN and be on their way. Simple, right?

New Time IN method

My trouble now is that users were unable to see their own records on the UserRecords page, unless I made them admins, but then it grants access to the full table on not just their own, showing all records for all users. The navigation menu originally had this snippet of code which I removed:

(@user.roles).indexOf('Admins', 'Managers') !== -1

but I'm not sure if I've missed something as the issue still seems to be partly user role based? Here is an image of the datasource fields, there are only two models in this app, GatePass (as shown in the picture) & Directory (for user picker).

Datasource Fields

An export looks like this (and the idea is to get each user their own data, not the whole source table):

Export data

Also, this is what the users input page looks like now (if it helps), I managed to get the user email to autofill with record.Email = Session.getActiveUser().getEmail(); in the onBeforeCreate:

User Entry Latest

In a nutshell, the user should only see their records in the UserRecords page so they can access and complete their submission. The same is doubly true for their exports. Can anyone please help?


Solution

  • In your model, go to the DATASOURCES tab. If you have more than one datasource showing up, then it's up to you to choose the right one. Make sure to uncheck the option Automatically load data.

    enter image description here

    Next, go the page where the table is located. Then on the table onAttach event put the following:

    var ds = widget.datasource;
    if(!app.user.role.Admins){
      ds.query.filters.Email._equals = app.user.email;
    }
    ds.load();
    

    That should do the trick. In regards to the permission issue with the admin role, make sure that the model has the proper permissions. For that, go the the model's SECURITY tab and make sure to set the proper permissions.

    enter image description here

    References:

    https://developers.google.com/appmaker/security/secure-app-data https://developers.google.com/appmaker/models/datasources