CodeIgniter4 DataTables Usage - Searchable Columns
Table Customers
only "selected" column will be searchable on
setSearchableColumns
code. See Source Code.
Cust. No | Name | Phone | City | Country | Postal Code |
---|
Note
This sample database is downloaded from : https://www.mysqltutorial.org/mysql-sample-database.aspx/
Controller
setSearchableColumns()
parameter can be : Array
or String
with separated comma.
You can also set columns are not displayed.
on example code.
'state'
column is not on select query (will not displayed) but it will querying for search
also available
addSearchableColumns()
parameter also : Array
or String
use \Hermawan\DataTables\DataTable;
public function custom_searchable()
{
$db = db_connect();
$builder = $db->table('customers')->select('customerNumber, customerName, phone, city, country, postalCode');
return DataTable::of($builder)
->setSearchableColumns(['customerName', 'country', 'state'])
//->addSearchableColumns('city') //city added
->toJson();
}
Javascript
$(document).ready(function() {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: '/ajax-datatable/custom-searchable'
});
});
AJAX Response :
{
"draw": "1",
"recordsTotal": 122,
"recordsFiltered": 122,
"data": [
[
"103",
"Atelier graphique",
"40.32.2555",
"Nantes",
"France",
"44000"
],
[
"112",
"Signal Gift Stores",
"7025551838",
"Las Vegas",
"USA",
"83030"
],
[
"114",
"Australian Collectors, Co.",
"03 9520 4555",
"Melbourne",
"Australia",
"3004"
],
[
"119",
"La Rochelle Gifts",
"40.67.8555",
"Nantes",
"France",
"44000"
],
[
"121",
"Baane Mini Imports",
"07-98 9555",
"Stavern",
"Norway",
"4110"
],
[
"124",
"Mini Gifts Distributors Ltd.",
"4155551450",
"San Rafael",
"USA",
"97562"
],
[
"125",
"Havel & Zbyszek Co",
"(26) 642-7555",
"Warszawa",
"Poland",
"01-012"
],
[
"128",
"Blauer See Auto, Co.",
"+49 69 66 90 2555",
"Frankfurt",
"Germany",
"60528"
],
[
"129",
"Mini Wheels Co.",
"6505555787",
"San Francisco",
"USA",
"94217"
],
[
"131",
"Land of Toys Inc.",
"2125557818",
"NYC",
"USA",
"10022"
]
]
}