Onor.io

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.

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

You can use Phoenix.HTML.Form — Phoenix.HTML v4.3.0 with the core components input and type „select“

kip

kip

ex_cldr Core Team

Localize web has a form helper for this case. Something like:

<.form :let={f} for={@changeset} action={~p"/addresses"}>
  <label for="address_state">State</label>
    {Localize.HTML.subdivision_select(f, :state, territory: :US, prompt: "Choose a state")}
  <.button>Save</.button>
</.form>

Although note that this will return the subdivision (state for the US) short code. Ie al for Alabama, not 1. You could use the :mapper option to generate integers if thats a firm requirement.

Using localize_web means 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

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

state_items =
  Localize.HTML.Subdivision.subdivision_options(territory: :US)
  |> Enum.map(fn {name, code} -> %{label: name, value: to_string(code)} end)
  |> Corex.List.new()
<.form :let={f} for={@form} action={~p"/addresses"} method="post">
  <.select
    field={f[:state]}
    class="select ui-max-height-sm"
    items={@state_items}
    translation={%Corex.Select.Translation{placeholder: "Choose a state"}}
  >
    <:label>State</:label>
    <:trigger><.heroicon name="hero-chevron-down" /></:trigger>
  </.select>
  <.button>Save</.button>
</.form>

From DB

items =
  Repo.all(from s in State, order_by: s.name)
  |> Enum.map(&%{label: &1.name, value: to_string(&1.id)})
  |> Corex.List.new()
<.form :let={f} for={@form} action={~p"/addresses"} method="post">
  <.select
    field={f[:state_id]}
    class="select ui-max-height-2xs"
    items={@items}
    translation={%Corex.Select.Translation{placeholder: "Choose a state"}}
  >
    <:label>State</:label>
    <:trigger><.heroicon name="hero-chevron-down" /></:trigger>
  </.select>
  <.button>Save</.button>
</.form>

Hope this helps

Onor.io

Onor.io OP

Thank you for that but the state list was simply an example of what I call a lookup table. But thanks anyway!

Onor.io

Onor.io OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews