Lu2cz

Lu2cz

Dynamically generated columns in flop_table

Hi,
I am stuck and would appreciate advice.

I have a database of assets. Each asset belongs to a category. Each category has specific set of attributes.

I want to be able to choose a category from a dropwdown menu and that will generate a table of assets only from that category with attributes being the columns. Is it even possible with flop_table?

Marked As Solved

woylie

woylie

If you are able to obtain a list of columns by category, you could use a :for attribute on the :col slot.

def columns(:category_a) do
  [{"Name", :name}]
end

def render(assigns)
  ~H"""
  <Flop.Phoenix.table items={@items} meta={@meta} path={~p"/items"}>
    <:col
      :let={i}
      :for={{label, field} <- columns(@category)}
      label={label}
      field={field}
    >
      {Map.get(i, field)}
    </:col>
  </Flop.Phoenix.table>
  """
end

You could also add a formatter function to the tuple if you need to format columns differently (e.g. to format datetimes).

Also Liked

sodapopcan

sodapopcan

Hello and welcome!

Can you share a little more about your data structure? Is category polymorphic or do you mean only some attributes apply to different categories?

More broadly, Flop can’t dynamically generate tables based on different schemas (you would have to make a table for each type and switch them out), but you could conditionally render certain columns based on certain conditions:

<:col :let={record} :if={record.category.type == :some_type}>
  {record.name}
</:col>
woylie

woylie

You need to define the filterable fields explicitly by design. Making every column filterable automatically can introduce vulnerabilities.

To expand on the answers above, you can retrieve a list of all filterable columns with Flop.Schema.filterable/1 and then get additional information on each field with Flop.Schema.field_info/2.

woylie

woylie

If you have a query that returns only some columns to the user, and you allow to filter on columns that are normally hidden from the user, the user can gain insight about the column values just by using filters. For example, you might have a view of users that only shows the age and the city of each user. If the filter API allows you to apply filters on the email column, you can now check whether a user with a certain email address is in the system. Or maybe you already know that a user with a certain email address is in the system - now you can easily find the age and city of that user, even though the UI normally doesn’t give access to this information. Or if you have a numerical field, let’s say a trust_score that is for internal use only, you can easily construct filters with > and < operators to narrow down the exact value for a particular user.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement