There doesn’t appear to be a way to filter out records NOT owned by the logged-in user… I tried to do it with the following Javascript:
/* Hides any records owned by the logged-in user in table */
$(document).on('knack-view-render.view_200', function(event, view, data, records) {
const ownerEmail = data.field_156
const userEmail = Knack.getUserAttributes().email
if (ownerEmail = userEmail) {
records.hide();
}
});
But that doesn’t appear to work either.
Does anyone know how we can exclude records, owned by a logged-in user, from a table that lists all records?
The code needs to loop through the rows in the table. Also hide() is a jQuery method which can only to be applied to jQuery objects.
The resulting code would look something like this.
/* Hides any records owned by the logged-in user in table */
/* NOTE: Update the view and field numbers to your own */
$(document).on('knack-records-render.view_133', function(event, view, records) {
const userEmail = Knack.getUserAttributes().email
const emails = $('#' + view.key + ' td.field_135')
emails.each(function(index, email) {
if (userEmail === $(email).text().trim()) {
const row = email.closest('tr')
$(row).hide()
}
})
})
You mentioned that the hide() method is only for jQuery objects, so that may explain why this still doesn’t work yet.
Do I need to hide the record row with a different method? Or via CSS?
I’m basically wanting to hide the any records on that table view that have an email matching the logged-in user’s email to try and find a workaround solution for not yet being able to filter with ‘NOT Logged-in User’.
I would recommend scheduling a Zoom meeting so someone can take a closer look at your app’s JavaScript code. Even one error anywhere will cause all of the JavaScript code to fail. When copy/pasting code it’s common to accidentally produce errors. For example the following line will trigger an error because it is reassigning the variable ownerEmail which is a constant variable.
Solution found! Thanks to @KnackPros amazing depth of knowledge. I would encourage anyone to reach out to them for any Knack customization support or project work.
During our call he also rightly suggested I replace the email with an id field for the unique identifier, which the code below has been modified to reflect:
/* Hides any records owned by the logged-in user in table */
/* NOTE: Update the view and field numbers NNN to your own */
$(document).on('knack-records-render.view_NNN', function(event, view, records) {
const loggedinUser = Knack.getUserAttributes().id
const recordOwner = $('#' + view.key + ' td.field_NNN')
recordOwner.each(function(index, id) {
if (loggedinUser === $(id).text().trim()) {
const row = id.closest('tr')
$(row).hide()
}
})
});