I couldn't find any questions like this. All other questions are not using the Bootstrap datatables like me - they built their own table.
My Laravel 5.8 application returns currently a list of users in a searchable datatable. The problem is, that it's returning ALL users at once, so the page loads really slow since the application has a lot of users.
My routes\web.php:
Route::get('/admin/customers', 'Admin\CustomerController#renderPage')->name('admin.customers');
My app\Http\Controllers\Admin\CustomerController.php:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use ConsoleTVs\Charts\Facades\Charts;
use App\User;
class CustomerController extends Controller
{
public function renderPage() {
$customers = User::get();
return view('pages.admin.customers')->with([
'customers' => $customers
]);
}
}
My table in the view resources\views\pages\admin\customers.blade.php gets generated like this (I've removed the not relevant HTML code):
<!-- Bootstrap -->
<link href="/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<!-- Datatables -->
<link rel="stylesheet" href="/css/dataTables.bootstrap.min.css">
<div class="table-responsive">
<table class="table table-condensed table-hover" id="customers-table">
<thead>
<tr>
<th>#</th>
<th>First name</th>
<th>Last Name</th>
<th>Email Address</th>
</tr>
</thead>
<tbody>
#foreach($customers as $customer)
<tr>
<td>{{ $customer->id }}</td>
<td>{{ $customer->first_name }}</td>
<td>{{ $customer->last_name }}</td>
<td>{{ $customer->email }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- Datatables -->
<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.bootstrap.min.js"></script>
<script>
// Datatable settings
$(document).ready(function() {
$('#customers-table').DataTable({
"language": {
"lengthMenu": "Show _MENU_ entires per page",
"search": "Search:",
"decimal": ".",
"thousands": ",",
"zeroRecords": "No entries found.",
"info": "Showing entries _START_ to _END_ of total _TOTAL_",
"infoEmpty": "No entries available.",
"infoFiltered": "(filtered from _MAX_ total entries)",
"paginate": {
"first": "First",
"last": "Last",
"next": "Next",
"previous": "Previous"
}
}
});
} );
</script>
So the question is: What do I need to update to what in order to add support for pagination?
Instead of rendering the html on the server, try to load the DataTable via Ajax.
HTML
<table id="data-table" class="table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline collapsed">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>E-Mail</th>
<th>Action</th>
</tr>
<tfoot></tfoot>
</table>
JavaScript
const table = $('#customer-table').DataTable({
'processing': true,
'serverSide': true,
'ajax': {
'url': 'customers/list',
'type': 'POST'
},
'columns': [
{'data': 'id'},
{'data': 'first_name'},
{'data': 'last_name'},
{'data': 'email'},
{
'orderable': false,
'searchable': false,
'data': null,
'render': function (data, type, row, meta) {
// render custom html
return '<button type="button" class="btn btn-info">Edit</button>';
}
}
],
});
PHP
On the server-side take the POST request parameters and build a dynamic query (with the QueryBuilder).
Then map the result set into a DataTable compatible JSON response:
The controller action
// Build dynamic query
// ...
// Fetch result set
// ...
return response()->json([
'recordsTotal' => $count,
'recordsFiltered' => $count,
'draw' => $draw,
'data' => $rows,
];
More details about the json response: DataTable Server-side processing
public function renderPage() {
$customers = DB::table('users')->paginate(15);
return view('pages.admin.customers')->with([
'customers' => $customers
]);
}
Related
I asked this question twice but didn't get any answer, I have two tables: Country and Region so to get all regions you must select country_id which I did with normal table but how can I do this using ajax
thank you in advance
Model of Region:
public function getRegionsByCountryID($countryID)
{
return $this
->leftJoin('countries', $this->table.'.country_id', '=', 'countries.id')
->select
(
$this->table.'.*',
'countries.name as country_name'
)
->where('country_id','=',$countryID)
->orderBy('id','asc')
->get();
}
Jquery
$.ajax({
url:"{{ route('get-regions') }}",
type: 'GET',
data: {
'country_id': country_id
},
success: function(data){
},
});
});
Controller
public function getData(Request $request)
{
$data = $this->SourceData->getRegionsByCountryID($request->input('country_id'));
return Datatables::of($data)
->addIndexColumn()
->addColumn('Actions', function($data) {
return '....';
})
->rawColumns(['Actions'])
->make(true);
}
Blade
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered datatable">
<thead>
<tr>
<th>Id</th>
<th>No</th>
<th>Name</th>
<th>Country</th>
<th>Avaliable</th>
<th width="150" class="text-center">Action</th>
</tr>
</thead>
</table>
</div>
</div>
I have a web application project where I need to print selected or all the information about that specific person I tried using datatables, it works but it only prints what's inside the table. I need the information about that person which is in another blade.php I was wondering if I there is a code that I could use to link blades using query I'm just new to using laravel
I'm using Laravel framework latest version.
<div id="example_wrapper" class="dataTables_wrapper">
<p class="large"> <span class="glyphicon glyphicon-list"></span> All</p>
#if($joinCount == 0)
<center><p>There's no payslip</p></center>
#else
<table id="exampleid" class="display" style="width:100%">
<thead>
<tr>
<td>ID Number</td>
<td>Name</td>
<td>Department</td>
<td>Cut-off</td>
</tr>
</thead>
<tbody>
#foreach($joinn as $join)
<tr>
<td>{{$join->idnumber}}</td>
<td>{{$join->first_name}} {{$join->middle_name}}
{{$join->surname}}</td>
<td>{{$join->department}}</td>
<td>{{$join->cut_off}}</td>
</tr>
#endforeach
</tbody>
</table>
<script>
$(document).ready(function() {
$('#exampleid').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'print',
text: 'Print all',
exportOptions: {
modifier: {
selected: null
}
}
},
{
extend: 'print',
text: 'Print selected'
}
],
select: true
} );
} );
</script>
I am trying to display json data into php.
Can you tell me how can I sort this into a datatable also with tabs?
as I am going to Display this into each tab as well Like Bitcoin List with Icon and text and ETH list with icon and Text etc
so they can pick what ever coin they like then it shows them the faucet list for that coin
So i need This for each Tab with Datatable
Also yes I looked Google why I would not be posting here
faucetlist.json
[{"name": "Bitcoin",
"shorthand": "BTC",
"faucets": [
{"faucet name":"gr8 Faucet",
"minutes": "5",
"reward": "~3000",
"claim": "http://namefaucet.net"},
//Next Faucet here
]
},
{"name": "ETHcoin",
"shorthand": "ETH",
"faucets": [
{"faucetname":"eth Faucet",
"minutes": "5",
"reward": "~10000",
"claim": "http://ethwebsite.com"},
]
}
]
datatable
<table id="faucetlist" class="table table-bordered table-condensed table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>faucet name</th>
<th>Minutes</th>
<th>reward</th>
<th>Claim</th>
</tr>
</thead>
<tfoot>
</tfoot>
<th>test</th>
<th>test</th>
<th>test</th>
<th>test</th>
</table>
</div>
also javascript here
<script type="text/javascript">
$(document).ready(function() {
$('#faucetlist').DataTable();
} );
</script>
It looks like you did not define your columns or your data.
Assign your JSON to a variable:
var data = {
..faucetlist JSON here
}
Example borrowed from: https://datatables.net/manual/data/
$('#faucetlist').DataTable( {
data: data,
columns: [
{ data: 'column-1' },
{ data: 'column-2' },
{ data: 'column-3' },
{ data: 'column-4' }
]
} );
I purposefully did not fill out the column names.
I've been working on datatables server side processing and slim framework and I always get this error in my browser:
When I check chrome developer tools, I am seeing this in the console:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8000/user/ssp-data.php?draw=1&columns%5B0%5D%5Bdata%5D=0&c…art=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1440509303609
My html code is here:
<table id="dataTableUsers" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</tfoot>
</table>
My script:
$(document).ready(function() {
$('#dataTableUsers').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "ssp-data.php"
} );
} );
ssp.data.php
http://pastebin.com/iBnWgAHd
I successfully used datatables but not server side processing. It loads 1000+ rows for about 5 seconds and I don't want my client to wait every time like that. I tried searching and found that datatables server side processing can be helpful. What have I done wrong with my code? Thank you in advance.
Using slims Framework 3, you need to have 2 routes for this, the first one:
$app->get('/datatable-example',function(){
... // your html code goes here
});
is for rendering the HTML page, the '/datatable-example' could be changed into whatever you want. The second route:
$app->get('/datatable-data',function(){
... // your server-side handler goes here
});
is for returning the data. The 'datatable-data' could be changed but it must be consistent with the JS code below:
$(document).ready(function() {
$('#dataTableUsers').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "datatable-data"
});
});
notice that the value of "ajax" is also "datatable-data", the same as the second route.
I'll copy the whole example below, but it's kinda dirty and hacky, but it should work when you copy to your route file:
$app->get('/datatable-example',function(){
return
'
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script></head>
<head><script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script></head>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
<table id="dataTableUsers" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$("#dataTableUsers").dataTable( {
"processing": true,
"serverSide": true,
"ajax": "/datatable-data"
} );
} );
</script>
';
});
$app->get('/datatable-data',function(){
return
'{
"draw": 1,
"recordsTotal": 2,
"recordsFiltered": 2,
"data": [
["1", "Nick" ,"nick#mail.com"],["2", "John" ,"john#mail.com"]
]
}';
});
hope it helps.
I am trying to using datatables Jquery plugin in codeingiter but its not working please help me to debug
Heres my controller where I am calling my view:
public function show_all_merchants()
{
$data["query"]= $this->details_model->get_mids();
$this->load->view('show_merchants_view',$data);
}
Heres my view:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function()
{
$('#example').dataTable
({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : 'show_merchant_details',
'iDisplayStart' : 0,
'fnServerData': function(sSource, aoData, fnCallback)
{
$.ajax
({
'dataType': 'json',
'type' : 'POST',
'url' : sSource,
'data' : aoData,
'success' : fnCallback,
'cache' : false
});
}
});
});
</script>
<table cellpadding="0" cellspacing="0" border="0" id="example">
<thead>
<tr>
<th width="20%">ID</th>
<th width="25%">First Name</th>
<th width="15%">Last Name</th>
<th width="25%">Email</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">Loading data from server</td>
</tr>
</tbody>
</table>
and heres my controller where I am loading the data Here I am loading data tables library:
function show_merchant_details()
{
$this->datatables
->select('mid, merchant_name, merchant_link,merchant_contact')
->from('merchants')
->add_column('edit', 'Edit', 'id')
->add_column('delete', 'Delete', 'id');
echo $this->datatables->generate();
}
Friends show_merchant_details() is wrking properly as i am able to see the output
In the view page I am able to see it like Loading data from server ...... (dats it)