Skip to main content

field_for with index

Javascript • Asked by Wouter van den Beld

I have a form that uses java script and the data-attribute to do some calculations (Calx2).
to make the data-attributes unique i use the the index method that is available.

<tbody id="detail_list">
      <%= f.fields_for :detail_action_amounts, remote:true, :wrapper => false do |detail_action_amount| %>
        <tr class="fields">
           <td><%= detail_action_amount.text_field :description %></td>
           <td><%= detail_action_amount.text_field :amount :data => { :cell=> "A#{detail_action_amount.index}"} %></td>
           <td><%= detail_action_amount.text_field :factor :data => { :cell=> "B#{detail_action_amount.index}"} %></td>
           <td><%= detail_action_amount.text_field :length :data => { :cell=> "C#{detail_action_amount.index}"} %></td>
           <td><%= detail_action_amount.text_field :width, :data => { :cell=> "D#{detail_action_amount.index}"} %></td>
           <td><%= detail_action_amount.text_field :height :data => { :cell=> "E#{detail_action_amount.index}"} %></td>
           <td><%= detail_action_amount.text_field :total  :data => { :cell=> "F#{detail_action_amount.index}", :formula=> "A#{detail_action_amount.index}*(C#{detail_action_amount.index}*D#{detail_action_amount.index}*E#{detail_action_amount.index})/100*B#{detail_action_amount.index}"} %></td>
          <td><%= detail_action_amount.link_to_remove "Verwijder", :class => "btn btn-outline btn-red btn-smal btn-remove" %></td>
        </tr>
      <% end %>
    </tbody>
     <tfoot>
            <tr>
                <td colspan="6">Totaal</td>
                <td><%= f.text_field :amount, class: 'labelsize50', :data => { :cell=> "G1", :formula=> "SUM(F0:F1000)"} %></td>
                <td colspan="2"></td>
            </tr>
        </tfoot>
    </table>

This all works but when i add a field the index method adds the time in milliseconds instead of a ascending number.
is there a good and simple way to use a ascending number?


Hmm, what does your javascript look like?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.