0

I'm show the user list using datatable angularjs. I want to hide show column using conditionally.suppose role is onther then last column is not show and role is admin then show this last column.how can do that I don't know any one know how can fix it please let me know.

This is my controller.js:

app.controller("userscontroller", [
  "$scope", 
  "$http", 
  "DTOptionsBuilder", 
  "DTColumnBuilder", 
  "userservice",
  "$compile",
  function ($scope, $http, DTOptionsBuilder, DTColumnBuilder, userservic, $compile) {       
    $scope.dtColumns = [            
     DTColumnBuilder.newColumn("fullName", "Full Name").withOption('name','firstname'),
     DTColumnBuilder.newColumn("username", "Name").withOption('name','username'),
     DTColumnBuilder.newColumn("email", "Email").withOption('name', 'email'), 
     DTColumnBuilder.newColumn(null).withTitle('Action').notSortable().renderWith(function (data, type, full, meta) {            
       return '<button class="btn btn-primary" ng-click="delete(' + data.id + ');"><i class="fa fa-eye"></i>' + '</button>';                    
     })          
    ]

    $scope.dtOptions = userservice.GetAllUser(DTOptionsBuilder)
    .withOption('processing', true)
    .withOption('serverSide', true)
    .withPaginationType('full_numbers')
    .withDisplayLength(50)
    .withOption('aaSorting', [3, 'desc'])

    function createdRow(row, data, dataIndex) {
     $compile(angular.element(row).contents())($scope);
    } 
  }
]);

Here I want to if(IsAdmin) then show delete column other wise hide this column how can do?

6
  • @julien TASSIN any idea about this how can fix it??? Commented Apr 11, 2017 at 10:12
  • Where does isAdmin come from ? How do you laod it ? Commented Apr 11, 2017 at 10:14
  • isAdmin is come from layout in <script> tag i m check and set globally isAdmin true and here i m check Commented Apr 11, 2017 at 10:15
  • If so, why don't you just protect with if(window.isAdmin) {xxx} the last DTColumnBuilder.newColumn ? Commented Apr 11, 2017 at 11:32
  • how can do can you please just give me hint Commented Apr 11, 2017 at 12:21

1 Answer 1

1

Let's assume your isAdmin is set like so :

<script>window.__IS_ADMIN__ = <%=is_admin_from_server%></script>

You will have in window.__IS_ADMIN__ a boolean value that tells you if the connected user is admin. In your controller, you can check like this :

app.controller("userscontroller", [
  "$scope", 
  "$http", 
  "DTOptionsBuilder", 
  "DTColumnBuilder", 
  "userservice",
  "$compile",
  function ($scope, $http, DTOptionsBuilder, DTColumnBuilder, userservic, $compile) {       
    $scope.dtColumns = [            
     DTColumnBuilder.newColumn("fullName", "Full Name").withOption('name','firstname'),
     DTColumnBuilder.newColumn("username", "Name").withOption('name','username'),
     DTColumnBuilder.newColumn("email", "Email").withOption('name', 'email')
    ]; 
    // Bad practise to call directly window we should use $window or better a service
    if(window.__IS_ADMIN__) {
      $scope.dtColumns.push(DTColumnBuilder.newColumn(null).withTitle('Action').notSortable().renderWith(function (data, type, full, meta) {            
         return '<button class="btn btn-primary" ng-click="delete(' + data.id + ');"><i class="fa fa-eye"></i>' + '</button>';                    
      }));     
    }     

    $scope.dtOptions = userservice.GetAllUser(DTOptionsBuilder)
    .withOption('processing', true)
    .withOption('serverSide', true)
    .withPaginationType('full_numbers')
    .withDisplayLength(50)
    .withOption('aaSorting', [3, 'desc'])

    function createdRow(row, data, dataIndex) {
     $compile(angular.element(row).contents())($scope);
    } 
  }
]);

With this (pretty ugly) way you can have a conditionnal display of the last column.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.