简体   繁体   English

通过ajax加载图像数组

[英]Load image array via ajax

so I have an array as response when i upload images with dropzone: 所以当我用dropzone上传图像时,我有一个数组作为响应:

{50: "/media/50/twitter cover photos for new year 2016.png", 51: "/media/51/174920-1280.png",…}

50: "/media/50/twitter cover photos for new year 2016.png"
51: "/media/51/174920-1280.png"
52: "/media/52/buy-instagram-followers.jpg"

Now i want to append this: 现在我要附加这个:

 <div class="col-lg-2 col-md-4 col-xs-6 thumb">
       <img class="img-responsive" src=" // the values of the response array //" alt="">
       <div class="galleryremovebutton">
          <a href="/delete/ the keys / ids of the response array " class="btn btn-danger" role="button">Izbrisi sliku</a>
       </div>
 </div>

as a loop to this: 作为对此的循环:

<div id="galleryimgs" class="row">

After the complete event of dropzone, that means i would need this code here somewhere: 在dropzone完成事件之后,这意味着我将在某些地方需要此代码:

Dropzone.options.myGalleryDropzone = {

            paramName: "file", // The name that will be used to transfer the file
            maxFilesize: 1, // MB
            parallelUploads: 8,
            complete: function () {
                HERE
            }
        };

Using only jQuery, you have a choice between building the DOM through a string or programmatically. 仅使用jQuery,您可以选择通过字符串或以编程方式构建DOM。 Either way, it should be a DOM element wrapped in jQuery: 无论哪种方式,它都应该是包装在jQuery中的DOM元素:

function makeThumb() {
  // $('html_for_one_thumbnail');
  // $('<div />').append('<div />')...etc;
  return elementForThumb;
}

Then another function to populate the element: 然后是另一个函数来填充元素:

function configureThumb($element, thumb, thumbId) {
  $element.find('img').attr('src', thumb);
  $element.find('a').attr('href', '/delete/' + thumbId);
}

And one more to put it together: 还有一个组合起来:

function buildThumbnails($element, thumbs) {
  thumbs.forEach(function(thumb, thumbId) {
    var $thumb = makeThumb();
    $element.append($thumb);
    configureThumb($thumb, thumb, thumbId);
  });
}

Called at the complete, assuming the container is empty: 假设容器为空,则在完成时调用:

complete: function(data) {
    buildThumbnails($('#galleryimgs'), data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM