How to pass table content for bulk update?

I am using elixir-phoenix…I have a html table which displays all the content and is also editable…I want to update entire changed table content(each row) dynamically upon clicking update button… How can I pass table content from html page to controller??Can anyone help??

This is my html page


<%= form_for @conn, Routes.tipping_path(@conn, :index), fn f -> %>

<h2 align="center">Tipping</h2> </br>
<div class="pull-right">
<label>Current Season:</label>&nbsp; <label><%= @season_name %></label>
</div></br>
<div class="form-group">
       <%= label f, :Choose_a_Round, class: "control-label"%>
       <%= select f, :round_id, @round_list, class: "form-control", prompt: "Choose a Round", selected: @max, drab: "click:loadusers",readonly: false%>
       <%= error_tag f, :round_id %>
</div>
<table class="table">
  <thead>
    <tr>
      <th>Player name</th>
      <th>Team Name</th>
      <th>Team Nick Name</th>
      <th>Points</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
<%= for user <- @users do %>
    <tr id=user.id>
      <td>
      <div class="form-group">
        <%= text_input f, :user_id, class: "form-control", value: user.user_id, readonly: true%>
        <%= error_tag f, :user_id %>
      </div>
      </td>
      <td>
        <div class="form-group">
       <%= select f, :team_id, @teams, class: "form-control", prompt: "Choose a Team"%>
       <%= error_tag f, :team_id %>
      </div>
      </td>
      <td>
      <div class="form-group">
        <%= text_input f, :tip_team_id, class: "form-control", value: user.tip_team_id, readonly: true%>
        <%= error_tag f, :tip_team_id %>
      </div>
      </td>
      <td>
      <div class="form-group">
        <%= text_input f, :points, class: "form-control",value: user.points %>
        <%= error_tag f, :points %>
      </div>
      </td>
      <td>
        <span><%= submit "Update", to: Routes.tipping_path(@conn, :update,user) %></span>
      </td>
    </tr>
<% end %>
  </tbody>
</table>

<div class="form-group">
   
    <%= link "Update", to: Routes.tipping_path(@conn, :update,user) %>

  </div>
<span><%= link "Home", to: Routes.page_path(@conn, :index) %></span>
<% end %>
------------------------------
My Controller
``````````````````
def update(conn, %{"id" => id, "tipping" => tipping_params}) do
    tipping = Repo.get!(Tipping, id)
    changeset = Tipping.changeset(tipping, tipping_params)
    case Repo.update(changeset) do
      {:ok, team} ->
        conn
        |> put_flash(:info, "Tipping updated successfully.")
        |> redirect(to: tipping_path(conn, :index))
      {:error, changeset} ->
        render(conn, "index.html")
    end
  end

That question would be more accurate as “How can I pass the table content from the html page to the server”, which would just be a normal HTML form, nothing special about it there as it would be sent to the server in the usual HTML way. :slight_smile:

Essentially since your whole form is editable you just submit it make and submit the changes to the database (or whatever you are using) then render the new version of the page out.

The code given looks correct on an initial quick look, where specifically is it failing?

2 Likes

Thank you for replying…I do have a submit button which is performing insertion operation. Now this is another button in the form that should perform update…Currently it is passing only the last row of the table as parameter…

That’s because they all have the same name, since you are looping over them then you need to distinguish them somehow, like by setting something like this on each field: id: "#{input_id(f, :user_id)}[#{user.somethingUniquePerRow}]" :slight_smile:

Ya…That solved it … Thank you…:grinning:

1 Like