Working with ng2-dragula. I'm looking to update the orderNumber for every item in a database using the new dropped order.
dragulaService.dropModel.subscribe((value) => {
var drake = dragulaService.find('bag-orders').drake
var models = drake.models
console.log(models)
})
The new model order that it returns does not reflect the order within the bag.
TL;DR: has anyone implemented reordering within a database onDrop with ng2-dragula?
If you want to be able to drag items (without them disappearing) AND fire the dropModel event:
Put the [dragula] and [dragulaModel] directives in the parent element. (For example, contrary to the current doc where it says to put them in the
<li>
, you have to put them in the<ul>
Inject the dragularService, and, in the constructor of your component:
Call the dragulaService.setOptions for the bag (you can pass an empty dictionary). If you don't call it,
dropModel
callback is not firedSubscribe to dropModel
The result:
I found Marçal's answer incredibly helpful, and have even expanded on it a little to post an update to my DB to update the sequence value of each item (contacts within an organization, in my case) in the list:
HTML (container is the contact's organization. a contact, in my case, can't be moved between organizations):
JS (in my Contact Service, a PUT function to update the stored sequence values associated with each of my contacts, so that their orders persist):
}
JS (in my organization view component, to outline the actions to be taken upon drag and drop, respectively):
Hopefully this provides a bit more clarity on the matter to someone else in a spot similar to where I found myself today.
I was using the ng2-dragula and It's quite strange on something that I've noticed. The issue I had was the same. Server call is not updating the data object according to the dragged order.
I've just used if clause inside the ngDoCheck lifecycle hook to solve the issue.
It's sorted in the printed object in the console. After digging deeper a bit could find out in the network that the object that is sent with the update server call was the not updated one.
So, it's because the server call is made before the data object is updated.
All I did was adding a global variable which can keep track of drag has updated the data object.
Then, in the ngDoCheck lifecycle hook,
I finally found a solution. I have a horizontal list. The first two elements are the ones a I want to ignore and all have the
ignore
CSS class. And all elements have theitem
class. So markup:Then the TypeScript: