Add button conditionally

Add button conditionally

seven21seven21 Posts: 2Questions: 1Answers: 0

I can't find in the documentation or existing example on how to add button to a specific button layout group. In the example below, I want to add the Notify button in the topEnd only when the user has permission to do so. I am able to add the button after the datatable is initialized thinking it will take into consideration the index but it's adding the button to the first group.
Can someone help. Thanks.

 var dt = new DataTable('#example', {
 paging: false,
 ordering: false,
 info: false,
 layout: {
     topStart: { 
         buttons: ['csv', 'excel']
     },
     topEnd: {
         buttons: [{
         text: 'Invite',
         action: function (e, dt, node, config) {
                 $('#InviteModal').modal('toggle')
             }
         }
         /* ,{
             text: 'Notify',
             action: function (e, dt, node, config) {
                 $('#notifyModal').modal('toggle')
                 }
             } */
         ]
     }
     }

 }
 );

 dt.button().add(4, {
     action: function (e, dt, button, config) {
         $('#notififyModal').modal('toggle')
     },
     text: 'Notify'
 });

Answers

  • allanallan Posts: 64,373Questions: 1Answers: 10,628 Site admin

    I'd use a variable before the initialisation:

    let buttons = [{
      text: 'Invite',
      action: ...
    }];
    
    if (permissionToNotify) {
      buttons.push({
        text: 'Notify',
        action: ...
      });
    }
    

    Then for the assignment:

    topEnd: {
      buttons: buttons
    }
    

    Allan

  • seven21seven21 Posts: 2Questions: 1Answers: 0
    edited May 13

    Thanks, I'll give it a try

Sign In or Register to comment.