jQuery UI Widgets
Illustration by John Hersey

Making items sortable


From:

jQuery UI Widgets

with Joe Chellman

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Making items sortable

jQuery UI is not just about interface widgets. It also includes some interactions that you can use separately from any other widgets. In this movie, we're going to look at the sortable interaction. So, I have this little tiny gallery of thumbnails. And over here, is the markup. I have an unordered list, with an ID on it, and then every list element itselt, has an ID. This is going to be important, for reasons we'll see soon. And then I have my little trigger button, to make everything sortable. And here's the JavaScript.
please wait ...
Watch the Online Video Course jQuery UI Widgets
33m 53s Intermediate Nov 11, 2013

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.

Subject:
Web
Software:
jQuery
Author:
Joe Chellman

Making items sortable

jQuery UI is not just about interface widgets. It also includes some interactions that you can use separately from any other widgets. In this movie, we're going to look at the sortable interaction. So, I have this little tiny gallery of thumbnails. And over here, is the markup. I have an unordered list, with an ID on it, and then every list element itselt, has an ID. This is going to be important, for reasons we'll see soon. And then I have my little trigger button, to make everything sortable. And here's the JavaScript. This looks a lot like everything else we've seen in jQuery UI so far.

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, I'm going to open up my JavaScript console. And, now I'm going to trigger the sort again. And, start dragging this around. So, I'm just getting things into some different order. And now, here's how this works. I'm going to copy this call to sortable. And to get the current state of the sort, I call sortable again on the same thing, only I pass an argument. There are two choices. You can use toArray, which returns a JavaScript array of all the IDs. This is why each element having an ID was important. And it gives me back those IDs in the order that I've sorted them in here.

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.

So I'm going to save this, come back over, and reload, keeping my JavaScript console open. So I'm going to turn on sortable and then start shuffling things around. So I'm dragging things around, and nothing's happening. that's because I didn't actually log us to the console where we can see it. So, let's add that. Just wrap that whole thing in a console.log, reload, make them sortable. There we go. Now, every time I drag these around, that's getting passed out to the console. You can do something more sophisticated. Send an Ajax call out to a PHP application, or some other application on the back end.

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.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed jQuery UI Widgets.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.