Hi everyone, I got this very simple hide/show columns via socket assigns working but was wondering if anyone knows a better way.
I have a simple multi select (using the JS Select2 library) which allows my user to select columns to hide from a table. Selecting a column adds a character like “0” or “1” for example which is the position from left to right for the given column.
I then send an event to my live view with the selected column and add said column (“0”, “1”, “2”, etc) to a list in my assigns named hidden_columns:
In my HTML I determine which columns to show/hide based on my assigns in the following fashion, which works but just doesn’t feel like good design. Your opinion will be greatly appreciated. Can you think of a better way to show/hide columns, maybe a more efficient way?
<table class="border-collapse w-full mt-4">
<thead>
<tr>
<%= if "0" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Class number</th>
<% end %>
<%= if "1" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Unit number</th>
<% end %>
<%= if "2" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Type</th>
<% end %>
<%= if "3" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Make</th>
<% end %>
<%= if "4" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Model</th>
<% end %>
<%= if "5" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Class code</th>
<% end %>
<%= if "6" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Class</th>
<% end %>
<%= if "7" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Description</th>
<% end %>
<%= if "8" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">License plate</th>
<% end %>
<%= if "9" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Registration</th>
<% end %>
<%= if "10" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Year</th>
<% end %>
<%= if "11" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Serial number</th>
<% end %>
<%= if "12" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Location</th>
<% end %>
<%= if "13" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Owner</th>
<% end %>
<%= if "14" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Leasor</th>
<% end %>
<%= if "15" not in @hidden_columns do %>
<th class="p-1 font-bold uppercase text-sm bg-gray-200 text-gray-600 hidden lg:table-cell">Actions</th>
<% end %>
</tr>
</thead>
...............