如果用户在Spring Security中没有严格的角色,则数据表将隐藏列 [英] Datatables hide columns if user doesn't have the rigth role in Spring security
问题描述
如果用户不是ADMIN,则必须隐藏数据表的某些列.在HTML中,我有此代码
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>
该表通过javascript用ajax值填充.我有以下代码:
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();
}
要检查用户是否具有正确的角色,我在HTML和
To check if user has the right role I use a hidden input in my HTML and
document.getElementById("role").value=="[ROLE_ADMIN]"
在javascript中.但是如何避免构建删除按钮?html代码仅隐藏列的名称.谢谢
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);
}
但我不想创建该列
推荐答案
非常简单.您可以使用 columnDef
的 visible
属性:
Very simple. You can use columnDef
's visible
property :
columnDefs : [
{ targets: 8, visible: document.getElementById('role').value == '[ROLE_ADMIN]' }
]
...假设我们要跳过的是第8列-如果 #role
与 [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]
.
这篇关于如果用户在Spring Security中没有严格的角色,则数据表将隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!