ruby on rails - jQuery function works in partial but not show -
i have jquery code counts current rows in table (and arithmetic) , adds them column. i've used script in _form/_partial , works great. however, when use same script inside show.html.erb (to setup same table again) not work.
here _form
<table id="mytable"> <th>rownumber</th> <th>header2</th> <th>header3</th> <th>header4</th> <%= f.nested_fields_for :partial |f| render 'partial', f: f end -%> <caption align = "bottom"><%= link_to_add_fields "add field", f, :manual_iops %></caption> </table> _partial.html.erb
<tr> <td> <!-- nothing goes here --> </td> <td> <%= f.text_field :data1 %> </td> <td> <%= f.text_field :data2 %> </td> <td> <%= f.text_field :data3 %> </td> </tr> <script> $('tr').each(function(){ var index= $('#mytable').children('tr').index($(this)) if(index == 0){ var position= 5; } if(index == 1){ var position = 10; } if(index == 2){ var position = 15; } if(index == 3){ var position = 20; } if(index > 3){ var position = (index-1)*10 } $(this).children('td').first().html(position); }) </script> now here i'm trying in show:
show.html.erb
<table id="mytable"> <th>rownumber</th> <th>header1</th> <th>header2</th> <th>header3</th> <% @data_sheet.partials.each |c| %> <tr> <td></td> <td><%= c.data1 %></td> <td><%= c.data2 %></td> <td><%= c.data3 %></td> </tr> <script> $('tr').each(function(){ var index= $('#mytable').children('tr').index($(this)) if(index == 0){ var position= 5; } if(index == 1){ var position = 10; } if(index == 2){ var position = 15; } if(index == 3){ var position = 20; } if(index > 3){ var position = (index-1)*10 } $(this).children('td').first().html(position); }) </script> <% end %> </table> here image of how _form looks, can see first column has values: 5, 10, 15, 20, due script in partial.html.erb counting rows , doing + , *: 
and here show.html.erb, shows empty column(it should filled 5,10,15,20) due issue. running same script here partial:

so doing wrong in situation? thing see different loop , fact not rendering partial, see no reason why should effect javascript. i'm banging head against wall this.
ok, if want ruby it's easy:
<table id="mytable"> <th>rownumber</th> <th>header1</th> <th>header2</th> <th>header3</th> <% @count = 5 %> <% @data_sheet.partials.each |c| %> <tr> <td><%= @count %></td> <td><%= c.data1 %></td> <td><%= c.data2 %></td> <td><%= c.data3 %></td> </tr> <% if @count >= 20 %> <% @count = @count + 10 %> <% else %> <% @count = @count + 5 %> <% end %> </table> you can rewrite if , statement as:
<% @count >= 20 ? (@count = @count + 10) : (@count = @count + 5) %>
Comments
Post a Comment