【发布时间】:2018-03-23 17:47:04
【问题描述】:
我正在为网页使用Gridster 小部件。有一个 JSON 提供有关每个网格上应该有什么图像的数据(捕获来自 JSON 的文本,然后加载来自数据库的相应图像)。目前我能够在网格上显示单个图像。我的目标是在网格上显示多个图像。多个图像将是 json 中的逗号分隔名称。
当前的 JSON 格式为:
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
}
];
每个网格只有一张图片
新的 JSON 格式如下:
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
}
];
这么旧的形式就像
"html":"image1"
新形式就像
"html":"image1,image2,....imageN"
创建小部件的JS如下:
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);
};
我不明白上述循环将如何处理多个图像
更新 1
【问题讨论】:
标签: javascript jquery json