Hide records owned by logged-in user in a table

Hello Knack Community…

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?

Thanks in advance!

Hi @NXTWAV

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

Cheers,

Knack Pros

@KnackPros,

Thank you for the nested loop help. =)

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

Thanks again for the quick response.

Hi,

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.

  const ownerEmail = data.field_156
  const userEmail = Knack.getUserAttributes().email

  if (ownerEmail = userEmail) {
    // ...
  }

I believe the writer of this code intended to do a comparison which has three equal signs === instead of one =.

  if (ownerEmail === userEmail) {
    // ...  
  }

Feel free to schedule a call here.

Updating for the Knack Community…

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. :star: :star: :star: :star: :star:

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()
    }
  })
});
1 Like