trigeek381

trigeek381

Passing dynamic value to html tag using component slots and attrs (v1.7.2)

I’m using Phoenix v1.7.2. I’m using the default index.html.heex for listing records.

My records have a ‘color’ field that I store as a default color. I would like to pass this value to the individual as a background color.

I’ve added this to the table component.

  slot :col, required: true do
    attr :label, :string
    attr :col_class, :string  ## My addition
  end
... and the table template looks like this

<tr :for={row <- @rows} id={@row_id && @row_id.(row)} class="group hover:bg-zinc-50">
  <td
      :for={{col, i} <- Enum.with_index(@col)}
      phx-click={@row_click && @row_click.(row)}
      class={["relative p-0", @row_click && "hover:cursor-pointer", col[:col_class]] <-My edit
  >
...

Here is the snippet of the table that I’m trying to modify. Static value works as below

<.table
  id="services"
  rows={@streams.services}
  row_click={fn {_id, service} -> JS.navigate(~p"/services/#{service}") end}
>
  <:col :let={{_id, service}} label="Name"><%= service.name %></:col>
  <:col :let={{_id, service}} label="Color" col_class="bg-slate-200"><%= service.color %></:col>  <- Works

But, I would like to do something like this

  <:col :let={{_id, service}} label="Color"  col_class={"bg-#{service.color}-200"><%= service.color %></:col>

but I’m getting this error

undefined function service/0 (expected HelloWeb.ServiceLive.Index to define such a function or for it to be imported, but none are available)

Thanks for looking at this

Marked As Solved

LostKobrakai

LostKobrakai

You can‘t. :let={} is calculated by calling the function component, which passes in all other attributes. So all other attributes need to already exist and that before the value for :let={} is calculated. You‘d need to solve that within a single component/template.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement