I’m trying to generate the following html :
JavaScript
x
16
16
1
<div class="row">
2
<div class="col-md-3"></div>
3
<div class="col-md-3"></div>
4
<div class="col-md-3"></div>
5
<div class="col-md-3"></div>
6
</div>
7
<div class="row">
8
<div class="col-md-3"></div>
9
<div class="col-md-3"></div>
10
<div class="col-md-3"></div>
11
<div class="col-md-3"></div>
12
</div>
13
<div class="row">
14
<div class="col-md-3"></div>
15
</div>
16
So I tried the following :
JavaScript
1
18
18
1
var response = '[{"id":7},{"id":10},{"id":15},{"id":11},{"id":14},{"id":9},{"id":8},{"id":12},{"id":1}]'
2
var json = $.parseJSON(response);
3
var add_html = "";
4
$(json).each(function (i, val) {
5
if (i % 4 == 0){
6
add_html += "<div class='row'>";
7
}
8
add_html = add_html + "<div class='col-md-3'></div>";
9
if (i % 4 == 0){
10
add_html = add_html + "</div>";
11
}
12
});
13
/*
14
if (i % 4 != 1){
15
add_html = add_html + "</div>";
16
}
17
*/
18
console.log(add_html)
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
But doesn’t output the desired result
Advertisement
Answer
Try like below. And check changes with comment.
There are two changes in your code.
- You need to open your
row
div withi%4 == 0
and close it withi%4 == 3
. - Once
each
loop complete you should check if(json.length - 1) % 4 != 3
then need to add</div>
JavaScript
1
21
21
1
var response = '[{"id":7},{"id":10},{"id":15},{"id":11},{"id":14},{"id":9},{"id":8},{"id":12},{"id":1}]'
2
var json = $.parseJSON(response);
3
4
var add_html = "";
5
$(json).each(function(i, val) {
6
if (i % 4 == 0) {
7
add_html += "<div class='row'>";
8
}
9
add_html = add_html + "<div class='col-md-3'></div>";
10
// Change 1 - Update condition here to compare with 3
11
if (i % 4 == 3) {
12
add_html += "</div>";
13
}
14
});
15
16
// Change 2 - Add additional condition
17
if ((json.length - 1) % 4 != 3) {
18
add_html += "</div>";
19
}
20
21
console.log(add_html)
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Old version
- Initialize
add_html
with opening row div. - When
i % 4 == 3
which means it is forth index add closing div and open a new one. - Once
each
loop completes then add closing div.
Note – This has one bug. It will add extra <div class='row'></div>
when json array is of json.length % 4 == 0
.
JavaScript
1
18
18
1
var response = '[{"id":7},{"id":10},{"id":15},{"id":11},{"id":14},{"id":9},{"id":8},{"id":12},{"id":1}]'
2
var json = $.parseJSON(response);
3
4
// Initialize your html with opening row div
5
var add_html = "<div class='row'>";
6
$(json).each(function(i, val) {
7
add_html = add_html + "<div class='col-md-3'></div>";
8
if (i % 4 == 3) { // <- Update condition here to compare with 3
9
// Close current row div and open new one
10
add_html += "</div>";
11
add_html += "<div class='row'>";
12
}
13
});
14
15
// End your html with closing row div
16
add_html = add_html + "</div>";
17
18
console.log(add_html)
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>