hide column [data: {}] with conditional?

hide column [data: {}] with conditional?

cris19ncris19n Posts: 55Questions: 18Answers: 0
edited April 2021 in DataTables 1.10
   let user=$('#user').val();

 "columns": [
           //{ data: 0 },
           { data: 1 },
            { data: 2 },
           //here how can I do something like that?
           if(user=='admin'){
                 { 
                     "render" function ( data, type, row ) {
                     }
                   }           
            }else{
                //If you do not comply, do nothing.
             }
    ]

How can I do something like this?

what I want is that if the user is an administrator,
Show me the action buttons.
if it is another user it does not show anything.

thanks for the answers

Replies

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited April 2021

    IF you want to hide the whole column if not an admin then use column.visible, something like this:

      let user=$('#user').val();
     
    "columns": [
              //{ data: 0 },
              { data: 1 },
               { data: 2 },
               { data: null,
                 visible: user === 'admin' ? true : false,
                        "render": function ( data, type, row ) {
                           return '<button>...';
                        }
                      }          
    
       ]
    

    Otherwise if you want to show the column but no button then put you if statement inside the columns.render function, like this:

      let user=$('#user').val();
     
    "columns": [
              //{ data: 0 },
              { data: 1 },
               { data: 2 },
               { data: null,
                        "render": function ( data, type, row ) {
                           if (user === 'admin) {
                             return '<button>...';
                           }
                           return '';
                        }
                      }          
    
       ]
    

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0
    edited April 2021

    thanks @kthorngren , I had more or less the idea with visible property, I was injecting everything into a function.

    Thank you so much

This discussion has been closed.