2017-04-18 1 views
0

Ich habe ein li die Drag and Drop ist und funktioniert, wenn die Seite aktualisiert, aber wenn ich ein li mit AJAX in der Liste hinzufügen, das droppable Ereignis nicht zu ihm angebracht bekommt. Wie fügt man ein live fallbares Ereignis an das li an.JQuery Dropfähig hinzufügen Live-Event

Hier ist, was ich bisher ausprobiert habe,

$('.main-menu.workspaces li').droppable({ 
    hoverClass: "ui-state-hover", 
    tolerance: "pointer", 
    accept: '.notification-item.subtask', 
    drop: function (event, ui) { 
    var workspace_id = $(event.target).data('workspace'); 
    var task_id = $(ui.draggable[0]).data('taskid'); 
    $.post('{{route('workspace.assign', $subdomain)}}', { 
     workspace: workspace_id, 
     task: task_id 
    }).done(function(data) { 
    }); 
    } 
}); 
+1

Sie müssen 'abwerfbaren()' wieder auf den neuen Inhalt initialisieren, nachdem Sie es auf das DOM hinzufügen –

+0

sollte ich Funktion der abwerfbaren machen und rufen, wenn ich 'li' DOM hinzufügen? –

+0

Das klingt, als ob es funktionieren würde –

Antwort

0

Dank @Rory McCrossan für den Vorschlag und das ist, was ich tat, und es funktionierte.

(function($){ 
     var options = { 
      hoverClass: "ui-state-hover", 
      tolerance: "pointer", 
      accept: '.notification-item.subtask', 
      drop: function (event, ui) { 
      var workspace_id = $(event.target).data('workspace'); 
       var task_id = $(ui.draggable[0]).data('taskid'); 
       var href = $('.notification-body.todotask').attr('data-assignlink'); 
       $.post(href,{workspace: workspace_id, task: task_id}) 
       .done(function(data){ 
        //todo 
       }); 
      } 
      } 

     $('.main-menu.workspaces li').droppable(options); 


     // Attaching li to DOM using AJAX 
     function attachLi(){ 
      $.ajax({ 
      url: 'store', 
      method: 'POST', 
      data: { 
      task: searchString, 
      }, 
      success: function (response) { 
       //attaching li to DOM 
       $('.task').append(response.html); 

       //Reinitializing Droppable 
       $('.main-menu.workspaces li').droppable(options); 
      } 
      }); 
     } 

     ... 
     . 
     . 
})(jQuery);