【问题标题】:Displaying multiple images dynamically with Javascript or jquery使用 Javascript 或 jquery 动态显示多个图像
【发布时间】: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);
 };

我不明白上述循环将如何处理多个图像

Fiddle Link

更新 1

Updated Fiddle Link

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    我不熟悉 gridster,但也许这对你有用 -

    分割图像文件名字符串,然后让另一个嵌套循环构建输出字符串 -

    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/

    【讨论】:

    • 我在这里尝试过 jsfiddle.net/7a0c2kq9/3 但我认为它现在以所需的方式工作。
    • 哦,我错了,我忘了在嵌套的 for 循环中使用 images[j]。检查编辑
    • 我很难把它放在小提琴这就是我试过的jsfiddle.net/2qkqmoc4/1
    • 我不确定你想要的结果/外观是什么 - jsfiddle.net/2qkqmoc4/8
    • 这就是我想要的,我的意思是网格上带有删除功能的多个图像。但除此之外,我还想要这里存在的其他字段jsfiddle.net/xg33t871(网格上具有图像链接的文本区域当我单击序列化时,它应该在文本区域中生成带有 id 日志的 json)
    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多