Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lookup table example is U. S. States. You might have something like this:
State Id State Name State Abbrev
1 Alabama AL
2 Alaska AK
3 Arizona AZ
etc. etc.
Now in other tables that need to reference the state I’d use an integer field which is a foreign key to my state table:
Street Address City StateId Zip
123 Any Street AnyTown 1 11111
456 Any Avenue OtherTown 2 22222
etc.
Of course when I want data entry I don’t want to display the Id; I want to display the name in order to make it easy for my end user to select the right value. Is there a core component or some simple way to roll my own control such that it creates the proper tag for me? Something like (contiuning with the state example):
<select name="state" id="state">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
.
.
.
</select>
Right now the default created HEEx has a numeric field. I want to automatically generate a select tag with the correct option tags from the table.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You can use Phoenix.HTML.Form — Phoenix.HTML v4.3.0 with the core components input and type „select“
kip
Localize web has a form helper for this case. Something like:
Although note that this will return the subdivision (
statefor the US) short code. Iealfor Alabama, not1. You could use the :mapper option to generate integers if thats a firm requirement.Using
localize_webmeans you can easily select subdivisions for other territories (countries), localize the content to a given language and sort the subdivisions appropriately (different locales sort differently). All of that comes “for free”.netoum
If you are looking for an alternative, Corex Select component let you use either your own list or Localize values instead. Works in Controller and Live View
I used a similar approach for the language switch, Localize is great for getting each language names in each in its own language.
You can find a demo repo here
From Localize
From DB
Hope this helps
Onor.io
Thank you for that but the state list was simply an example of what I call a lookup table. But thanks anyway!
Onor.io
Thanks for that but the state list was solely intended as an example of the issue I’m trying to solve. Still I might be able to use that stuff to solve the issue I am trying to solve so I greatly appreciate the pointer.