I'm using Ignited Datatables to display posts in tabular way in my view. I've been struggling the last 4 days in order to do this working correct, but I can't understand what I'm doing wrong.
This is the functionality that fetches the data
// the Post_model
/**
* Get page data on datatables
*/
public function get_datatable() {
$this->load->library('datatables');
$this->datatables->select('id, title, slug, sort_description, status');
$this->datatables->from('posts');
return $this->datatables->generate();
}
// the posts controller
/**
* List all posts
*/
public function index() {
$this->data['datatables'] = true;
$this->data['data_url'] = 'admin/posts/data_ajax';
// $this->data['posts'] = $this->post_model->get_datatable();
// dump($this->data['pages']);
// load view
$this->load->view('admin/posts/index', $this->data);
}
public function data_ajax() {
$this->output->enable_profiler(false);
echo $this->post_model->get_datatable();
// echo json_encode($this->post_model->get_datatable());
exit();
}
// the view
<table class="table dataTable table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Url Slug</th>
<th>Sort Description</th>
<th>Status</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Title</th>
<th>Url Slug</th>
<th>Sort Description</th>
<th>Status</th>
</tr>
</tfoot>
<!-- <tbody>
<tr>
<td colspan="5" class="dataTables_empty"></td>
</tr>
</tbody> -->
</table>
<?php if(isset($datatables)): ?>
<?php echo js_tag('js/dataTables/jquery.dataTables.min.js'); ?>
<?php echo js_tag('js/dataTables/dataTables.bootstrap.min.js'); ?>
<script type="text/javascript">
$(document).ready(function() {
$('.dataTable').DataTable({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : '<?php echo base_url($data_url); ?>',
'sServerMethod' : 'POST',
'fnServerData' : function (sSource, aoData, fnCallback) {
$.ajax({
dataType : 'json',
type : 'post',
url : sSource,
data : aoData,
success : fnCallback,
"columns": [
{ "data": "id" },
{ "data": "title" },
{ "data": "slug" },
{ "data": "sort_description" },
{ "data": "status" }
]
});
}
});
});
</script>
<?php endif; ?>
So if I access the data_ajax() function from the url like this
localhost/my-blog/admin/posts/data_ajax
and echo echo $this->page->get_datatable(); I can the records
{"draw":0,"recordsTotal":2,"recordsFiltered":2,"data":[{"id":"1","title":"First post","slug":"first-post","sort_description":"This is the first post","status":"visible"},{"id":"2","title":"Second post","slug":"second-post","sort_description":"This is the second post","status":"visible"}]}
but the problem is I can't display them on my screen. In addition I also get this warning in an alert box
DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4
This is a screenshot of what I get
How can I make this work properly? Any help would be appreciated
Please try this
Add Columns parameter to associate data with column
The code will be something like this
$('.dataTable').DataTable({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : '<?php echo base_url($data_url); ?>',
'sServerMethod' : 'POST',
"columns": [
{ "data": "id" },
{ "data": "title" },
{ "data": "slug" },
{ "data": "date_published" },
{ "data": "status" }
]
'fnServerData' : function (sSource, aoData, fnCallback) {
$.ajax({
dataType : 'json',
type : 'post',
url : sSource,
data : aoData,
success : fnCallback,
});
}
});
Please refer datatables column, for details
I had similar problem. This is how I got it solved:
You have to set the columns as this
"columns": [null, null, null, null, null,{}]
Set it separately not in the Ajax.
Related
I use laravel 8 + lumen rest api server and little bit confused because when i consume timestamp and parse it to localize format date on laravel side, also separated the display and timestamp for the js side datatables, the editColumn()it make the filtered search doesn't work because the main of search params is from the timestamp i was set before but on the view i saw it use display side, so how i can make the both filtered(search) and sort work ?
response data :
array:4 [▼
0 => array:6 [▼
"id_role" => 1
"nama_role" => "super.admin"
"created_at" => "2021-11-02T07:32:00.000000Z"
"updated_at" => "2021-11-02T07:32:00.000000Z"
"created_by" => "self"
"updated_by" => "self"
]
.. => ...
]
the column i was sorted was created_at and updated at.
RoleController.php
<?php
public function index(Request $request)
{
$raw = Http::withHeaders(['Authorization' => 'Bearer ' . Cookie::get('access_token')])->get(env('API_URL') . '/v1/kelola-role/role');
$data = $raw->json('data');
$status = $raw->json('status');
if ($request->ajax()) {
if ($status == 'success') {
return DataTables::of($data)
->addIndexColumn()
->editColumn('created_at', function ($e) {
return [
'display' => Carbon::parse($e['created_at'])->format('d/m/Y'),
'timestamp' => Carbon::parse($e['created_at'])
];
})
->editColumn('updated_at', function ($e) {
return [
'display' => Carbon::parse($e['updated_at'])->format('d/m/Y'),
'timestamp' => Carbon::parse($e['updated_at'])
];
})
->make(true);
}
return abort(401);
}
return view('pages.pengaturan.kelola-role');
}
the view :
<table class="table" id="dataRole" style="width:100%">
<thead>
<tr>
<th>No</th>
<th>Nama Role</th>
<th>Dibuat Pada</th>
<th>Diupdate Pada</th>
</tr>
</thead>
<tfoot>
<tr>
<th>No</th>
<th>Nama Role</th>
<th>Dibuat Pada</th>
<th>Diupdate Pada</th>
</tr>
</tfoot>
</table>
the js side :
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
document.addEventListener("DOMContentLoaded", function() {
$("#dataRole").DataTable({
processing: true,
serverSide: true,
ajax: {
type: "GET",
url: "",
dataSrc: function(json) {
barDone();
return json.data;
}
},
columns: [{
data: 'DT_RowIndex',
name: 'DT_RowIndex',
orderable: false,
searchable: false
},
{
data: 'nama_role',
name: 'nama_role'
},
{
name: 'created_at.timestamp',
data: {
_: 'created_at.display',
sort: 'created_at.timestamp'
}
},
{
name: 'updated_at.timestamp',
data: {
_: 'updated_at.display',
sort: 'updated_at.timestamp'
}
},
],
responsive: true,
fixedHeader: true,
select: {
style: "multi"
},
language: {
url: '{{ env('APP_URL') }}/id.json',
processing: "<div id='loadercontainer'><div class='d-flex justify-content-center text-secondary' id='loader'><div class='spinner-border' role='status'><span class='sr-only'>Loading...</span></div></div></div>"
},
dom: '<"row"<"col-12 col-sm-6 py-0"l><"col-12 col-sm-6 py-0 pt-2 pt-sm-0"fr><"col-12"t><"col-12 d-flex justify-content-between"ip>>',
render: function(data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
},
});
});
created_at sort view just work
but when i search it doesn't work
You also need to transform the search on the query level. Try using filterColumn to modify how the package will search on the edited column.
In some cases, we need to implement a custom search for a specific column. To achieve this, you can use filterColumn api.
I fetched data from database with passing generator_id as parameter, I executed query in Model & Controller but how to pass generator_id in jquery to fetch data into data table based on id. can anyone help me?
Example:
When passing generator id 1, in data table should be fetched generator details of id 1.
Thank you so much
My model:
public function getGeneratorRAReport($param,$generator_id){
$arOrder = array('','RA_number');
$this->db->where("RA_status",1);
$this->db->where("generator_id",$generator_id);
if ($param['start'] != 'false' and $param['length'] != 'false') {
$this->db->limit($param['length'],$param['start']);
}
$this->db->select('*,DATE_FORMAT(RA_start_date,\'%d-%m-%Y\') as RA_start_date,DATE_FORMAT(RA_end_date,\'%d-%m-%Y\') as RA_end_date');
$this->db->from('rental_agreement');
$this->db->join('customer','customer_id = customer_id_fk');
$this->db->join('generators','generator_id = generator_id_fk');
$this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
$this->db->order_by('RA_id', 'DESC');
$query = $this->db->get();
$data['data'] = $query->result();
$data['recordsTotal'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
$data['recordsFiltered'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
return $data;
}
public function getGeneratorRAReportTotalCount($param,$generator_id){
$this->db->where("RA_status",1);
$this->db->where("generator_id",$generator_id);
if ($param['start'] != 'false' and $param['length'] != 'false') {
$this->db->limit($param['length'],$param['start']);
}
$this->db->select('*');
$this->db->from('rental_agreement');
$this->db->join('customer','customer_id = customer_id_fk');
$this->db->join('generators','generator_id = generator_id_fk');
$this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
$this->db->order_by('RA_id', 'DESC');
$query = $this->db->get();
return $query->num_rows();
}
My controller:-
public function index()
{
$template['body'] = 'Generators/Generator_RAReport';
$template['script'] = 'Generators/Generator_RAReport_script';
$this->load->view('template', $template);
}
public function get($generator_id){
$this->load->model('Generator_model');
$param['draw'] = (isset($_REQUEST['draw']))?$_REQUEST['draw']:'';
$param['length'] =(isset($_REQUEST['length']))?$_REQUEST['length']:'10';
$param['start'] = (isset($_REQUEST['start']))?$_REQUEST['start']:'0';
$param['order'] = (isset($_REQUEST['order'][0]['column']))?$_REQUEST['order'][0]['column']:'';
$param['dir'] = (isset($_REQUEST['order'][0]['dir']))?$_REQUEST['order'][0]['dir']:'';
$param['searchValue'] =(isset($_REQUEST['search']['value']))?$_REQUEST['search']['value']:'';
$data = $this->Generator_model->getGeneratorRAReport($param,$generator_id);
$json_data = json_encode($data);
echo $json_data;
}
View :-
<div class="box-body table-responsive">
<table id="RA_details_table" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sl No.</th>
<th>RA number</th>
<th>RA type</th>
<th>Customer</th>
<th>RA start date</th>
<th>RA end date</th>
<th>Description</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- /.box-body -->
script :-
$(function () {
var RA_type = {'I':'Inside','O':'Outside'};
$table = $('#RA_details_table').DataTable( {
"searching": false,
"processing": true,
"serverSide": true,
"bDestroy" : true,
dom: 'lBfrtip',
buttons: [
],
"ajax": {
"url": "<?php echo base_url();?>index.php/Generator_RAReport/get/",
"type": "POST",
"data" : function (d) {
}
},
"createdRow": function ( row, data, index ) {
$table.column(0).nodes().each(function(node,index,dt){
$table.cell(node).data(index+1);
});
$('td',row).eq(2).html(RA_type[data['RA_type']]);
},
"columns": [
{ "data": "RA_id", "orderable": false },
{ "data": "RA_number", "orderable": false },
{ "data": "RA_type", "orderable": false },
{ "data": "customer_name", "orderable": false },
{ "data": "RA_start_date", "orderable": false },
{ "data": "RA_end_date", "orderable": false },
{ "data": "RA_description", "orderable": false }
]
} );
in your html area put this tag..
<input type="hidden" id="gen_id" value="<?=$gen_id?>" />
in your js code ...
var id = document.getElementById('gen_id').value;
"ajax": {
"url": "<?php echo base_url();?>index.php/Generator_RAReport/get/"+id,
"type": "GET",
"data" : function (d) {
}
},
in this case you can use both model and clientcode(jquery) in view.
code igniter dosn't force you to use mvc
$ci =& get_instance();
$ci->load->model('some-model');
$ci->some-model->get();
I am trying to get a dataTable on a page called "locations.php" but i am still get a JSON invalid error.
I generate the JSON dynamically using this php code on "get_SR_Locations.php":
$myjson = array();
foreach ($locationList as $list) :
$json= array(
"id" => addslashes($list['id']),
"name" => addslashes($list['name']),
"address" => addslashes($list['address']),
"telephone" => addslashes($list['telephone']),
"emailaddress" => addslashes($list['email'])
);
array_push($myjson, $json);
endforeach;
$mj=array("data"=>$myjson);
echo json_encode($mj);
The generated JSON looks something like this:
{"data":[{"id":"108","name":"Sportpark","address":"Karspstreet 501","telephone":"0123456789","emailaddress":"sport#mail.com"},{"id":"2","name":"Blaashal","address":"Gustavstreet 2920","telephone":"","emailaddress":"sporting#mailing.com"}]}
So when I put this in a JSON validator it says: VALID JSON
But I still get an invalid JSON warning by ajax.
On locations.php I have this code:
HTML
<table id="displayTable" class="table table-striped table-bordered display compact" style="width:100%">
<thead>
<tr>
<th> </th>
<th>Name</th>
<th>Address</th>
<th>Telephone</th>
</tr>
</thead>
<tfoot>
<tr>
<th> </th>
<th>Name</th>
<th>Address</th>
<th>Telephone</th>
</tr>
</tfoot>
</table>
And this JQUERY
function format ( d ) {
// `d` is the original data object for the row
return 'So Far .... NOT so Good';
}
$('#SR_sendForm').click(function(){
$('#displayTable').DataTable(
{
"order": [[ 1, "asc" ]],
"scrollY": $(window).height()-($(window).height()/10),
"scrollX": true,
"ajax": 'pages/get_SR_Locations.php?fdr='+$('#FDR').val()+'&s_term='+$('#searchterm').val()+'&city='+$('#city').val()+'&searchgroup='+$('#searchgroup').val(),
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "address" },
{ "data": "telephone" }
]
}
);
$('#responseTable').show();
}
});
// Add event listener for opening and closing details
$('#responseTable tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
And this is the error I get:
DataTables warning: table id=displayTable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
What is wrong?
Thank you Jeff.... I did what you said ... Add json to the data instead of going via ajax.
So the solution is:
I made an tempDIV to load the data from get_SR_Locations.php
<div class="row" id="tempDIV" style="display: none"></div>
then I load the data and I use the callback for errors etc.
$('#tempDIV').load('pages/get_SR_Locations.php?fdr='+$('#FDR').val()+'&s_term='+$('#searchterm').val()+'&city='+$('#city').val()+'&searchgroup='+$('#searchgroup').val(), function(responseTxt, statusTxt, xhr) {
if(statusTxt == "success"){
$('#displayTable').DataTable(
{
"order": [[ 1, "asc" ]],
"scrollY": $(window).height()-($(window).height()/10),
"scrollX": true,
"data": JSON.parse($.trim($('#tempDIV').text())),
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "address" },
{ "data": "telephone" }
]
}
);
$('#responseTable').show();
}
if(statusTxt == "error"){
$('#responseTable').html("<b>Oops</b><br>Houston we have a problem");
alert("Error: " + xhr.status + ": " + xhr.statusText);
$('#responseTable').show();
}
});
And a last modification in get_SR_Locations.php:
I removed this line:
$mj=array("data"=>$myjson);
$myjson = array();
foreach ($locationList as $list) :
$json= array(
"id" => addslashes($list['id']),
"name" => addslashes($list['name']),
"address" => addslashes($list['address']),
"telephone" => addslashes($list['telephone']),
"emailaddress" => addslashes($list['email'])
);
array_push($myjson, $json);
endforeach;
echo json_encode($myjson);
I have simple table for my tags.
Route
route::resource('admin/tags','TagController');
Controller :
public function index()
{
$tags=Tag::latest()->paginate(5);
return view('products.tag',compact('tags'));
}
View :
#extends('admin.index')
#section('content')
...html code...
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">tag name</th>
<th scope="col">tag slug</th>
<th scope="col">description</th>
</tr>
</thead>
<tbody>
#foreach($tags as $tag)
<tr>
<th scope="row">{{$tag->id}}</th>
<td>{{$tag->tag_name}}</td>
<td>{{$tag->tag_slug}}</td>
<td>#{{$tag->tag_description}}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="paging site-pagination">
{{$tags->links()}}
</div>
...html code...
#endsection
Now, this code work like charm but I want to get data from ajax ,then pass to my view.
My ajax code :
$(document).ready(() => {
get_data()
function get_data() {
$.ajax({
type: 'GET',
url: '/admin/tags',
beforeSend: function () {
console.log('before')
},
success: function (data , status , xhr){
console.log(data);
console.log(status );
console.log(xhr);
},
complete: function (data) {
console.log('complete')
},
error: function (data) {
console.log('error')
},
});
}
)}
Well.now my success function is Work and get my html.
but I just want to get tags variable and pass it to view.
I change my controller like this:
//return view('admin.pages.products.tag',compact('tags'));
return response()->json($tags);
Now my tag view is:
{
"current_page": 1,
"data": [
{
"id": 46,
"tag_name": "EbTLzDikpS",
"tag_slug": "mKXcpVOF3L",
"tag_description": "05HMeVazYO",
},
...
{
"id": 5,
"tag_name": "T4uvmukgZA",
"tag_slug": "fpWwimnnIk",
"tag_description": "A0nfZ4POHR",
}
],
"first_page_url": "http://127.0.0.1:8000/admin/tags?page=1",
"from": 1,
"last_page": 10,
"last_page_url": "http://127.0.0.1:8000/admin/tags?page=10",
"next_page_url": "http://127.0.0.1:8000/admin/tags?page=2",
"path": "http://127.0.0.1:8000/admin/tags",
"per_page": "5",
"prev_page_url": null,
"to": 5,
"total": 46
}
Now even if I delete my ajax function still my view not change.
So i change again my controller :
return view ('admin.pages.products.tag', compact('tags'))->render();
Still not work.
You have forgotten to give the name of function(method) for your Route:
route::resource('admin/tags','TagController#index');
Your code is look like API, but you can use api/admin/tags if you are in route/web.php
If your problem is till, then change the function :
public function index()
{
$tags=Tag::latest()->paginate(5);
return response()->json($tags);
}
I'm trying to create a dataTable of users where each row has a drop-down child row that contains checkboxes of the user privileges. So quite dynamically a 'super' admin can click on users and assign their privileges within the table.
Firstly, not sure if this is a good idea, so feel free to suggest a better way of doing it. Maybe a simple popup modal per row would be easier but for now I've decided this would be a cool way of doing it, so I push on.
Trying to initialise the dataTable with AJAX has me stumped currently however.
PermissionsController.php
public function grid()
{
//retrieve data from models
$data['data'] = collect([ 'admins' => $admins, 'roles' => $roles ]);
return $data;
}
routes.php
Route::get('user-permissions', 'PermissionsController#grid');
permissions.blade
<table class="table table-striped" id="admins_table" >
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
</table>
js
var oTable = $('#admins_table').dataTable( {
"sDom": "<'row'<'col-md-6'l><'col-md-6'f>r>t<'row'<'col-md-12'p i>>",
"aaSorting": [],
"oLanguage": {
"sLengthMenu": "_MENU_ ",
"sInfo": "Showing <b>_START_ to _END_</b> of _TOTAL_ entries"
},
"ajax": {
//here's where I'm trying to grab the data
"url": "http://example.app/user-permissions",
"dataSrc": "data"
},
"columns": [
{ "data": "last_name" },
{ "data": "first_name" },
{ "data": "email" },
{ "data": "phone" }
]
});
Ajax
{
"data":
{
"admins":
[{
"id":31,
"email":"example#gmail.com",
"last_login":"2015-07-27 09:50:50",
"first_name":"Gary",
"last_name":"Barlow",
"roles":[{
"id":1,"slug":"admin"
}]
}],
"roles":
[
{"id":3,"slug":"admin","name":"Admin"},
{"id":7,"slug":"accounts","name":"Accounts"},
{"id":8,"slug":"sales","name":"Sales"},
{"id":9,"slug":"superAdmin","name":"SuperAdmin"}
]
}
}
The "admin" object encompasses all the admins that are passed through and their already assigned roles. These should appear as already ticked within the child-row.
The "roles" object will contain all the current roles available to allow for assignment of additional roles. Basically, this identifies the number of checkboxes that need to appear.
I've abstracted out the rest as what's above pertains to the initialisation. Greatly appreciate any help.
Trying to use AJAX I'm getting nothing but "No data available in table" when if I type in the browser the route path I get the JSON object output.
I'm not sure how I should call the route.
url: '/user-permissions',
dataSrc: 'data.admins',
success: function (data) {
console.log(data);
}
Will the above suffice? I don't really want to be calling the whole url. I even added a success function to try and get a console output of the data but still nothing.
the correct dataSrc reference would be data.admins
you miss a admins[].phone in the sample data?
As I understand you want to show a <select> populated with data.roles, showing the current data.admins[].roles[0].id?
You can do this by collecting data.roles in the dataSrc callback (or in a xhr.dt event) and use a render method for the role column :
<table class="table table-striped" id="admins_table" >
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Phone</th>
<th>role</th>
</tr>
</thead>
</table>
js, only the important
var roles,
var oTable = $('#admins_table').dataTable( {
"ajax": {
"url": "http://example.app/user-permissions",
"dataSrc" : function(json) {
roles = json.data.roles;
return json.data.admins;
}
},
"columns": [
{ "data": "last_name" },
{ "data": "first_name" },
{ "data": "email" },
{ "data": "phone" },
{ "data": "roles",
"render": function(data, type, row) {
var select = '<select>',
role = data[0].id;
for (var i=0;i<roles.length;i++) {
select+='<option value="'+roles[i].id+'"';
if (role == roles[i].id) select+=' selected';
select+='>'+roles[i].name+'</option>';
}
select+='</select>';
return select;
}
}
]
});
produces this table :
with this data :
{ "data": {
"admins": [{
"id":31,
"email":"example#gmail.com",
"last_login":"2015-07-27 09:50:50",
"first_name":"Gary",
"last_name":"Barlow",
"phone" : "123",
"roles":[{
"id":8,"slug":"sales"
}]
},{
"id":32,
"email":"example#yahoo.com",
"last_login":"2015-07-27 09:50:50",
"first_name":"Bary",
"last_name":"Garlow",
"phone" : "321",
"roles":[{
"id":7,"slug":"accounts"
}]
}],
"roles": [
{"id":3,"slug":"admin","name":"Admin"},
{"id":7,"slug":"accounts","name":"Accounts"},
{"id":8,"slug":"sales","name":"Sales"},
{"id":9,"slug":"superAdmin","name":"SuperAdmin"}
]
}
}
It's been awhile now since I asked this question and I thought I'd clarify what I did to solve the problem. First I ensured all the data tables scripts were the latest versions, not sure if this was a problem but I was using some outdated stuff.
Within the tables initialisation the ajax call became simply:
ajax: "/user-permissions"
Most the hard work went into the controller function that was called by the above route:
public function grid()
{
$admins = // get users with relevant roles e.g. ($user->load('roles'))
return Datatables::of($admins)->make(true)
}