platform
platform copied to clipboard
Add data attributes to select option
Is your feature request related to a problem? Please describe. I suggest a way to add custom data attributes to options on Select when use FromModel
Describe the solution you'd like I think something like
Select::make('user.personal.country_id')
->fromModel(Model::class, 'name' , [
'data-attribute-1' => 'column-from-model'
])
Resulting in
<select>
<options value="" attribute-1="" > TEXT </options>
</select>
Additional context This function will be nice to use on JS functions to access more data from models
The third argument of the fromModel
method already exists.
Select::make('')->fromModel(User::class, 'name', 'id'),
The syntax is getting long.
I also have two questions.
- We rely on the package https://tom-select.js.org/ have you looked at it to build what you want?
- If it's for dynamic things like "If checkbox is clicked, show only active users". Then why not use the
Listener
layer already built in, instead of write your own JS code?
Yes, my fault when writing the example about the third argument. I dont have many examples to put now , But the scene that led me to write the issue was as follows. I have a list of countries, with name, code and dial_code. When I select a country, I need to set in an input the dial_code and the country code.
I have in the select , the name and id of the country but i'm without options to bring dial_code and code. At the moment, when changing the value of the select, I trigger an ajax with the parent ID and retrieve this information, but this could be avoided if I had this data in a data-attribute in the select options.
Maybe a method to generate the data attributes from the model ? something like:
->setAttributeFromModel([
'name' => 'value_from_model'
]);
This is one scene, but i'm certainly that this feature can be useful for more situations
It seems to me that we already have what you would like, with a field that automatically does field searches:
Relation::make('country')
->fromModel(Country::class, 'name')
->searchColumns('code', 'dial_code')
->title('Choose your country');