如何创建尽可能多的dynamicEl,就像我们从PHP收到的一样

问题描述 投票:0回答:1

[在主页上具有图库,在单击图像时需要打开lightGallery幻灯片,其中包含在ftp目录中找到的图像,并且在主页上单击了图库名称。

用于查找图像并对其进行计数的PHP:

<?php
header("Content-Type: application/json");

$dirname = $_POST["galleryName"];

$imgGallery = glob("../gallery/" . $dirname . "/".$dirname."_*.*");
$thumbGallery = glob("../gallery/" . $dirname . "/thumb_*.*");

$countImages = count($imgGallery);

echo json_encode(array("imgNormal" => $imgGallery, "imgThumb" => $thumbGallery, "imgCount" => $countImages));
?>

JS:

$('.info').click(function() {
    var galleryName = $(this).closest('.imgGallery').find('img.img-thumbnail').attr('name');
    $.ajax({
        url: "gallery/imgFinder.php",
        dataType: "json",
        type: "post",
        data: {
            galleryName: galleryName
        },
        success: function(xhr) {
            if (xhr.imgCount != 0) {
                console.log(xhr.imgNormal);
                console.log(xhr.imgThumb);
                console.log(xhr.imgCount);
                for (var i = 1; i <= xhr.imgCount; i++) {
                    $(this).lightGallery({
                        dynamic: true,
                        dynamicEl: [{ //We need to create as much as we received w "xhr.imgCount"
                                        "src": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg",
                                        "thumb": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg"
                        }]
                    })
                    return;
                }
            } else {
                console.log('Gallery \'' + galleryName + '\' has no images');
                return;
            }
        },
        error: function(xhr) {
            console.error("Some error found");
        }
    });
});

我们需要使用接收到的xhr.imgCount来创建尽可能多的具有图像/缩略图变量的dynamicEl

要得到这样的东西:

dynamicEl: [{
    "src": '...',
    'thumb': '...'
}, {
    'src': '...',
    'thumb': '...'
}, {
    'src': '...',
    'thumb': '...'
}, {
    'src': '...',
    'thumb': '...'
}]

在主页上具有图库,在单击图像时需要打开lightGallery幻灯片,其中包含在ftp目录中找到的图像,并且在主页上单击了图库名称。 PHP用于查找图像并对其进行计数:...

php ajax variables dynamic lightgallery
1个回答
1
投票

在您的JS代码内部,您需要进行如下更新:

© www.soinside.com 2019 - 2024. All rights reserved.