jQuery Masonry and Ajax Append Items?

前端 未结 13 1392
借酒劲吻你
借酒劲吻你 2020-12-08 07:19

I am trying to use some ajax and the jQuery Masonry plugin to add some items - but for some reason the new items aren\'t getting the masonry applied ?

I\'m using

相关标签:
13条回答
  • 2020-12-08 07:37
    var mediaItemContainer = $( '#container' );
    mediaItemContainer.masonry( {
        columnWidth:  '210px',
        itemSelector: '.item'
    } );
    $( mediaItemContainer ).prepend( '<div class="item">foo</div>' );
    $( mediaItemContainer ).masonry( 'reloadItems' );
    $( mediaItemContainer ).masonry( 'layout' );
    

    Solution

    0 讨论(0)
  • 2020-12-08 07:37

    I found a solution that works fine for me. it reloads every half second the layout of an instance of masonry.

    //initialization of a masonry object:
    
    var msnry = new Masonry("#container",{
    itemSelector: '#post',
    gutter: 15
    }); 
    
    //thread that makes the magic happens:
    
    setInterval(function(){
    msnry.reloadItems();
    msnry.layout();
    },500);
    

    this way, you can append things using ajax, and voilá, there is the masonry layout.

    0 讨论(0)
  • 2020-12-08 07:38

    Following has worked for me. I have an ajax which returns set of html items (returns a partial view, from the ajax) when I click a load more button in my web page. Below is the partial view, which is dynamically generated.

    foreach (var item in Model.SocialFeedList)
    {
            <div class="grid-item">
                <div class="grid-inner">
                    <div class="img-holder" style="background-image:url(imageURLHere)">
                    </div>
                    <div class="content-area">
                        <h3><a target="_blank" href="SomeLink">TitleOfTheLink</a></h3>
                        <p>SomeDescription</p>
                        <h5 class="date"><span>Published</span>: 2016/07/13</h5>
                    </div>
                </div>
            </div>
    }

    In the success callback ajax method, I have done the below,where "response" is the set of html items I get from the above html. Where "divFeedList" is the root element where I show the set of html elements.

    jQuery("divFeedList").append(response).masonry('reloadItems', response, true).masonry();

    Please let me know if the answer is unclear.

    0 讨论(0)
  • 2020-12-08 07:41

    For Masonry v3.2.2 (latest at the time of this post), this is what works:

    Assuming newHtml is a string like this:

    <li>item 1</li><!--split-->
    <li>item 2</li><!--split-->
    <li>item 3</li>
    

    You process it like this:

    $.get(apiUrl, function(newHtml) {
        var textArr = newHtml.split("<!--split-->");
        var elArr = [];
        $.each(textArr, function(i,v) {
            if (v) {
                elArr.push($(v)[0]);
            }
        });
        $(this).append(elArr);
        $container.waitForImages( function() {
            $container.masonry('appended', elArr);
        });
    }
    

    Took me 2 hours to find out this!

    0 讨论(0)
  • 2020-12-08 07:43

    I had the same problem with my ajax listing, i could solve it by calling reloadItems & layouts functions after ajax respond :

    var mediaItemContainer = $( '#container' );
    mediaItemContainer.masonry( {
        columnWidth:  '210px',
        itemSelector: '.item'
    } );
    $( mediaItemContainer ).prepend( '<div class="item">foo</div>' );
    $( mediaItemContainer ).masonry( 'reloadItems' );
    $( mediaItemContainer ).masonry( 'layout' );
    
    0 讨论(0)
  • 2020-12-08 07:44

    It appears that the masonry function expects a jQuery object as its second parameter and not a raw HTML string. You should be able to fix this by wrapping the success callback parameter like so:

    jQuery.ajax({
        type: "POST",
        url: ajax_url,
        data: ajax_data,
        cache: false,
        success: function (html) {
            if (html.length > 0) {
                var el = jQuery(html);
                jQuery("#content").append(el).masonry( 'appended', el, true );
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题