CodeIgniter4 DataTables Usage - Hide Column
Table Customers
Name | Phone | City | Country | Postal Code | Action |
---|
Note
This sample database is downloaded from : https://www.mysqltutorial.org/mysql-sample-database.aspx/
Controller
hide()
method is for hidding column will not show as result.
Sometimes we still need querying column to add/modify for extra column but dont want to show them as result.
use \Hermawan\DataTables\DataTable;
$db = db_connect();
$builder = $db->table('customers')->select('customerNumber, customerName, phone, city, country, postalCode');
return DataTable::of($builder)
->add(null, function($row){
return '<button type="button" class="btn btn-primary btn-sm" onclick="alert(\'edit customer number: '.$row->customerNumber.'\')"><i class="fas fa-edit"></i> Edit</button>';
})
->hide('customerNumber')
->toJson();
Javascript
$(document).ready(function() {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: '/ajax-datatable/hide-column',
columnDefs: [
{ targets: -1, orderable: false}, //target -1 means last column
]
});
});
AJAX Response :
{
"draw": "1",
"recordsTotal": 122,
"recordsFiltered": 122,
"data": [
[
"Alpha Cognac",
"61.77.6555",
"Toulouse",
"France",
"31000",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Alpha Cognac')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"American Souvenirs Inc",
"2035557845",
"New Haven",
"USA",
"97823",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: American Souvenirs Inc')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Amica Models & Co.",
"011-4988555",
"Torino",
"Italy",
"10100",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Amica Models & Co.')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"ANG Resellers",
"(91) 745 6555",
"Madrid",
"Spain",
"28001",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: ANG Resellers')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Anna's Decorations, Ltd",
"02 9936 8555",
"North Sydney",
"Australia",
"2060",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Anna's Decorations, Ltd')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Anton Designs, Ltd.",
"+34 913 728555",
"Madrid",
"Spain",
"28023",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Anton Designs, Ltd.')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Asian Shopping Network, Co",
"+612 9411 1555",
"Singapore",
"Singapore",
"038988",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Asian Shopping Network, Co')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Asian Treasures, Inc.",
"2967 555",
"Cork",
"Ireland",
null,
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Asian Treasures, Inc.')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Atelier graphique",
"40.32.2555",
"Nantes",
"France",
"44000",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Atelier graphique')\"><i class=\"fas fa-edit\"></i> Edit</button>"
],
[
"Australian Collectables, Ltd",
"61-9-3844-6555",
"Glen Waverly",
"Australia",
"3150",
"<button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"alert('edit customer: Australian Collectables, Ltd')\"><i class=\"fas fa-edit\"></i> Edit</button>"
]
]
}