Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to control the sorting of elements when using jquery ui

I have a sortable div (#sortable) with elements (.draggable) inside it. In there, when I sort elements from bottom to up, the elements can easily be sorted by dragging up and I don't have to drag much to the top. But when sorting elements from up to bottom, I have to drag the element far below then wanted. Is there any way to control the sorting of elements, so that even if I drag just a little bit up/below and not all the way up/down it will display the placeholder to place the element respectively?

Demo at codepen.

js

  $('#content #sortable').sortable({
    handle: '.drag_handle',
    placeholder: "ui-state-highlight",
    axis: "y"
  });

  $('#blocks .draggable').draggable({
    helper: "clone",
    revert: "invalid",
    connectToSortable: '#content #sortable'
  });

update

Sorting upwards

Sorting upwards is easy as I don't have to drag the element all the way up above the element I want to sort with.

enter image description here

Sorting downwards

Sorting downwards is difficult as I have to drag the element all the way down below the element I want to sort with.

enter image description here

like image 875
Aamu Avatar asked Oct 03 '15 13:10

Aamu


People also ask

How to use jQuery UI sortable?

By default its value is "input,textarea,button,select,option". This option is a selector that identifies another sortable element that can accept items from this sortable. This allows items from one list to be moved to other lists, a frequent and useful user interaction. If omitted, no other element is connected.

How does jQuery sortable work?

jQueryUI provides sortable() method to reorder elements in list or grid using the mouse. This method performs sortability action based upon an operation string passed as the first parameter.

How do you drag and drop in jQuery?

Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port. If the value of this option is set to false, it will prevent the DOM elements to be dragged .


1 Answers

Just change the tolerance option of the sortable API to "pointer". This option will make the draggable considered to be on the placeholder when the cursor is over it (what you need right now) instead of wait until over 50% of the element is overlapping the placeholder. This was why you had to scroll way too far down in order to sort it.

$('#content #sortable').sortable({
    handle: '.drag_handle',
    placeholder: "ui-state-highlight",
    tolerance: "pointer"
});

Here is the updated CodePen

like image 168
Alvaro Flaño Larrondo Avatar answered Oct 16 '22 16:10

Alvaro Flaño Larrondo