admin管理员组文章数量:1415484
I have an apps script that dynamically creates content from a Google Sheet, loading content into Bootstrap grid ponents.
While the code does work correctly, I need to cater for multiple rows, explained below.
A simplified version of the code is:
<? for (var i=0 ; i <lastRow; ++i) { ?>
<?if(getNSW[i] != "" && getNSW[i] != "TOTAL") {?>
<div class="col-md-4">
<!-- output -->
</div>
</div><? }} ?>
What I am aiming to do is for every 3rd col div created, to put them in a new "row" div.
Output would be something like:
<div class="row">
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
</div>
<div class="row">
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
</div>
Obviously I need some sort of for loop to count the cols etc... I am just drawing a blank for the correct syntax.
I have an apps script that dynamically creates content from a Google Sheet, loading content into Bootstrap grid ponents.
While the code does work correctly, I need to cater for multiple rows, explained below.
A simplified version of the code is:
<? for (var i=0 ; i <lastRow; ++i) { ?>
<?if(getNSW[i] != "" && getNSW[i] != "TOTAL") {?>
<div class="col-md-4">
<!-- output -->
</div>
</div><? }} ?>
What I am aiming to do is for every 3rd col div created, to put them in a new "row" div.
Output would be something like:
<div class="row">
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
</div>
<div class="row">
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
<div class="col-md-4"><!-- content --></div>
</div>
Obviously I need some sort of for loop to count the cols etc... I am just drawing a blank for the correct syntax.
Share Improve this question asked Jan 24, 2017 at 23:43 Nathan RusconiNathan Rusconi 231 silver badge4 bronze badges 1- use a nested loop where the outside loop creates the row every 3rd time the inside loop runs – RenaissanceProgrammer Commented Jan 24, 2017 at 23:45
3 Answers
Reset to default 3Try this:
var output = "<div class='row'>";
for(var i=0;i<lastRow;i++)
{
if((i%3)==0)
{
output += "</div><div class='row'>" + "<div class='col-md-4'><!-- content --></div>";
}
else
{
output += "<div class='col-md-4'><!-- content --></div>";
}
}
if((i%3)!=0)
{
output += "</div><div class='row'>";
}
What the above code does is, every 3rd iteration it inserts
</div><div class="row">
which terminated the previous div tag and starts a new row.
PS. You'll need to add the logic for the content to be displayed.
Use nested loops
<?php
for($i=0;$i<rows;$i++){
echo '<div class = "row">'
for($j=0;$j<3;$j++){
if(getNSW[i] != "" && getNSW[i] != "TOTAL"){
?>
<div class="col-md-4"><!--content--></div>
<?php
}
}
echo '</div>'
}
?>
Using Django template you could easily do it like this:
<div class="row">
{% for item in items %}
<div class="col-md-3">{{ item }}</div>
<!-- if last column in row -->
{% if forloop.counter|divisibleby:"4" and not forloop.last %}
</div><div class="row">
{% endif %}
{% endfor %}
</div>
本文标签: javascriptDynamically create rows with for loop in BootstrapHTMLStack Overflow
版权声明:本文标题:javascript - Dynamically create rows with for loop in BootstrapHTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745233979a2648948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论