Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery UI sortable item position on change event

In JqueryUI sortable, When the user drags a item, change event called. I got this on below example.

jsFiddle code here

stop: function(event, ui) {
        console.log("New position: " + ui.item.index());
    },
    change: function(event, ui) {
        // When the item postion is changed while dragging a item, I want it's position
        console.log("New position: " + ui.item.index());
    }

In my code When the user drags a item (Item7) and changes the position , while dragging a item and changing the position . I want to get the position of a dragging item.

I just saw a answer here : jQuery UI Sortable Position

In the above link, position value (ui.item.index()) is accessed on Stop function, while dragging a item, I want it's position (on change function) ? Any Idea ?

I need this because when the user tries to place a item7 after a item2, I will do some validation getting values from previous Item2 (If I know the current position of the dragging item, Then only I can access previous item) . If the validation is success, It can be placed or I have to show some message.

like image 332
Muthu Avatar asked Jun 18 '13 04:06

Muthu


People also ask

How to use sortable update event in jQuery?

The jQuery UI Sortable update event is used to trigger when the user stopped sorting and the DOM position has changed. Initialize the sortable widget with the update callback function. Bind an event listener to the sortable update event. event: This event is triggered when the user stopped the sorting with a position change of DOM.

How do I sort a list in jQuery UI?

JqueryUI - Sortable. 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 to reorder elements in a list or grid using jQuery UI?

Description: Reorder elements in a list or grid using the mouse. The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse. Note: In order to sort table rows, the tbody must be made sortable, not the table. The sortable widget uses the jQuery UI CSS framework to style its look and feel.

What happens when a sortable item is moved from one sortable?

The sortable that the item comes from if moving from one sortable to another. The jQuery object representing the element being used as a placeholder. This event is triggered when a sortable item is moved away from a sortable list. Note: This event is also triggered when a sortable item is dropped.


1 Answers

If you want to get the index of what's currently hovered, try using placeholder.

change: function(event, ui) {
      console.log("New position: " + ui.placeholder.index());
}

FIDDLE

like image 188
Jude Duran Avatar answered Nov 04 '22 12:11

Jude Duran