我正在使用JqueryUI Draggable和Sortable的組合來從網格中選擇圖像,並將它們拖入可重新排序的可排序容器中。問題是當我加載更多的圖像時,我需要重新初始化可拖動以包含添加的項目。jQueryUI重新初始化可拖動後可重複排序的元素
https://jsfiddle.net/Lpj8jthk/2/
初始化將
function initDragAndDrop(){
$(".ui-draggable").draggable({
opacity: 1.0,
helper: 'clone',
revert: 'invalid',
connectToSortable: '#drop-area'
});
$("#drop-area").sortable({
axis:"x",
connectWith: '.connectedSortable'
});
}
$(document).ready(function() {
load_draggable_images(track_page); //load content
initDragAndDrop();
});
負載更多按鈕handeler
$("#load-more-draggable-images").click(function (e) { //user clicks on button
track_page++; //page number increment everytime user clicks load button
load_draggable_images(track_page); //load content
initDragAndDrop();
});
function load_draggable_images(track_page){
// $('.animation_image').show(); //show loading image
$.post('includes/load-images.php', {'page': track_page}, function(data){
if(data.trim().length == 0){
//display text and disable load button if nothing to load
$("#load_more_button").text("No more records!").prop("disabled", true);
}
var parsed = JSON.parse(data);
// $("#images-container").empty();
$.each(parsed, function(k,v){
var name = v['filename'].split(".").shift()
var htmlString = "<div class='tile' data-timestamp='" + v['time']+ "'>" +
"<div>"+
"<img class='ui-draggable' src='" + v['thumbnail'] + "'> "+
"<p>" + name + "</p>"+
"</div> <br> "+
"</div> ";
$("#images-container").append(htmlString);
});
initDragAndDrop();
//scroll page to button element
// $("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 800);
//hide loading image
// $('.animation_image').hide(); //hide loading image once data is received
});
}
https://jsfiddle.net/Lpj8jthk/2/
這裏的東西沒有加起來。您是否通過將其拖動到可排序或通過單擊按鈕來添加更多項目?後者需要「刷新」我想。你能提供一個小問題的例子嗎? – Twisty
@Twisty'jquery.post()'('$ .post()')發送一個AJAX post請求,結果被附加到容器。 – Lonergan6275
好吧,添加項目之後,你想'刷新'像這樣:http://api.jqueryui.com/sortable/#method-refresh – Twisty