Jump to Particular Index in for loop Phoenix HTML?

How to look out for particular value in the For loop of the Phoenix HTML.
Like I only want to fetch out the 2nd candidate_pipeline from the @candidate_pipelines. How to do that? Your response will be highly appreciable. Note- Inside my for loop, the variable are depended on candidate_pipeline.

              <%= for candidate_pipeline <- @candidate_pipelines do %>
          
                <%= form_for get_candidate_update_changeset(candidate_pipeline), "#", [class: "d-inline", phx_change: "update_candidate_stage"], fn f -> %>
                  <input type="hidden" value="<%= candidate_pipeline.id %>" name="candidate_id"/>
                  <%= select f, :job_stage_id, Enum.map(@job_stages, &{&1.name, &1.id}), [class: "custom-select custom-select-sm"] %>
                <% end %>
              <% end %> 

https://hexdocs.pm/elixir/Kernel.SpecialForms.html#for/1
Look for the part about “filters”.

Can you give me example to how to fetch out nth index value from the For Loop?

You can’t but you can use Enum.with_index to add indexes to your list of items.

I have used the Enum.at but the problem is that I can’t then use the attribute of the candidate_pipe. For example, then I can’t use the candidate_pipeline.id. Could you solve the above part either by anyway? Note- I have the index value.

Or there is something like this- <%= candidate_pipeline = @candidate_pipelines[index] %>. So that values which will be stores into the variable and further I can access attribute values like candidate_pipeline.id.

It is not the functional way to proceed.

There are no loops, and using index to get Enum.at is not really efficient.

Something like taking each 2 elements should be done with filter.

What do You really want to do?

I want to fetch out the particular element present at a index value from @candidate_pipelines to a variable. With the help of this variable I can access value like variable.attribute. Mainly, I want to access all parameter that are available at the particular index value of the @candidate_pipelines.

When You use for, each element of @candidate_pipelines is going to be treated. You don’t need to lookup with an index, because it should be the current element.

Or did I miss something?

That’s what I have done- iterating over the enumerable but I want only 1 at a time that is present at a index, don’t want to iterate over list/enumerable.

You already get one element at a time with this.

Yes, You are right but the problem is that I don’t want other values. It is wastage of time, resource and unwanted value are getting as output. For example- I want only 2nd element but it is iterating over the whole due to which extra data from other index are geeting as output which I don’t want.

That is why @LostKobrakai suggested to use a filter with for :slight_smile:

If You set up correctly the filter, You will iterate only for the values You want.

Can you tell me how to do? Modify my code please and tell me. I can’t figure out. Its been 10 hr looking onto it.

From documentation linked…

# A comprehension with a generator and a filter
for n <- [1, 2, 3, 4, 5, 6], rem(n, 2) == 0, do: n
[2, 4, 6]

This will take each 2 elements.

Is this what You are looking for?

Get rid of the for comprehension and define a function in the view to do the work

<%= form_for second_changeset(@candidate_pipelines), "#", [class: "d-inline", phx_change: "update_candidate_stage"], fn f -> %>
  <input type="hidden" value="<%= candidate_pipeline.id %>" name="candidate_id"/>
  <%= select f, :job_stage_id, Enum.map(@job_stages, &{&1.name, &1.id}), [class: "custom-select custom-select-sm"] %>
<% end %>

For example you can pick the second element in a list this way:

iex(1)> second = fn [_, value| _] -> value end
#Function<6.128620087/1 in :erl_eval.expr/5>
iex(2)> list = [1,2,3]
[1, 2, 3]
iex(3)> second.(list)
2
iex(4)>

Example of defining view functions (page_view.ex) being used in index.html.eex

1 Like

If it is taking only one element, then Enum.at should be working too.

Can I do something like this-

Enum.at( @candidate_pipelines, get_current_pipeline_index(__something___)).id

Can I access “id” like this or other attributes like this?

iex(1)> list = [%{id: 1}, %{id: 2}, %{id: 3}]
[%{id: 1}, %{id: 2}, %{id: 3}]
iex(2)> second = Enum.at(list,1)
%{id: 2}
iex(3)> IO.puts("#{inspect(second.id)}")
2
:ok
iex(4)> list |> Enum.at(1) |> Map.get(:id)
2
iex(5)>

What is stopping you from assigning the necessary candidate and changeset in the controller for easy access in the template?