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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews