我有一個數組,我只想一次只顯示4個項目,當我點擊Next時,它將顯示下4個項目。 查看代碼:如何在rails中顯示一定數量的數組項目
<table>
<tr>
<th></th>
<th>3</th>
<th>2</th>
<th>1</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th></th>
</tr>
<% count = 0 %>
<% while count < 4%>
<tr class="match_row" value="<%=count%>">
<th id="left_competitor"><%= @matches[count][0].title %></th>
<% (1..6).each do |i|%>
<th> <input type="radio" class="group_<%=count%>" name="<%=count%>" value="<%= i%>"><br></th>
<% end %>
<th id="right_competitor"> <%= @matches[count][1].title %></th>
</tr>
<br />
<% count += 1 %>
<% end %>
<br />
</table>
<input id="save_votes" type="button" value="Next">
現在這裏是CoffeeScript的代碼:
move_to_next_match = ->
$("#save_votes").click -> display_next_competitors()
display_next_competitors = ->
project_id = document.getElementById("project_id").value
match_rows = $('.match_row')
results = []
for i in [0...match_rows.length]
do (i) ->
radios = $(".group_#{i}")
for j in [0...radios.length]
do (j) ->
if radios[j].checked
count = radios[j].getAttribute 'name'
number = radios[j].getAttribute 'value'
results.push("#{count} #{number}")
data = "{\"get_matches_results\": \"#{results}-#{project_id}-#{marker}\"}"
$.ajax({
type: "POST"
url: "projectss/pca_results"
data: "data=" + data
})
location.reload(true);
我的想法是讓鑑於變量count(<%計數= 0%>)取一個變量的值而不是0,但我無法弄清楚如何做到這一點。 任何想法?
你不需要'do(i) - >'和'do(j) - >'在你的代碼中。您只需要'do(x) - >'語法來捕獲由內部函數引用的值。 –