I need to hide/show action columns based 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 ='
<i class="fas fa-pen-square"></i>
';
} 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'}
]
});
});
my expected code in the datatable is a condition that filters action column data
Hi Abdir Abdirahim Ali,
There is a visible property which accept boolean.
https://datatables.net/reference/option/columns.visible
Here is a example for you.
Your frontend:
let showActionColumn= "<?= $showActionColumn ?>";
columns : [
{data : 'firstname'},
{data : 'middlename'},
{data : 'lastname'},
{data : 'username'},
{data : 'mobileno'},
{data : 'statusname'},
{data : 'groupname'},
{data : 'actions', visible : showActionColumn}
]
Your backend:
$data['showActionColumn'] = ChechPermission($this->session->userdata('permissions'), "users", "edit")
Using columnDefs can be useful for your case :
$('#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;
}
},
]
});
Related
Here is what I'm trying to do. I'm trying to export all of the data in a datatable regardless of the amount displayed on the page.
Based on the link: https://datatables.net/forums/discussion/48044/export-all-regardless-of-pagination
I tried to do the following:
but guess what I'm only able to export the csv file with only a certain amount of records.
var datatableFilters_{$this->id} = ".App_Json::encode($this->strictFilters).";
var datatablePassthru_{$this->id} = ".App_Json::encode($this->passthruVariables).";
$( document ).ready(function() {
var datatable = $('#{$this->id}').DataTable(
{
".(($this->allowExporting) ? "dom: 'Bfrtip', buttons: [ { extend:'copy',exportOptions: {modifier: {page: 'all', search: 'none' } }, }, { extend:'csv',exportOptions: {modifier: {page: 'all', search: 'none', serverSide: 'false' } }, }, { extend:'excel',exportOptions: {modifier: {page: 'all', search: 'none' } }, }, { extend:'pdf',exportOptions: {modifier: {page: 'all', search: 'none' } }, }, { extend:'print',exportOptions: {modifier: {page: 'all', search: 'none' } }, } ], " : '')."
".(($this->showRecordsPerPageAtBottom) ? "sDom: 'Rfrtlip'," : '')."
serverSide: true,
responsive: true,
pageLength: {$this->recordsPerPage},
searching: ".(($this->allowSearch) ? 'true' : 'false').",
paging: ".(($this->allowPagination) ? 'true' : 'false').",
ordering: ".(($this->allowSorting) ? 'true' : 'false').",
info: ".(($this->showRecordCount) ? 'true' : 'false').",
language: {
search: '_INPUT_',
searchPlaceholder: 'Search...'
},
$order
ajax: {
url: 'ajax.php',
type: 'POST',
data: function(d) {
d.ajaxAction = 'loadAjaxClassAction',
d.className = 'Browser_DataTable',
d.classFunction = 'ajaxProcess',
d.aryFunctionAttributes = jsonEncode([
'".get_called_class()."',
jsonEncode(datatableFilters_{$this->id}),
jsonEncode(datatablePassthru_{$this->id})
])
}
}
}
);
Is there a way to export all records?
Hi I'm working with Datatable. I am populating it using PHP Codeigniter and AJAX request. The data is loading fine but the search function of dataTable is not working.
Here's the ajax call so far
function get_card_info(id){
if(id != ''){
$.ajax({
'url' : '<?php echo base_url('getter/get_card_info/'); ?>'+id,
'type' : 'POST',
'data' : {'id' : id},
'cache' : false,
'success' : function(data){
if ($.trim(data)){
var json = JSON.parse(data);
var suffix = json.info[0].Citizen_Suffix !== null ? json.info[0].Citizen_Suffix : '';
var currentBarangay = json.currentAddress[0].Barangay_Name != 'Others' ? ', '+json.currentAddress[0].Barangay_Name : '';
$('#card_number').text(json.info[0].card_id);
$('#fullname').text(json.info[0].Citizen_Title+' '+json.info[0].Citizen_LastName+', '+json.info[0].Citizen_FirstName+' '+json.info[0].Citizen_MiddleName+' '+suffix);
$('#address').text(json.currentAddress[0].Address_Name+currentBarangay);
//Image
var url = '<?php echo base_url('resources/images/citizen_photo/'); ?>'+id+'.jpeg';
$('#photo').attr('src', url);
$('#dt-services').DataTable( {
"ajax": '<?php echo base_url('getter/get_merchant_services/'); ?>'+id,
"destroy": true,
"paging": false,
"columnDefs": [{
'targets': 0,
'className': 'dt-body-center',
'render': function (data, type, full, meta){
return '<input type="checkbox" '+(data == 'true' ? 'checked' : '')+'>' ;
}
}],
"columns" :[
{data: ""},
{data: "card_id"},
{data: "Citizen_FirstName"}
]
} );
}
else{
ShowAlert('Error', 'Card Not Found', 'danger');
}
},
'error': function(request, error){
alert(error);
}
});
}
else{
ShowAlert('Error', 'Please enter card number', 'warning');
}
};
Also, i'm trying to put checkbox so I can get the selected values. Any guide on this? Thanks
Working a Project here and I need some help. I have a php page and an html page, trying to pass the username of the php into a DataTable that is in my html file. The code that I have for each is:
PHP is below:
public function __construct(Guard $auth)if (Auth::check() ||Auth::attempt()) {
$auth_id = Auth::user()->id;}
My HTML portion is the following:
var table = $('#example').DataTable( {
// Makes one continuous line
"autoWidth": false,
// How many rows to return
"pageLength": 25,
// Setup the search box with the current username to filter the values on the screen - jsg 2/12/2016
"search": {
"search": $('auth')
}, dom: "Bfrtip",
ajax: "../php/staff.php",
columns: [
{
data: null,
defaultContent: '',
className: 'select-checkbox',
orderable: false
},
{ data: "instructor", visible: false },
{ data: "first_name" },
{ data: "last_name" },
{ data: "category" },
{ data: "Metric_text" },
{ data: "response_value" },
{ data: "fkey_course_id", visible: false },
{ data: "course_code" },
{ data: "course_number" },
{ data: "course_section"}
],
My Question is how can I get $auth_id into the search portion of the HTML coding. As you can see I've tried this with $('auth') but its giving me an object error. If I trying a name like "search": "Test" then test populates and it works. Basically I want to pass the username into the search box of the DataTable so it ill show only the usernames rows.
you can use Ajax.
The code looks like this:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/ids-objects.php",
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "start_date" },
{ "data": "salary" }
]
} );
} );
Look at this sample.
Hope it can help you.
I'm trying to use DataTables with php and mysql. I have an ajax call that is pulling in data as such:
[
{
"id": 3,
"ptid":"blah",
"last_name":"blah",
"first_name":"blah",
"priv_application":"E",
"priv_document":"E",
"priv_note":"E",
}
]
I'm configuring DataTables with the following:
$('#listing').DataTable( {
"paging": false,
"searching": false,
"select": true,
ajax: {
url: '{{ url("administration/admindata") }}',
dataSrc: ''
},
columns: [
{ title: "ID" },
{ title: "PtID" },
{ title: "Last Name" },
{ title: "First Name" },
{ title: "Application" },
{ title: "Documents" },
{ title: "Notes" }
]
});
And the HTML for the table is as follows:
<table id="listing" class="display" width="100%"></table>
However, it doesn't want to load the data even though this configuration is exactly like the first example given here:
https://datatables.net/manual/ajax
I'm getting the following error:
DataTables warning: table id=listing - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
Any assistance is greatly appreciated.
When using array of objects as your data source, you need to specify data source for each column using columns.data option.
$('#listing').DataTable( {
"paging": false,
"searching": false,
"select": true,
"ajax": {
"url": '{{ url("administration/admindata") }}',
"dataSrc": ''
},
columns: [
{ data: "id", title: "ID" },
{ data: "ptid", title: "PtID" },
{ data: "last_name", title: "Last Name" },
{ data: "first_name", title: "First Name" },
{ data: "priv_application", title: "Application" },
{ data: "priv_adocument", title: "Documents" },
{ data: "priv_note", title: "Notes" }
]
});
Try something like this.
ajax": {
type': 'POST',
'url': "<?=action('TestController#postTestfunction')?>"
},
try to use the ajax call without blade template code.
and make sure your route is written something like this.
Route::controller('test', 'TestController');
Also try with change your controller function name "admindata" to "postAdmindata"
Let me know still if you get stuck anywhere.
Somebody would helpme with this code:Grid dont load infomations.
Below is the code I'm using, but the grid carries no information.
Extjs
Ext.onReady(function(){
var store = new Ext.data.JsonStore({
// store configs
storeId: 'myStore',
proxy: {
type: 'ajax',
url: 'data.php',
reader: {
type: 'json',
root: 'country',
idProperty: 'total'
}
},
//alternatively, a Ext.data.Model name can be given (see Ext.data.Store for an example)
fields: ['name', 'area']
});
Ext.create('Ext.grid.Panel', {
title: 'Retorno',
//store: Ext.data.StoreManager.lookup('simpsonsStore'),
store:store,
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Area', dataIndex: 'area', flex: 1 }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
});
data.php here is the code with json code.
<?php
print '{
"total": 10,
"country": [
{
"name": "CULTIV",
"area": "6.96120082466223e-007"
},
{
"name": "asdASdasd",
"area": "123123123"
}
]
}';
?>
I think you need to set the store's autoLoad configuration to true. If you do not set this attribute, then you will need to call the load() method of the store.
Option 1
var store = new Ext.data.JsonStore({
// store configs
storeId: 'myStore',
autoLoad:true,
proxy: {
type: 'ajax',
url: 'data.php',
reader: {
type: 'json',
root: 'country'
//idProperty: 'total'
}
},
//alternatively, a Ext.data.Model name can be given (see Ext.data.Store for an example)
fields: ['name', 'area']
});
Option 2
var store = new Ext.data.JsonStore({
// store configs
storeId: 'myStore',
proxy: {
type: 'ajax',
url: 'data.php',
reader: {
type: 'json',
root: 'country'
//idProperty: 'total'
}
},
//alternatively, a Ext.data.Model name can be given (see Ext.data.Store for an example)
fields: ['name', 'area']
});
store.load();
I created a working fiddle for a demonstration.