CodeIgniter4 DataTables Usage - Numbering (Object data source)
Table Customers
No | Name | Phone | City | Country | Postal Code |
---|
Note
This sample database is downloaded from : https://www.mysqltutorial.org/mysql-sample-database.aspx/
Controller
when call method
addNumbering()
you can add argument with column name
. Need column name for initialize DataTable javaScript
use \Hermawan\DataTables\DataTable;
public function numbering()
{
$db = db_connect();
$customers = $db->table('customers')->select('customerName, phone, city, country, postalCode');
return DataTable::of($customers)->addNumbering('no')->toJson(true);
}
Javascript
$(document).ready(function() {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: '/ajax-datatable/numbering-object',
order: [],
columns: [
{data: 'no', orderable: false},
{data: 'customerName'},
{data: 'phone'},
{data: 'city'},
{data: 'country'},
{data: 'postalCode'},
]
});
});
AJAX Response :
{
"draw": "1",
"recordsTotal": 122,
"recordsFiltered": 122,
"data": [
{
"no": 1,
"customerName": "Atelier graphique",
"phone": "40.32.2555",
"city": "Nantes",
"country": "France",
"postalCode": "44000"
},
{
"no": 2,
"customerName": "Signal Gift Stores",
"phone": "7025551838",
"city": "Las Vegas",
"country": "USA",
"postalCode": "83030"
},
{
"no": 3,
"customerName": "Australian Collectors, Co.",
"phone": "03 9520 4555",
"city": "Melbourne",
"country": "Australia",
"postalCode": "3004"
},
{
"no": 4,
"customerName": "La Rochelle Gifts",
"phone": "40.67.8555",
"city": "Nantes",
"country": "France",
"postalCode": "44000"
},
{
"no": 5,
"customerName": "Baane Mini Imports",
"phone": "07-98 9555",
"city": "Stavern",
"country": "Norway",
"postalCode": "4110"
},
{
"no": 6,
"customerName": "Mini Gifts Distributors Ltd.",
"phone": "4155551450",
"city": "San Rafael",
"country": "USA",
"postalCode": "97562"
},
{
"no": 7,
"customerName": "Havel & Zbyszek Co",
"phone": "(26) 642-7555",
"city": "Warszawa",
"country": "Poland",
"postalCode": "01-012"
},
{
"no": 8,
"customerName": "Blauer See Auto, Co.",
"phone": "+49 69 66 90 2555",
"city": "Frankfurt",
"country": "Germany",
"postalCode": "60528"
},
{
"no": 9,
"customerName": "Mini Wheels Co.",
"phone": "6505555787",
"city": "San Francisco",
"country": "USA",
"postalCode": "94217"
},
{
"no": 10,
"customerName": "Land of Toys Inc.",
"phone": "2125557818",
"city": "NYC",
"country": "USA",
"postalCode": "10022"
}
]
}