How to hide a column of a datatable if it is a specific user?

0

I have a datatable with a column defaultContent , where I have buttons to edit, delete and give privileges but I want to hide it if the profile type is user, that is, only the administrator can see that column

usser.js ...

$(document).ready(function(){
//...
var table = $('#dt_usser').DataTable({
    "ajax": "php/usser_cargar.php",
    "columns":[
        { "data": "num_emp", "visible": false},
        { "data": "nom"},
        { "data": "nom_usu"},                   
        { "data": "tipo"},
        { "data": "status", "sClass": "dt-body-center"},
        {"defaultContent": '<div class="text-center"> <button title="Editar perfil." type="button" class="editar btn btn-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></button> <button  title="Eliminar usuario" type="button" class="eliminar btn btn-danger"><i class="fa fa-trash-o" aria-hidden="true"></i></button> <button title="." type="button" class="privilegios btn btn-default"><i class="fa fa-key" aria-hidden="true"></i></button> </div>', "width": "15%" }
    ],"language": datables_espanol
    //...
}

How to hide column defaultContent with handling session ?

I wanted to perform a function within the same js but it stops showing all the data in the table.

function direc(){
   if (<?$_SESSION['usrSis_tipo'];?> === 'U'){
      table.columns([5]).visible(false);
   }

};

In usser.php is the structure of the table that is filled with the code placed in usser.js , and usser_cargar.php the query is made to the database and the data is sent in JSON

    
asked by Roii Santos 28.12.2017 в 18:40
source

3 answers

0

I occupy a js called priv.js I put part of the code

function cargarPrivilegios($e){
    $.ajax({
     url:'php/priv.php',
     type:"POST",   
	 data:{num_emp:$e},
     dataType:'json',
     success: function(data){
            var $icono;
            if (data.exito){
                           
                if (data.ges_add){ }else{$(".btnU").remove(); }
                if (data.ges_mod){ }else{$(".editarU").remove(); }
                if (data.ges_elim){ }else{$(".eliminarU").remove(); }
				if (data.ges_priv){ }else{$(".privilegiosU").remove(); } 

as you see in the url a php file is used to query the database ... I also place part of the priv.php file code

$sql = "SELECT mant_acceso, mant_add, mant_mod, mant_elim, ges_acceso, ges_add, ges_mod, ges_elim, ges_priv FROM usser WHERE num_usr = '".$cbx_emplP."'";
	if (!$resultado =$conexion->query($sql)) {
		$jsondata['exito'] = false;
		$jsondata['mensaje'] = "La ejecución de la consulta falló.";
		$jsondata['tipo_mensaje'] = 'danger';
	}else{
		  if($resultado->num_rows>0){
            
                       $data = mysqli_fetch_assoc($resultado);         
			  
                        $jsondata['exito'] = true;
              if ($data['mant_add']==1){
                            $jsondata['mant_add'] = true;    
                        }else{
                            $jsondata['mant_add'] = false; 
                        }
              
                        //-----------------------------------------
                        
                        if ($data['mant_mod']==1){
                            $jsondata['mant_mod'] = true;    
                        }else{
                            $jsondata['mant_mod'] = false; 
                        }
...

we return to the file priv.js in the function we see that in each if is where I check if you have the privilege, nothing happens is shown, but if you have the privilege, I remove the button with its respective class

    
answered by 08.01.2018 / 20:24
source
0

validate the user profile from the database, if for example you do not want to show them to the tellers do not add it in the if that I put below

if ( isset(user['admin']) or isset(user['superusuario'])){
<td>'.$columna['campo'].'</td>
}
    
answered by 28.12.2017 в 19:14
0

A simple solution ( but not very elegant ) could be:

  • In the file usser.php you save the value of the variable of PHP $_SESSION['usrSis_tipo'] in a global variable of JS (example window.usrTipo )

    <!-- ... Código ... -->
    <script>window.usrTipo = "<?= $_SESSION['usrSis_tipo'] ?>"</script>
    <script src="{PATH}/usser.js"></script>
    <!-- ... Código ... -->
    
  • In the file usser.js , you should consume this variable to validate the level

    $(document).ready(function(){
      //...
      var table = $('#dt_usser').DataTable({
        "ajax": "php/usser_cargar.php",
        "columns":[
          //...
          {
            "visible": window.usrTipo !== 'U', // Si no usuario
            "defaultContent": '<div class="text-center"> <button title="Editar...',
            "width": "15%"
          }
        ],
        "language": datables_espanol
        //...
    
answered by 28.12.2017 в 19:45