How can I disable a DataTable button based on roles and permissions in PHP

How can I disable a DataTable button based on roles and permissions in PHP

OlngaurOlngaur Posts: 1Questions: 1Answers: 0
edited October 2022 in Free community support

Description of problem:

I have the following statements //initializing an array based on the query $urlData = array();

Checking if the add button permission is in the array. if(in_array("#add-button", $urlData))

Now I want to say if it is not found, to hide the add button. The problem is that the button is in the Datatable as shown below

<script>
     $(document).ready(function() {
        var empDataTable = $('#departmentTable').DataTable({
              dom: "<'row'<'col-sm-9 col-md-3'><'col-sm-8 col-md-6'B><'col-sm-12 col-md-3'f>>" +
                       "<'row'<'col-sm-12'tr>>" +
                       "<'row'<'col-sm-12 col-md-5'l><'col-sm-9 col-md-4'i><'col-sm-12 col-md-3'p>>",
              responsive: true,
              buttons: [{     
                    text: '<i id="header-plus-icon-role" 
                              class="fa fa-plus" 
                              style="font-size:24px; color: rgb(7, 140, 242); " 
                              type="button" 
                              class="btn btn-info btn-lg" 
                              data-toggle="modal" 
                              data-target="#add-faculty" 
                              id="header-role">
                           </i> ',
                    titleAttr: 'Add to Base', 
                    className: "btn btn-info btn-lg",},],

I want the above button to be shown only if the user has permission to see it

"ordering": true,
            "searching": true,
            "paging": true,
            "columnDefs": [{
                "targets": 0,
                "searchable": false,
                "orderable": false,
                "visible": true,
                "targets": 0,
            }],
            "order": [
                [1, 'asc']
            ],
        });

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    You need to generated your Javascript dynamically based on the permissions that the user has. Is this Javascript inline in your PHP code for the page (i.e. in index.php or whatever file it is called)? If so, query your session / database to get the permissions and change the Javascript as needed.

    Allan

Sign In or Register to comment.