You can use the function fieldTypeSearchColumn to add a dropdown search. Just to make it optimize we can have it only for 2 states. Don’t forget to add the below if statement:
if ($crud->getState() === 'Datagrid' || $crud->getState() === 'Initial') {
So the final code will look like this:
$crud->setRelation('country', 'countries', 'nicename');
$countries = [];
// 🟢 Added an extra if statement here
if ($crud->getState() === 'Datagrid' || $crud->getState() === 'Initial') {
$countriesModel = $model->getRelationData(
'countries',
'nicename'
);
foreach ($countriesModel as $country) {
$countries[$country->id] = $country->title;
}
// 🟢 Added this extra line of code here
$crud->fieldTypeSearchColumn('country', 'dropdown_search', $countries);
}
$crud->callbackColumn('country', function ($value) use ($countries) {
return ($value && isset($countries[$value]) ? $countries[$value] : $value);
});
Let me know if that worked for you.
Regards
Johnny