0

I have two blocks, "draggable" and "sortable". Inside "draggable" I have few items which I can drag them to "sortable".

I want to have the possibility to click an item inside "draggable" and automatically drag it into "sortable".

Here is my JS:

$(".sortableList").sortable({
   placeholder: 'ui-state-highlight',
});

$('.sortableList').disableSelection();

$(".draggable").draggable({
 connectToSortable: '.sortableList',
 cursor: 'pointer',
 helper: 'clone',
 revert: 'invalid',
 start: function (event, ui) {
     $(this).addClass('testing');
 }
});

and this is a jsbin

Any ideas how can I drag the elements to "sortable" by clicking on them ?

4
  • I think I'm not following. You want to be able to create new draggables attached to the sortable list from the static items you have around? Commented Sep 29, 2014 at 12:27
  • No, I want to mimic the the behavior of dragging the element into the sortableList by clicking on the element I want to drag. Commented Sep 29, 2014 at 12:30
  • Ok, so you have items outside the sortable list and you want to add those you want, keeping others out, right? Commented Sep 29, 2014 at 12:30
  • Yes, you can see how the dragging works on the jsbin example. So I want to the same thing when I click on the items I normally have to drag. Commented Sep 29, 2014 at 12:31

2 Answers 2

1

Add a click handler, tell it to append the target to the sortable. Then refresh the select.

http://jsbin.com/fukutomometu/5/

 $("#sidebar-wrapper").on("click", ".draggable", function(e){
     $(".sortableList").append(e.target).sortable('refresh');
 });

You can also add and remove relevant classes from target if you don't want it to be draggable anymore.

And $.clone instead of $.append if you don't want to move the original. And change the order a bit:

$(e.target).clone().appendTo(".sortableList");

Relevant question:

Add to Jquery-ui sortable list

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried replacing append with clone but it doesn't work, any suggestions ?
@agis I've edited. Clone doesn't copyTo, it just clones and returns the clone.
0

Your problem is not with the coding, it's with the lack of some css.

Add this:

.sortableList li {
   width:150px; /*This was already set*/
   height:250px;
   background-color:blue;
}

Also, consider using and id for the sortable list, not a class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.