I found a problem regarding DataTables. Previously I added Headers to DataTables Manually, like so:
<div class="table-responsive">
<table class="table table-bordered" id="example" style="width:100%;">
<thead>
<tr>
<th>Header1</th>
<th>Header2</th>
<th>Header3</th>
<th>Header4</th>
</tr>
</thead>
</table>
</div>
For that it doesn't matter. But when I add the header name like this (via database), like this :
<div class="table-responsive">
<table class="table table-bordered" id="example" style="width:100%;">
<thead>
<? foreach ($kolom as $row) : ?>
<th><? echo $row ?></th>
<? endforeach; ?>
</thead>
</table>
</div>
The result will create 2 stacked headers like so:
| header1 | header2 | header3 | header4 |
| header1 | header2 | header3 | header4 |
The value from $kolom is an Array like this :
Array ( [0] => Header1 [1] => Header2 [2] => Header3 [3] => Header4 )
This is the script from jQuery i use :
<script>
$(document).ready(function() {
var dataTable;
dataTable = $('#example').DataTable({
"columnDefs": [{
"defaultContent": "-",
"targets": "_all"
}],
"aoColumns": [
{data : "f1"},
{data : "f2"},
{data : "f3"},
{data : "f4"}
],
"scrollCollapse": true,
"fixedColumns": {
"left": 1
},
'processing': true,
'language': {
"loadingRecords": ' ',
"processing" : '<i class="fa fa-spinner fa-spin" style="font-size:24px;color:rgb(75, 183, 245);"></i>'
},
"scrollX": true,
});
});
</script>
I only want to show 1 header only.
Which error did I experience and which part should I fix?
Thanks, I hope I can find a solution here.
Related
I am making a table of product with their respective subgroup with datatable.
I am trying to make the "group" to be like a link. When user click it, it will redirect to a page of the product group and all the product under the group.
this is my code.
var prod_table = $('#product_table').DataTable({
"bProcessing": true,
"serverSide": true,
"pagingType": "first_last_numbers",
responsive: true,
stateSave: true,
"lengthMenu": [
[10, 25, 50, 100],
[10, 25, 50, 100]
],
language: {
search: "_INPUT_",
searchPlaceholder: "Search product here..",
},
"order": [
[0, "desc"]
],
"ajax": {
url: "php/get_product.php",
type: "post"
}
});
<table class="table table-striped" id="product_table" cellspacing="0" width="100%">
<thead>
<tr>
<th><b>Product Code</b></th>
<th><b>Product Name</b></th>
<th><b>Manufacturer</b></th>
<th><b>Group</b></th>
<th><b>Price</b></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
My current table
Product Code
Product Name
Manufacturer
Group
Price
ABC123
teser 1
test inc
Hardware
$45.00
DEF456
tester 2
test2 pte ltd
Software
$120.00
Expected outcome
Product Code
Product Name
Manufacturer
Group
Price
ABC123
teser 1
test inc
Hardware
$45.00
DEF456
tester 2
test2 pte ltd
Software
$120.00
Thanks!!
Jeff
Try adding a <span> <a href> </a> </span> to your <td> element.
<td> <span>Hardware</span></td>
Try following code.
<td>
About
</td>
I'm creating a live report, I have data within the datatables, and I need to add additional data that is not relevant to the actual data table.
Controller
$response = json_decode($output,true);
if(!isset($response['error'])){
/**
* Key has access to AA permissions!
*/
if($request->ajax()) {
$data = Attacks::groupBy('attacker_name')
->selectRaw('count(*) as grand_total, attacker_name')
->selectRaw('count(case result when ? then 1 else null end) as hosp',['Hospitalized'])
->selectRaw('count(case result when ? then 1 else null end) as mug',['Mugged'])
->selectRaw('count(case result when ? then 1 else null end) as attack',['Attacked'])
->selectRaw('count(case result when ? then 1 else null end) as arrested',['Arrested'])
->selectRaw('count(case result when ? then 1 else null end) as lost',['Lost'])
->selectRaw('count(case result when ? then 1 else null end) as assist',['Assist'])
->selectRaw('count(case when war > 1 then 1 else null end) as wall')
->selectRaw('ROUND(SUM(respect_gain),2) as respect')
->selectRaw('ROUND(SUM(respect_loss),2) as respectlost')
->where('attacker_faction', 478)
->where('timestamp_ended', '>', $response['chain']['start'])
->orderBy('grand_total', 'DESC')
->get();
//$data['chain'] = $response;
return datatables()
->of($data)
->addIndexColumn()
->toJson();
}
//return view('chain', ['chain' => $response['chain']]);
}
Routes
Route::get('chain/list', [AttacksController::class, 'chaining'])->middleware(['auth'])->name('chain.list');
Route::get('chain', function () {
return view('chain');
})->middleware(['auth'])->name('chain');
Blade
<div class="mb-2">
<div class="w-full rounded shadow-xs">
<div id='chainInfo' class="p-0 rounded shadow bg-gray-600">
#php
//var_dump($chain);
#endphp
</div>
</div>
</div>
<div class="mb-2">
<div class="w-full rounded shadow-xs">
<div id='recipients' class="p-0 rounded shadow bg-gray-600">
<table class="stripe hover bg-gray-600 text-sm w-full" id="chaining">
<thead>
<tr class="text-xs font-semibold tracking-wide text-left text-white border-b dark:border-gray-700 bg-gray-500 dark:text-gray-400 dark:bg-gray-800">
<th class="px-4 py-3">#</th>
<th class="px-4 py-3">User</th>
<th class="px-4 py-3">Grand Total</th>
<th class="px-4 py-3">Total</th>
<th class="px-4 py-3">Lost</th>
<th class="px-4 py-3">Hosps</th>
<th class="px-4 py-3">Mugs</th>
<th class="px-4 py-3">Arrested</th>
<th class="px-4 py-3">Attacked</th>
<th class="px-4 py-3">Assists</th>
<th class="px-4 py-3">Respect gain</th>
<th class="px-4 py-3">Respect loss</th>
<th class="px-4 py-3">Wall</th>
</tr>
</thead>
<tfoot>
<tr class="text-xs font-semibold tracking-wide text-left text-white border-b dark:border-gray-700 bg-gray-500 dark:text-gray-400 dark:bg-gray-800">
<th colspan="2" style="text-align:right">Total:</th>
<th></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
var table = $('#chaining').DataTable({
serverSide: true,
processing: true,
ajax: "{{ route('chain.list') }}",
"order": [[3, 'desc']],
createdRow: function ( row, data, index ) {
$(row).addClass('text-xs')
},
columnDefs: [
{ className: "text-right", targets: "_all" },
],
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex'},
{data: 'attacker_name', name: 'attacker name'},
{data: 'grand_total', name: 'gtotal'},
{
data: null, // this will allow usage of full object
render: function ( data, type, full, meta ) {
return (data.hosp + data.attack + data.mug + data.arrested);
},
name: 'total'
},
{data: 'lost', name: 'lost'},
{data: 'hosp', name: 'hosp'},
{data: 'mug', name: 'mug'},
{data: 'arrested', name: 'arrested'},
{data: 'attack', name: 'attacked'},
{data: 'assist', name: 'assists'},
{data: 'respect', name: 'respect'},
{data: 'respectlost', name: 'respect lost'},
{data: 'wall', name: 'wall'},
],
footerCallback: function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 3 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 2 ).footer() ).html(
total
);
},
paging : false,
lengthMenu: false,
searching: false,
info: false
}).on( 'order.dt search.dt', function () {
table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
cell.innerHTML = i+1;
} );
} ).columns.adjust();
setInterval(function () {
table.ajax.reload();
console.log('table reloaded')
}, 10000);
});
</script>
I want to be able to access $response within my blade, as well as sending data to the datatables! the response does not contain the same data as $data!
I think you can use the view compose feature in the Laravel and send extra information to the view.
You need to register a composer in the view service provider.for more information check the below page
https://laravel.com/docs/8.x/views#view-composers
My page looks like this:
I want to add scrollX when page is too small so I must add line
scrollX: true
When I add page looks like this:
DataPicker is behind rows and line of column is not in this same line but scrollX work(look picture). When I write in input and ajax load data that line of column works fine but datapicker still not work. How can I repair this?
Code _table.blade.php
<table class="table table-striped table-bordered" id="policies-table" style="width: 100%;">
<thead>
<tr>
<th>Numer polisy</th>
<th>Produkt</th>
<th>Data wystawienia</th>
<th>Wartość</th>
<th>Składka</th>
<th>Wystawiający</th>
<th>Akcje</th>
</tr>
</thead>
</table>
Ajax load:
$('#policies-table').DataTable({
ajax: {url:"url",
type: "POST",
'headers': {'X-CSRF-TOKEN': '{{ csrf_token() }}'}},
processing: true,
serverSide: true,
autoWidth: true,
scrollX: true,
scrollCollapse: true,
jQueryUI: true,
order: [[ 2, "desc" ]],
........
});
Try this $('#policies-table').DataTable({responsive: true} );
Or If you are using bootstrap, you cant try this
<div class='table-responsive'>
<table class='your classes'>
</table>
</div>
I have a table in a database which outputs this JSON:
{"sEcho":1,
"iTotalRecords":7,
"iTotalDisplayRecords":7,
"aaData":[
{"FormID":"6",
"FormName":"Configuration",
"FormPath":"#","FormCIPath":"#"},
{"FormID":"1",
"FormName":"Dashboard",
"FormPath":"#",
"FormCIPath":"admin\/dashboard\/System"}],
"sColumns":"FormName,FormPath,FormCIPath"}
But I have not given any columns in the datatables a FormID, as I don't want to show the FormID of database to public.
But how can I get the FormID?
Here is the Javascript:
<script>
$(document).ready(function() {
var oTable = $('#ManageForms').dataTable({
"bServerSide":true,
"bProcessing":true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bFilter":true,
"sServerMethod": "POST",
"sAjaxSource": "{{base_url()}}admin/configurations/listForms_DT/",
"iDisplayLength": 2,
"aLengthMenu": [[2, 25, 50, -1], [2, 25, 50, "All"]],
"sEcho": 1,
"columns":[
{data:"FormName"},
{data:"FormPath"},
{data:"FormCIPath"},
{ "data": null,
"defaultContent": "<a href='#editBtnModal' class='editBtnFunc' ><i style='color: #666666' class='fa fa-pencil fa-fw fa-2x'></i></a><a href='#' id='deleteBtn'><i style='color: #ff0000' class='fa fa-times fa-fw fa-2x'></i></a>",
"targets": -1
}
],
'fnServerData' : function(sSource, aoData, fnCallback){
$.ajax ({
'dataType': 'json',
'type' : 'POST',
'url' : sSource,
'data' : aoData,
'success' : fnCallback
}); //end of ajax
}
});
//Edit Button on Modal Window
$('#ManageForms').on('click', '.editBtnFunc', function(e){
e.preventDefault();
//var aPos = oTable.fnGetPosition( this );
//var aData = oTable.fnGetData( aPos[0] );
console.log(oTable);
});
//Edit Button
} );
</script>
Here is the HTML
<div class="row ui-sortable">
<div class="col-lg-12">
<div class="box">
<header>
<div class="icons">
<i class="fa fa-table"></i>
</div>
<h5>Manage Forms</h5>
</header>
<div class="body" id="collapse4">
<table id="ManageForms" class="table table-bordered table-condensed table-hover table-striped">
<thead>
<tr>
<th>Form Name</th>
<th>Form Path</th>
<th>Form CI Path</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
In order to get the fnGetPosition(this), you should click either TR, TD or TH in the table's body. You can find closest td or tr of ".editBtnFunc" tag and get position of row.
$('#ManageForms').on('click', '.editBtnFunc', function(e){
var row = $(this).closest('tr');
var aPos = oTable.fnGetPosition( row );
var aData = oTable.fnGetData( aPos[0] );
});
I have a datatable that I'm using in server-side mode to retrieve data via AJAX. On the surface everything appears correct however, when I load the page I get "No Matching Records Found" displayed instead of the data displaying. The peculiar part is that it says it is showing the records "Showing records 1 to 2 of 2 entries".
The table HTML and JS is as follows:
<div class="container">
<script type="text/javascript">
$(document).ready(function() {
$("#freebieslist").dataTable({
"bServerSide": true,
"sAjaxSource": "/config/getadvertisers",
"bPaginate": true,
"bProcessing": true,
"bFilter" : true,
"sPaginationType" : "bootstrap",
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>"
});
$.extend( $.fn.dataTableExt.oStdClasses, {
"sWrapper": "dataTables_wrapper form-inline"
} );
});
</script>
<div class="row"><h1 class="pull-left">Advertisers</h1><div class="pull-right" style="margin-top:15px;">Add New Delete</div> </div>
<div class="row">
<div class="span12">
<table id="freebieslist" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="span1"> </th><th>Advertiser</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div> </div>
The JSON being loaded is this:
{
"sEcho": "1",
"iTotalRecords": "2",
"iTotalDisplayRecords": "2",
"aaData": [
[
"2",
"Test2"
],
[
"1",
"Tester"
]
]
}
How to fix this so it actually displays the returned rows?
I solved the issue by using datatables debugger in Chrome. I checked the AJAX result coming back (based on the get paramaters datatables was passing in) and found it was getting a null result set. A quick change in the server side code and things started working properly.