Hiding or showing views in the same scene based on User Profile

For those of you new to knack it can be hard to work how to build a View with multiple tables that can only be accessed by selected users. For example, having two tables on the one View. One that lets you see but not edit or delete and one that allows edit and delete. This is especially useful when you want to assign a user admin rights to a view but not the whole system.

 

  1. Add a "user role" for example U-Admin
  2. Build your view with two tables:
    1. One that can edit/delete – Lets say its view number is View_1
    2. One that cannot edit/delete – Lest say its view number is View_2
  3. Find the Object id of the U-Admin profile (You can click on the profile and see its object number in the URL above it.

Once you have the View(s) and The Object you can use the code below to hide or show the views based on whether the user is in the U-Admin profile. I am sure there are smarter ways of doing it, however I find this the easiest to code.

//----------------------------------------------------------------------------------------------------

// Hide and Show views  based on the User Profile

//---------------------------------------------------------------------------------------------------

$(document).on('knack-view-render.view_XXX', function(event, view, data) {

 

if(Knack.getUserRoles('object_XXX')!==false)//Check to see if the logged in user is in the user profile U-Admin

          {

           $('.view_XXX').hide();//Non U-Admin

          $('.view_YYY').show();//U-Admin

         }

if(Knack.getUserRoles('object_131')==false)// If not in list hide the

    {

      $('.view_XXX').show();//Non U-Admin

      $('.view_YYY').hide();//U-Admin

         }

});

Why not just handle in the page display rules based on the user account?