Automatically adding columns to the report, no need to set a <th>

I’m currently putting together a report to query 2 separate tables with different column names.

My big problem is that in html, I create columns that way.
with fixed names.

                                      <table id="example2" class="table table-bordered table-hover dataTable dtr-inline"
                                        role="grid" aria-describedby="example2_info">
                                        <thead>
                                          <tr role="row">
                                            <th>Name</th>
                                            <th>Columns</th>
                                            <th>Status</th>
                                          </tr>
                                        </thead>
                                        <tbody id="reports">
                                          <tr id="reports-id">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                          </tr>
                                        </tbody>
                                      </table>

Example of function 1

[
  %Table1{
    __meta__: #Ecto.Schema.Metadata<:loaded, "table1">,
    city: "Example",
    id: 28,
    start: ~N[2021-06-17 19:27:00],
    inserted_at: ~N[2021-06-17 22:27:38],
    alert: 934534,
    status_table1: "Closed",
    updated_at: ~N[2021-06-17 22:27:38]
  }
]

Example of function 2


[
  %Table2{
    __meta__: #Ecto.Schema.Metadata<:loaded, "table2">,
    name: "Example name",
    test: "Teste 2",
    status_table2: "Resolved",
    id: 28,
    inserted_at: ~N[2021-06-17 22:27:38],
    alert: 934534,
    updated_at: ~N[2021-06-17 22:27:38]
  }
]

Is there the possibility of the phoenix liveview itself recognizing the columns automatically of the database, without the need to set the name ?

Could you expound more on this e.g. by placing the actual data of the structs in the desired html. I don’t seem to understand this clearly

You can introspect your schema and get the list of fields… Then it’s easy to build the table dynamically from this list.

Table1.__schema__(:fields)
Table2.__schema__(:fields)
2 Likes