我需要根据权限隐藏/显示操作列如果操作列为null,我想隐藏操作列,如果操作有数据,我想显示数据在后端传递。
// my backend code php
$data = $this->userModal->userslist();
foreach($data as $d){
if(ChechPermission($this->session->userdata('permissions'), "users", "edit")) {
$d->actions ='
<a href="Users/edit/'.$d->userid.'" class="btn btn-success btn-md"><i class="fas fa-pen-square"></i></a>
';
} else {
$d->actions = '';
}
}
echo json_encode($data);
// end backend code php
// my datatable
$(document).ready(function(){
$("#userTable").dataTable({
pageLength: 25,
lengthMenu: [25, 50, 75, 100],
"dom": '<"top"lfB>rt<"bottom"p><"clear">',
buttons: [
'copy', 'excel', 'pdf', 'print'
],
'ajax' : {'url' : 'Users/getusers' , dataSrc : ""},
columns : [
{data : 'firstname'},
{data : 'middlename'},
{data : 'lastname'},
{data : 'username'},
{data : 'mobileno'},
{data : 'statusname'},
{data : 'groupname'},
{data : 'actions'}
]
});
});
数据表中我期望的代码是筛选操作列数据的条件
你好,阿卜迪尔·阿卜迪拉希姆·阿里,
有一个可见属性接受boolean
。
https://datatables.net/reference/option/columns.visible
这是一个给你的例子。
您的前端:
let showActionColumn= "<?= $showActionColumn ?>";
columns : [
{data : 'firstname'},
{data : 'middlename'},
{data : 'lastname'},
{data : 'username'},
{data : 'mobileno'},
{data : 'statusname'},
{data : 'groupname'},
{data : 'actions', visible : showActionColumn}
]
您的后端:
$data['showActionColumn'] = ChechPermission($this->session->userdata('permissions'), "users", "edit")
使用columnDefs
对您的情况很有用:
$('#userTable').DataTable({
pageLength: 25,
lengthMenu: [25, 50, 75, 100],
"dom": '<"top"lfB>rt<"bottom"p><"clear">',
buttons: [
'copy', 'excel', 'pdf', 'print'
],
'ajax' : {'url' : 'Users/getusers' , dataSrc : ""},
columns : [
{data : 'firstname'},
{data : 'middlename'},
{data : 'lastname'},
{data : 'username'},
{data : 'mobileno'},
{data : 'statusname'},
{data : 'groupname'},
{data : 'actions'}
],
columnDefs: [
{
targets: 7, // column assigned in the html table
"render": function(data, type, row) {
let _data = '';
if (row['actions'] != '') {
_data = row['actions'] ;
}
return _data;
}
},
]
});