2011-03-05 45 views
4

如何通過Ajax刪除項目時如何刷新砌體?這是我用來刪除該項目的代碼:刪除項目時刷新jquery.masonry

if($deletes = $('a[rel=delete]')) { 
    $deletes.click(function() { 
     if(!window.confirm('Are you sure you wish to delete this picture?')) 
      return false; 
     $t = $(this); 
     $.post(
      $t.attr('href'), 
      {}, 
      function(data) { 
       if(data == "success") 
        $t.parents('.deleteable:first').fadeOut(); 
      } 
     ); 
     return false;   
    }); 
} 

我想要刷新的原因是在刪除項目後刪除產生的空格。

+0

請考慮選擇一個正確的答案。 – Chango 2013-05-15 14:30:12

+0

[使用jquery masonry隱藏元素後重新組織所有元素]可能的重複(http://stackoverflow.com/questions/12086900/reorganize-all-elements-after-hiding-a-element-using-jquery-masonry) – Chango 2013-05-15 14:35:09

回答

1

我會說只是在選擇器上再次調用它。它似乎有a check來查看它是否被再次調用。

...snip... 
    // checks if masonry has been called before on this object 
    props.masoned = !!$wall.data('masonry'); 
...snip... 

我也想建議 saveOptions設置,因爲它似乎支持它重新調用。 沒關係,它似乎在默認情況下這樣做(D'哦!)

+0

我嘗試再次調用它,使用這個,但沒有效果: $('。masonry-wrapper')。masonry({columnWidth:188,singleMode:true,itemSelector:'.item'}); – Freeman 2011-03-06 00:11:53

4

添加一個回調到您​​實際調用.remove()您刪除的元素,一旦它的淡出,然後再打電話容器上.masonry()

+0

謝謝大家!這些建議奏效。 – Freeman 2011-03-06 01:32:10

+0

調用masonry再次爲我工作。謝謝! – jphreak 2014-05-18 19:59:00

+0

感謝這個有用的提示。適用於我的項目。 – 2017-04-18 08:34:07

1

在淡出回調中再次調用砌體。讓自己變得簡單,並在一個函數中進行砌體初始化。在那裏定義你的選項,這樣你就不必將選項帶入你的回調範圍。

像這樣

$(function(){ 

    var $bricks = $('your > elements'); 

    function BuildWall(){ 
    $bricks.masonry({your:'options'}); 
    } 

    BuildWall(); 


//Your code modified 
if($deletes = $('a[rel=delete]')) { 
    $deletes.click(function() { 
     if(!window.confirm('Are you sure you wish to delete this picture?')) 
      return false; 
     var $t = $(this); 
     $.post(
      $t.attr('href'), 
      {}, 
      function(data) { 
       if(data == "success") 
        $t.parents('.deleteable:first').fadeOut(function(){ 
         //This is faster than $(this).remove(); 
         $(this).empty().remove(); 
         //Reset Masonry 
         BuildWall(); 
        }); 
      } 
     ); 
     return false;   
    }); 
} 
}); 
相關問題