【问题标题】:Need to display multiple json image arrays but only one ever does需要显示多个 json 图像数组,但只有一个
【发布时间】:2014-03-10 23:48:21
【问题描述】:

所以它一定是我没有得到的迭代!我的模板文件中有一个 for 循环,它显示多个 cycle2 幻灯片:

<?php foreach ($foo as $bar) { ?>

    <div class="image">
        <a href="<?php echo $product['href']; ?>">
            <div class="cycle-slideshow"
                data-cycle-fx=scrollHorz
                data-cycle-timeout=0
                data-cycle-swipe=true
                >
                <div class="cycle-prev"></div>
                <div class="cycle-next"></div>
                <img class="productID_<?php echo $product['product_id']; ?>" src="<?php echo $product['thumb']; ?>" title="Click to see more details on <?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" />
            </div>
        </a>
    </div>

<?php } ?>

现在我最初在 mouseenter 上进行了这项工作,因此它对 for 循环中的每个 .image 都执行了此操作。现在我摆弄它来加载所有准备好的图像。这就是我目前所拥有的,但它只为 cycle2 幻灯片的每个实例加载第一个图像数组。

$( document ).ready(function() {
    var image = $(".cycle-slideshow").find("img:first");
    var classList = image.prop('class').split(/\s+/);
    $.each( classList, function(index, item){
        if( item.indexOf('productID_') > -1 ) {
            product_id = item.replace("productID_","");

            $.ajax({
                url: 'index.php?route=product/product/images',
                type: 'post',
                data: 'product_id='+product_id + '&width='+image.width() + '&height='+image.height(),
                dataType: 'json',
                success: function(json) {
                    if (json['images']) {
                        for (var i = 0; i < json['images'].length; i++) { 
                            image.after('<img src="'+json['images'][i]+'" width="'+image.width()+'" height="'+image.height()+'" alt="'+$(image).prop("alt")+'" />');
                        }

                        image.parent().cycle();
                    }
                }
            });
        }
    });
});

我已经用这条线尝试了各种方法:

var image = $(".cycle-slideshow").find("img:first");

与使用 .children 和 img:eq(0) 一样,但上述方法在正确引入图像尺寸参数的情况下效果最佳。

但是如何迭代,让每个 cycle2 实例都列出自己的 json 数组呢?

非常感谢任何帮助。

编辑以澄清下面的答案,看看它是否有帮助:

public function images() {
    $json = array();
    if (isset($this->request->post['product_id'])) {
        $this->load->model('catalog/product');
        $product_images = $this->model_catalog_product->getProductImages($this->request->post['product_id']);
        if ($product_images) {                      
            $this->load->model('tool/image');
            foreach ($product_images as $result) {
                if(($result['image'] && file_exists(DIR_IMAGE . $result['image'])) && isset($this->request->post['width']) && isset($this->request->post['height'])){
                    $json['images'][] =  $this->model_tool_image->resize($result['image'], $this->request->post['width'], $this->request->post['height']);
                }
            }
        }
    }   
    $this->response->setOutput(json_encode($json));
}

【问题讨论】:

    标签: jquery arrays json iteration jquery-cycle2


    【解决方案1】:

    我认为这是你需要的:

    $(document).ready(function() {
        $(".cycle-slideshow").each(function() {
            var image = $(this).find("img:first");
            // Rest of your code
        });
    });
    

    您的代码只是将image 设置为第一张幻灯片中的第一张图片,而从不处理其他幻灯片。

    其他建议:

    将产品 ID 放在 data- 属性中,这样您就不必从类中提取它。

    在您的 AJAX 调用中使用以下语法:

    data: {
        product_id: product_id,
        width: image.width(),
        height: image.height()
    }
    

    【讨论】:

    • 那是快速和真棒!好的,所以我使用了您的第一段代码......我知道我必须使用每个我只是没有得到放置/语法的。我是 JS/Jquery 菜鸟。也许我应该扩展其余的代码,因为我尝试了您发布的数据位并且图像大小都搞砸了。我在控制器文件中使用上面的代码...请参阅编辑。
    • 我必须说我真的不明白尺寸是怎么回事,但我得到的数据更改是:iphone_2-44x16.jpg 而不是:iphone_2-271x271.jpg
    • 我没有改变实际发送的数据,我对data:参数的改变只是告诉jQuery为你格式化。它确保一切都被正确编码,因为你忘了打电话给encodeURIComponent()
    • 嗯,好的,是的,我可以看到我的图像尺寸仍然搞砸了。它在第一组图像上很好,但之后尺寸很小然后被拉伸。多哈。需要调查一下。但是谢谢你的帮助,伙计。
    猜你喜欢
    • 2019-04-05
    • 2011-03-23
    • 2017-06-29
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多