trigeek381

trigeek381

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

Showing Posts 1 to 7

josevalim

josevalim

Creator of Elixir

The :let applies to the body of the tag but not its attributes in case of slots. So for slots, you can’t access the variables you defined on :let on the attributes themselves.

BradS2S

BradS2S

I think you can set a global attribute with a default class and then use that on the td.

The documentation includes an example with a default class value set.

trigeek381

trigeek381 OP

Man this is confusing! :blush: Any hint on how I can access the variable in the attributes? I apologize if it’s simple, but I’m still wrapping my head around consuming streams, components and heex.

BradS2S

BradS2S

I think this example fits best:

attr :rest, :global, default: %{class: "bg-blue-200"}

attr :message, :string, required: true

def notification(assigns) do
  ~H"""
  <span {@rest}><%= @message %></span>
  """
end
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.

trigeek381

trigeek381 OP

Thanks for clarifying that. I started having assumptions this was the case. Now I can rest

ericridgeway

ericridgeway

Is there no way to fix this? For example

<div :for={dog ← @dogs} id={“pets-#{dog.name}”>

There we get a variable in the :for part but we can still access to it in the other attributes

Is there any way :let could do this too, or is it just impossible with how things stand?

— 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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
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

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews