我正在尝试在Rails应用程序中使用引导程序创建选项卡内容。我当前的实现如下所示
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="v-pills-England" role="tabpanel" aria-labelledby="v-pills-England-tab">
<% @teams.each do | team | %>
<% if team.team_country == "England" %>
<div class="form-check">
<input class="form-check-input" type="radio" name="team" id="<%= team.id %>" value="<%= team.id %>">
<label class="form-check-label" for="<%= team.id %>">
<%= team.short_name %>
</label>
</div>
<% end %>
<% end %>
</div>
<div class="tab-pane fade" id="v-pills-France" role="tabpanel" aria-labelledby="v-pills-France-tab">
<% @teams.each do | team | %>
<% if team.team_country == "France"%>
<div class="form-check">
<input class="form-check-input" type="radio" name="team" id="<%= team.id %>" value="<%= team.id %>">
<label class="form-check-label" for="<%= team.id %>">
<%= team.short_name %>
</label>
</div>
<% end %>
<% end %>
</div>
</div>
这行得通,我在合适的国家/地区获得了预期的“团队”。问题是,代码是如此重复,我正在寻找一种使它更“干”的方法。当我尝试遍历“选项卡内容”类时,我没有让团队进入正确的国家/地区。这就是我尝试过的
<div class="tab-content" id="v-pills-tabContent">
<% @teams.each do | team | %>
<div class="tab-pane fade show active" id="v-pills-<%= team.team_country %>" role="tabpanel" aria-labelledby="v-pills-<%= team.team_country %>-tab">
<%= team.short_name %>
</div>
<% end %>
</div>
在团队位于正确国家/地区的情况下,迭代选项卡内容并动态插入值的最佳方法是什么。
一种选择是用于group_by
将阵列分组为国家/地区,然后遍历各组:
<div class="tab-content" id="v-pills-tabContent">
<% @teams.group_by(&:team_country).each do |country, country_teams| %>
<div class="tab-pane fade show active" id="v-pills-<%= country %>" role="tabpanel" aria-labelledby="v-pills-<%= country %>-tab">
<% country_teams.each do | team | %>
<div class="form-check">
<input class="form-check-input" type="radio" name="team" id="<%= team.id %>" value="<%= team.id %>">
<label class="form-check-label" for="<%= team.id %>">
<%= team.short_name %>
</label>
</div>
<% end %>
</div>
<% end %>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句