LiveView list element with lot of entrys but not a nil value and sorting

I create a liveview with the generator. In the form_component.ex is a entry for owners. That give a list of created owner out. I can select one an save it with the form.

<.input field={@form[:owner_id]} type="select" label="Owner" options={Persons.list_owner_1() |>{&1.owner_d, &})} />

The function list_owner_1 give me the list of owners back. The owners are in a seperate table.

def list_owner_1 do
(from s in Owner, select: %Owner{id:, owner_d: fragment(“concat( ?, ’ ', ?)”, s.firstname, s.surname)})
|> Repo.all

That works fine. But I need a possibility to assign the owner later. In the list, there should be a empty value. And the best it ist the default. How can I do that?

And the other question ist how I can sort the list of Owners?

Hello and welcome to the forum!

I’m not quite sure what you mean by “needing to assign the owner later” but if you just need a default, non-value, there is the prompt option for selects:

<.input type="select" options={[...]} prompt="Select owner" />`

To order by a computed field, you need to use selected_as. Something like this (untested):

from s in Owner,
select: %Owner{
  owner_d: selected_as(fragment(“concat( ?, ’ ', ?)”, s.firstname, s.surname), :owner_d)
order_by: selected_as(:owner_d)

Of course if you just want to sort by first name or last name alone then you don’t need that, just do: order_by: s.surname.

1 Like

Thank you very much for your welcome.

Your idea with the prompt helped me a lot. The sorting should be work in a different way. Because if the getting value is nil, there is an exception raised.

I want to remove the function “list_owner_1”. But how can I get the values from the other table and be safe against nil values?