I am trying to remove the last action row when printing my webpage that uses a jQuery datatable. I am using datatable for showing data and that works, but when I try to print the default print option, it prints the edit/ delete part.
Here is my table :
<table id="dataTable" class="table table-bordered text-center">
<thead>
<tr>
<th width="5%">SN</th>
<th>Customer Code</th>
<th>Customer Name</th>
<th>customer address</th>
<th>customer phone</th>
<th>customer city</th>
<th>customer email</th>
<th>openig due</th>
<th>customer status</th>
<th width="8%" class="text-center">Action</th>
</tr>
</thead>
<tbody>
@php $i=1; @endphp
@foreach($customer as $row)
<tr>
<td>{{$i++}}</td>
<td>{{$row->customer_code}}</td>
<td>{{$row->customer_name}}</td>
<td>{{$row->customer_address}}</td>
<td>{{$row->customer_phone}}</td>
<td>{{$row->customer_city}}</td>
<td>{{$row->customer_email}}</td>
<td>{{$row->openig_due}}</td>
<td>
@if($row->customer_status==1)
<a href="#" class="btn btn-sm btn-success">active</a>
@else
<a href="#" class="btn btn-sm btn-danger">deactive</a>
@endif
</td>
<td >
<a href="{{url('admin/customerEdit/'.$row->id)}}" class="btn btn-warning btn-sm waves-effect waves-light">
<i class="fa fa-edit"></i> <span>
Edit
</span>
</a>
<a href="{{url('admin/customerdlt/'.$row->id)}}" class="btn btn-danger btn-sm waves-effect waves-light" id="delete"> X
<i class="fa fa-delete"></i> <span>
Delete
</span>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
and Here is my jquery for data table
$("#dataTable").DataTable({
processing: true,
serverSide: true,
aaSorting: [],
columnDefs: [ { orderable: false, targets: [0,-1] }],
ajax: dataUrl,
buttons: {
buttons: [
{ extend: 'excel', className: 'btn-outline-dark' },
{ extend: 'print', className: 'btn-outline-dark'}
]
}
}).buttons().container().appendTo('#dataTable_wrapper .col-md-6:eq(0)');
How can I remove last row when I click the print button?
3
Answers
You can use CSS to hide the last cell of the row along the
@media
statement to perform that action only when the page is printed. Try this;You can hide elements when printing with CSS:
Now add the class to the buttons and other elements that you don’t want to print.
Another thing you can use the
$loop->iteration
value instead of the$i
variable. Laravel docsYou can do this in the javascript when declaring your buttons https://datatables.net/extensions/buttons/
https://datatables.net/reference/option/buttons.buttons.extend#Description
https://datatables.net/reference/button/
https://datatables.net/extensions/buttons/examples/html5/columns.html
Full example