hiding/showing actions column according to permission
hiding/showing actions column according to permission
Hey all
for the last days I have a problem about using datatable, I need to hide/show action columns base on permissions
if action column is null I want to hide action column, if action has data i want to show that data passes on backend side.
// 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'}
]
});
});
Answers
Are you saying that all rows in the action column will either have buttons or will be empty?
If so, in
initCompleteyou can get the returned JSON object (second parameter) and interrogate that to see if the action column is empty. You can check just the first row element assuming all are empty. Then usecolumn().visible()to hide the column, for example:Kevin
@kthorngren could you make more details because I added that and nothing happens
What exactly did you try?
Kevin
my new datatable code is
That doesn't hide the actions column? Strange.
Here is a simple demo with two data sources; one with buttons in the action column and one without. Since there is not an
ajax optionthere won't be ajsonvalue to use ininitComplete. It uses thedatavariable sent to the function based on the button clicked.http://live.datatables.net/quwohemi/1/edit
Kevin
@kthorngren
still same although I did with ajax and without ajax
thanks in advance you work hard with me
Remove the
data: data. You can't have two data sources. Use thejsonparameter of theinitCompletefunction. Use the browser's debugger or console.log to output thejsonvariable so you can see the structure and the response data. Maybe something like this since you havedataSrc : "":Kevin
Also you don't need
destroy: true,. That was needed for the demo to work.Kevin