DEV Community

Horacio Degiorgi
Horacio Degiorgi

Posted on

1

Dynamic datalist in filamentphp 3

Revised version , thanks Filament Team !!

The new version of filamentphp allow the updating of datalist tag in TexInput with new methods. Some tricks are needed but is possible to load datalist options from the database based on the characters inserted in input field.
The tricks:
->autocomplete('off') avoid browser tips
->live(debounce:400) activate sending info to the server
->datalist(function (?string $state, TextInput $component,?Model $record , $modelsearch='\App\Models\Metadata' , $fieldsearch='content') {

$options= $modelsearch::whereRaw($fieldsearch.' ilike \'%'.$state.'%\'')->limit(20)->pluck('content')->toarray();

})

All the code (simplified)

Forms\Components\TextInput::make('title')->label('Campo')
                ->live(debounce:400)
                ->autocomplete('off') 
                ->datalist(function (?string $state , Forms\Components\TextInput $component,?Model $record , $modelsearch='\App\Models\Metadata' , $fieldsearch='content') {
//get options from database using where and (ilike I'm a postgresql fan)
$options =[];
   if($state!=null  and Str::length($state)>=3){
                    $options= $modelsearch::whereRaw($fieldsearch.
' ilike \'%'.$state.'%\'')
->limit(20)
->pluck('content')
->toarray();
               //send options to datalist

}
return $options; 
                })

Enter fullscreen mode Exit fullscreen mode

Btw : PHP 8.0: Named Parameters ROCKS

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
isslerman profile image
Marcos Issler

There is any online example working of this feature? Thanks!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay