最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Datatables hide columns if user doesn't have the rigth role in Spring security - Stack Overflow

programmeradmin1浏览0评论

I have to hide some columns of my datatable if the user isn't ADMIN. In HTML I have this code

<table id="fleetsTable"
    class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Application</th>
            <th>Cubic</th>
            <th>Power</th>
            <th>Euro class</th>
            <th>Engine Type</th>
            <th>Traction</th>
            <th>Transmission</th>
            <th>Cars</th>
            <th sec:authorize="hasRole('ROLE_ADMIN')">Delete</th>
        </tr>
    </thead>
</table>        

The table is filled with ajax value through javascript. I have this code:

if ( ! $.fn.DataTable.isDataTable( '#fleetsTable' ) ) {
    fleetTable = $('#fleetsTable').DataTable({
        responsive: true,
        //disable order and search on column
        columnDefs: [
             {
                 targets: [7, 8],
                 orderable: false,
                 searchable: false,
             }
         ],
         //fix problem with responsive table
         "autoWidth": false,
         "ajax": "fleet/table",
         "columns": [
             { "data": "application" },
             { "data": "cubic" },
             { "data": "power" },
             { "data": "euroClass" },
             { "data": "engineType" },
             { "data": "traction" },
             { "data": "transmission" },

             { 
                 data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-primary" id="showCarsButton">Show cars</button>'; 
                 }

             },
             {data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-danger" id="deleteFleet" data-toggle="modal"'
                 +'data-target="#deleteFleetModal">Delete</button>' 
             }
             }
        ],
    });
}
else {
    fleetTable.ajax.url("table").load();
}   

To check if user has the right role I use a hidden input in my HTML and

document.getElementById("role").value=="[ROLE_ADMIN]"       

in javascript. But how can I avoid to build the delete button? The html code only hide the name of the column. Thanks

UPDATE: for now I hide the column

if (!(document.getElementById("role").value=="[ROLE_ADMIN]")){
        // Get the column API object
        var column = fleetTable.column(8);
        // Toggle the visibility
        column.visible( false);
}

but I prefer to don't create the column

I have to hide some columns of my datatable if the user isn't ADMIN. In HTML I have this code

<table id="fleetsTable"
    class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Application</th>
            <th>Cubic</th>
            <th>Power</th>
            <th>Euro class</th>
            <th>Engine Type</th>
            <th>Traction</th>
            <th>Transmission</th>
            <th>Cars</th>
            <th sec:authorize="hasRole('ROLE_ADMIN')">Delete</th>
        </tr>
    </thead>
</table>        

The table is filled with ajax value through javascript. I have this code:

if ( ! $.fn.DataTable.isDataTable( '#fleetsTable' ) ) {
    fleetTable = $('#fleetsTable').DataTable({
        responsive: true,
        //disable order and search on column
        columnDefs: [
             {
                 targets: [7, 8],
                 orderable: false,
                 searchable: false,
             }
         ],
         //fix problem with responsive table
         "autoWidth": false,
         "ajax": "fleet/table",
         "columns": [
             { "data": "application" },
             { "data": "cubic" },
             { "data": "power" },
             { "data": "euroClass" },
             { "data": "engineType" },
             { "data": "traction" },
             { "data": "transmission" },

             { 
                 data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-primary" id="showCarsButton">Show cars</button>'; 
                 }

             },
             {data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-danger" id="deleteFleet" data-toggle="modal"'
                 +'data-target="#deleteFleetModal">Delete</button>' 
             }
             }
        ],
    });
}
else {
    fleetTable.ajax.url("table").load();
}   

To check if user has the right role I use a hidden input in my HTML and

document.getElementById("role").value=="[ROLE_ADMIN]"       

in javascript. But how can I avoid to build the delete button? The html code only hide the name of the column. Thanks

UPDATE: for now I hide the column

if (!(document.getElementById("role").value=="[ROLE_ADMIN]")){
        // Get the column API object
        var column = fleetTable.column(8);
        // Toggle the visibility
        column.visible( false);
}

but I prefer to don't create the column

Share Improve this question edited Feb 17, 2016 at 13:59 luca asked Feb 17, 2016 at 13:43 lucaluca 3,32811 gold badges69 silver badges149 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 13

Very simple. You can use columnDef's visible property :

columnDefs : [
  { targets: 8, visible: document.getElementById('role').value == '[ROLE_ADMIN]' }
]

...Assuming it is column #8 we want to skip - by that the column is never created if #role is any different from [ROLE_ADMIN].

Although , i am late to the party, but i want to share my solution as well.

In Javascript (Used with Laravel)

var role = "{{ Auth::user()->role_id }} ";

$(function(){
    var showAdminColumns =  role ==3 ? true:false;

    $('#datatable').DataTable({
        serverSide:true,
        processing:true,
        pageLength:25,
        ajax:urlMasterData,
        columns:[

            { data:'edit' , name: 'edit' ,visible : showAdminColumns},
            { data:'cancel' , name: 'cancel' ,visible : showAdminColumns},

        ],

    })
})

Hope it helps

You can keep the column array as a local variable. Push the required columns first and then depending on the condition you can push the delete column. Then assign this array to columns.Like

var cols= [
             { "data": "application" },
             { "data": "cubic" },
             { "data": "power" },
             { "data": "euroClass" },
             { "data": "engineType" },
             { "data": "traction" },
             { "data": "transmission" },

             { 
                 data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-primary" id="showCarsButton">Show cars</button>'; 
                 }

             },
          ];
     if (!(document.getElementById("role").value=="[ROLE_ADMIN]")){
          cols.push( {data:null, render: function ( data, type, row ) {
             return '<button type="button" class="btn btn-danger" id="deleteFleet" data-toggle="modal"'
             +'data-target="#deleteFleetModal">Delete</button>' 
         }
        };

and then initialize the table like

 "columns": cols

it is work for me just checking condition true or false on $permission.

In Javascript with Laravel

var permission = "{{$permission}}";
$(function() {
var showhideColumns =  permission;
  console.log(showhideColumns);
    $('#tbl-order-items').DataTable({
    processing: true,
    serverSide: true,
    pageLength: 50,
    ajax: "{{ url('/supplier_order/processing_data_order_item_history', 
       ['supplier_order_id'=>$supplier_order_id]) }}",
        columns: [
                  { data: 'image', name: 'image' },
                  { data: 'sku', name: 'sku' },
                  { data: 'product_name', name: 'product_name' },
                  { data: 'status', name: 'status' },
                  { data: 'option', name: 'option' },
                  { data: 'qty', name: 'qty' },
                  { data: 'price', name: 'price' },
                  { data: 'supplier_price', name: 'supplier_price' },
                  { data: 'supplier_actual_price_yuan', name: 
                  'supplier_actual_price_yuan' },
                  { data: 'supplier_actual_price_usd', name: 
                  'supplier_actual_price_usd', visible : showhideColumns  },
                  { data: 'arrived_date', name: 'arrived_date'},
                  { data: 'actions', name: 'actions'}
              ],
              "language": {processing: '<i class="fa fa-spinner fa-spin fa-2x 
              fa-fw" style="color:blue"></i><span class="sr-only">Data is 
               Loading...</span> '},

            });

          });
发布评论

评论列表(0)

  1. 暂无评论