Reputation: 3584
I am using Gridster widget for webpages.There is a JSON which gives data about what image should be there on each grid(Text from JSON is captured and then corresponding image from database is loaded).Currently I am able to display a single image on grid.My aim is to display multiple images on the grids.The multiple images will be comma seperated names in json.
Current JSON is of the form:
var json = [{
"html": 'abc.png',
"col": 1,
"row": 1,
"size_y": 2,
"size_x": 2
}, {
"html": "xyz.png",
"col": 4,
"row": 1,
"size_y": 2,
"size_x": 2
},
{
"html": "def.png",
"col": 6,
"row": 1,
"size_y": 2,
"size_x": 2
},
{
"html": "abc.png",
"col": 1,
"row": 3,
"size_y": 1,
"size_x": 1
}, {
"html": "def.png",
"col": 4,
"row": 3,
"size_y": 1,
"size_x": 1
},
{
"html": "abc.png ",
"col": 6,
"row": 3,
"size_y": 1,
"size_x": 1
}
];
It has just one image per grid
New JSON will be of the form:
var json = [{
"html": "abc.png,xyz.png,def.png', //3 Images
"col": 1,
"row": 1,
"size_y": 2,
"size_x": 2
}, {
"html": "xyz.png", //1 Image
"col": 4,
"row": 1,
"size_y": 2,
"size_x": 2
},
{
"html": "def.png,abc.png", //2 Images
"col": 6,
"row": 1,
"size_y": 2,
"size_x": 2
},
{
"html": "abc.png", //1 Image
"col": 1,
"row": 3,
"size_y": 1,
"size_x": 1
}, {
"html": "def.png,abc.png", //2 Images
"col": 4,
"row": 3,
"size_y": 1,
"size_x": 1
},
{
"html": "abc.png", // 1 Image
"col": 6,
"row": 3,
"size_y": 1,
"size_x": 1
}
];
So old form is like
"html":"image1"
New form is like
"html":"image1,image2,....imageN"
The JS which creates widgets is as follows:
for(var index=0;index<json.length;index++) {
{% load static %}
gridster.add_widget('<li class="new" ><button class="delete-widget-button" style="float: right;">-</button><img src="{% get_static_prefix %}images/'+json[index].html+'"></li>',json[index].size_x,json[index].size_y,json[index].col,json[index].row);
};
I am not getting how the above loop will take care of handling multiple images
Update 1
Upvotes: 1
Views: 1560
Reputation: 10305
I'm not familiar with gridster, but maybe this will work for you -
Split the image filename string and then have another nested loop build the output string -
var images = json[index].html.split(',');
var imageOutput = "";
for(var j = 0; j < images.length; j++) {
imageOutput += '<img src="{% get_static_prefix %}images/'+ images[j] +'">';
}
gridster.add_widget('<li class="new" ><button class="delete-widget-button" style="float: right;">-</button>' + imageOutput + '</li>',json[index].size_x,json[index].size_y,json[index].col,json[index].row);
https://jsfiddle.net/joqfgr2t/3/
Upvotes: 1