CodeIgniter4 DataTables Usage - Edit / Format Column
Table Customers
Cust. No | Name | Phone | Country | Credit Limit |
---|
Note
This sample database is downloaded from : https://www.mysqltutorial.org/mysql-sample-database.aspx/
Controller
Call method
The First argument is
The second argument is anonymous function (Closure) to set return for column value. Anonymous function itself has
method
The different is on anonymous function has
$meta added since version
edit()
for edit column value.
The First argument is
column name
.
The second argument is anonymous function (Closure) to set return for column value. Anonymous function itself has
$row, $meta
argument.
method
format()
is similar with edit()
.
The different is on anonymous function has
$value, $meta
argument
$meta added since version
0.7
use \Hermawan\DataTables\DataTable;
public function edit_column()
{
$db = db_connect();
$builder = $db->table('customers')->select('customerNumber, customerName, phone, country, creditLimit');
return DataTable::of($builder)
->edit('customerNumber', function($row, $meta){
return '<button type="button" class="btn btn-info btn-sm" onclick="alert(\''.$meta['alias'] .' :' . $row->customerNumber.'\')"><i class="fas fa-info"></i> Info</button>';
})
->format('creditLimit', function($value, $meta){
return '$ '.number_format($value, 2,'.',',');
})
->toJson();
}
Javascript
$(document).ready(function() {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: '/ajax-datatable/edit-column',
});
});
AJAX Response :
{
"draw": "1",
"recordsTotal": 122,
"recordsFiltered": 122,
"data": [
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 103')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Atelier graphique",
"40.32.2555",
"France",
"$ 21,000.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 112')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Signal Gift Stores",
"7025551838",
"USA",
"$ 71,800.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 114')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Australian Collectors, Co.",
"03 9520 4555",
"Australia",
"$ 117,300.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 119')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"La Rochelle Gifts",
"40.67.8555",
"France",
"$ 118,200.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 121')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Baane Mini Imports",
"07-98 9555",
"Norway",
"$ 81,700.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 124')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Mini Gifts Distributors Ltd.",
"4155551450",
"USA",
"$ 210,500.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 125')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Havel & Zbyszek Co",
"(26) 642-7555",
"Poland",
"$ 0.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 128')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Blauer See Auto, Co.",
"+49 69 66 90 2555",
"Germany",
"$ 59,700.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 129')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Mini Wheels Co.",
"6505555787",
"USA",
"$ 64,600.00"
],
[
"'<button type=\"button\" class=\"btn btn-info btn-sm\" onclick=\"alert('Customer Number: 131')\">'<i class=\"fas fa-info-circle\">'</i> Info'</button>",
"Land of Toys Inc.",
"2125557818",
"USA",
"$ 114,900.00"
]
]
}