Show or hide collectors button with if statement

Show or hide collectors button with if statement

martin1223345martin1223345 Posts: 84Questions: 23Answers: 0

I am trying to inplent the if statement in the buttons function. I cant seem to find the right syntaxt, it keeps saying error.. I need the button to show or hide to be not there when the condition is not matched.

I tried to do it like this, and on a lot of other ways but nothing seems to be correct. Doing it this way was the only accepted syntax option but this wont remove the button from sight..

 dom: 'Bfrtip',
        buttons: [
            {
                extend: 'collection',
                text: 'Table control',
                buttons: [
                    {
                        text: 'Show Type',
                        action: function ( e, dt, node, config ) {
                             if (php_va == "Hardbeat")
                            dt.column( -11 ).visible( ! dt.column( -11 ).visible() );
                        } 
                    },
                    {
                        text: 'Show Plane',
                        action: function ( e, dt, node, config ) {
                            dt.column( -8 ).visible( ! dt.column( -8 ).visible() );
                        }
                    },
                    {
                        text: 'Show Invite',
                        action: function ( e, dt, node, config ) {
                            dt.column( -2 ).visible( ! dt.column( -2 ).visible() );
                        }
                    },
                    {
                        text: 'Show Send location',
                        action: function ( e, dt, node, config ) {
                            dt.column( -1 ).visible( ! dt.column( -1 ).visible() );
                        }
                    }
                ]
            }
        ]
   

      });

http://live.datatables.net/zuxibeji/1/edit

Answers

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    That if statement will only execute when you click the button. See this thread for ideas on showing/hiding buttons based on condition.

    Kevin

  • martin1223345martin1223345 Posts: 84Questions: 23Answers: 0

    Thanks for reacting. I tried that example but i get this error? Uncaught SyntaxError:

            var checkCol = 11;         //checkbox column
                    var php_va = "<?php echo $targetlocation; ?>";
        console.log(php_va);
                    
                    $(document).ready(function() {
        
                    var table = $('#accountTable').dataTable({
                tblDT = $('#accountTable').DataTable()  
                    if(php_va === "Hardbeat"){
        tblDT.button( 15 ).enable( true );
        tblDT.button( 16 ).enable( true );
        console.log('Button 0 should be ENABLED');
    }else {
        tblDT.button( 15 ).enable( false );
        tblDT.button( 16 ).enable( false );
        console.log('Button 0 should be DISABLED')
    }
    
    
  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    The problem is you copied that code inside your Datatables initialization parameters. That wont work. That code needs to be outside of the Datatables initialization function.

    Look at the last post in the thread. Use the buttons.buttons.className to assign a classname to the button(s) you want to show/hide based on the condition. Before your Datatables initialization code set that classname like shown in the thread to hide the button.

    Kevin

This discussion has been closed.