Making items sortable
- Next steps
Viewers: in countries Watching now:
Despite the possibilities offered in HTML5, there's still a call for customizable widgets that can be used in websites and web apps. jQuery UI, a popular plugin for jQuery, is here to answer that call. Joe Chellman shows how to install the plugin, use the accordion and date picker widgets, and add behaviors that change how existing page elements respond to user input. Finally, you'll apply the concepts you've learned to a typical project you might see coming from a client—a survey form.
This course is a short companion piece to jQuery for Web Designers. See that course for information about building your core jQuery skill set.
Making items sortable
We select an element and then call a method on it. In this case, that method is sortable. So switching over here, I'm going to refresh to make sure I have the freshest copy. And now if I just drag things around, you can see there's no sorting happening. I just kind of grab the image, and this is the operating system taking over the drag. Now if I click my trigger, I can click these images, and they are sortable. You can take a look at the CSS in the UI examples, CSS file. But this is basic stuff. They're just all floated to the left with a little bit of margin around each one.
jQuery UI sortable takes care of everything else. Shifting things around, letting me move things into different parts of this little floated grid. It's all taken care of for me. Now, the tricky thing is, that after I've done all this, if I reload, all my sorting is gone. So, when you're using one of these interactions, it's not enough just to make the elements sortable. You're going to have to have some sort of other process in place to save the results of your sort. There are, of course, lots of ways that you could do this. We're not going to go through the whole thing. But, I am going to show you how to get the sort out of the sortable widget.
So watch. Two and four are the first two right now. So I'm going to swap them and call this again. And there we go, now you can see that four is the first one and two is second. So every time I update this, if I call sortable again, passing two array as the first argument, I'll get it back. So, that's one way to use it. You can also use serialize. And this gives you something that you could pass into, say, a PHP application. Something like that. Those are the two versions that sort of we'll give you by default. One more thing I'll mention, if you're working in the context of Ajax or something else, where you want the updates to be saved instantly whenever you make an update.
There is a callback available. And so if I set some options unsortable. There's one called update. And this is a function, generally we use an anonymous function for one off like this. And it takes two arguments. We're not going to use those right now, but it does take two arguments for when you're implementing this with a developer at some point. And so then, every time that you call this, you can use whichever version of this is appropriate for the application that you're writing. And because we've already selected sort me up here I can just use this, call sortable, and then to array.
There are lots of options. It just depends on what you and maybe other people on your team are building. So that's a look at how to use the sortable interaction. It makes it very easy to make objects on your webpage sortable with the mouse. And then you can take those results and process them in another application afterwards.
There are currently no FAQs about jQuery UI Widgets.