Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Making items sortable

From: jQuery UI Widgets

Video: Making items sortable

jQuery UI is not just about interface widgets. And here's the JavaScript.

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.

Show transcript

This video is part of

Image for jQuery UI Widgets
jQuery UI Widgets

10 video lessons · 4209 viewers

Joe Chellman
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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

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 ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.