NOTE: As of July 12, 2009, this blog has been discontinued and replaced by the new Thought Delimited blog. All of the entries in this blog can be found there, along with new posts.

Sorting a Nested List Using jQuery And Saving the Changes

A screenshot of the sorting demoA reader asked me if it was possible to sort/rearrange items in nested lists (lists within a main list) using the jQuery UI Sortables code. I told him that it was, and I had actually created a tool for a client recently that made use of that feature.

He wanted to know more about how to do it, so I went ahead and created a downloadable working demo of how to do it.

You can view an on-line version of the demo (and get the downloadable zip file) here:

Nested List Sort Demo

The drag-and-drop action that lets you move the items around is the easiest part, as that's simply the Sortables code doing its work. Most of the work involved the code that generates the list for display (written in ColdFusion but do-able in other languages) and the JavaScript code that leverages the core jQuery selectors to scan the list and create a string of data that represents the structure and order of entire list. Both sets of code use a recursive function anytime a nested list is encountered, making it possible to have an unlimited number of nested lists within the main list or any other nested list.

The hierarchy structure is tracked/described through the use of parent ids. Each item in the list has a database record that includes the unique record id of its parent (with items in the main list having a parent id of 0). The JavaScript code records the id of the parent DOM element for each item as it scans the list, and that information becomes part of the string that is submitted via the form. The order of the items is simply the byproduct of the JavaScript code traversing down the list.

Like the tool I wrote for rearranging items in a single list (see related entry), this is simply another means of letting users rearrange the display of records when the order of display can be arbitrary or a matter of preference rather than a strict alphanumeric sort.

Comments
Thanks a lot for posting this, Brian. I' m looking forward to digging into the code. Cheers!
# Posted By Tony Garcia | 1/28/08 8:22 AM
Thanks a lot for posting this, Brian. I' m looking forward to digging into the code. Cheers!
# Posted By runescape money | 9/10/08 10:05 PM
hey i m testing
# Posted By abcd | 9/23/08 8:13 AM
I'm real excited about your script but I've noticed that when dragging multiple list items a second 'ghost' li appears. This ghost follows around the selected content and once released everything seems fine but it doesn't look too pretty. It does this on every browser I've tested. Any idea what's causing this?
# Posted By bKozora | 9/23/08 4:13 PM
@bKozora: Not sure why it's doing that. There may simply be too many elements in play when moving a block of items.

I've actually stopped using the Sortables UI plugin for moving around nested lists: I have another blog post where I described a sorting system that temporarily creates clickable targets for moving items or blocks of items:

http://www.swartzfager.org/blog/index.cfm/2008/3/2...

...I find the new method to be easier to use because I don't have to guess where I need to drop an item so it falls into a nested list or just outside it: the targets provide a visual cue for that.
# Posted By Brian Swartzfager | 9/24/08 7:52 AM
This is killer stuff, Brian - the performance is way better in IE7 than the prototype/script.aculo.us based approach I had been using to rearrange a very deep nested list.

Building upon your example, I updated the JQuery ui version to 1.5.2 and I can now drag items between two nested lists using the connectWith argument. http://ui.jquery.com/repository/latest/demos/funct...

Thanks.
# Posted By Ted H. | 11/6/08 4:37 PM
Wow, thanks for posting this. I've been looking around for a while on how to do this.
# Posted By Aimee | 3/11/09 4:22 PM
I was in need of this type of example to integrate on one of the projects I am working on.

Thanks a lot.
# Posted By Ganga Bohara | 3/20/09 5:49 AM
I'm real excited about your script but I've noticed that when dragging multiple list items a second 'ghost' li appears. This ghost follows around the selected content and once released everything seems fine but it doesn't look too pretty. It does this on every browser I've tested. Any idea what's causing this?
# Posted By online drugstore | 1/16/10 5:34 AM
<A href="http://dks.g-ded.com/">?oノ￯ツᄁヒiメテ</A><A href="http://dks.g-ded.com/">?oノ￯ツᄁテJテtテF</A><A href="http://www.g-ded.com/">テeテフテNテノ</A><A href="http://frn.f-f2.com/">ユsラᅬ</A><A href="http://sernd.f-f2.com/">テZテbテNテXテtテフテモテh</A><A href="http://ser.f-f2.com/">テZテtテフ</A><A href="http://apiencompass.com/">?oノ￯ツᄁ</A><A href="http://c-mmm.com/">?oノ￯ツᄁ</A><A href="http://www.deaide.net/">?oノ￯ツᄁフfホᆭヤᅡ</A><A href="http://www.is-pro.com/">?oノ￯ツᄁ</A><A href="http://www.v-v1.com/">?oノ￯ツᄁ</A><A href="http://www.110874.net">?oノ￯ツᄁ?l?￈</A><A href="http://hzk.ppccd.com/">ユラムᆳ</A><A href="http://dhr.ppccd.com">テfテハテwテヒ</A><A href="http://dhs.ppccd.com/">テfテハテoテハ?[テwテヒテX</A><A href="http://dai.uucrr.com/">?oノ￯ツᄁ</A><A href="http://dam.uucrr.com/">?oノ￯ツᄁヨᄈラ﾿</A><A href="http://www.117dm.com/livechat/">テtテBテハテsテモテノテCテuテ`テテテbテg</A><A href="http://www.liveandme.tv/">テAテ_テヒテgテノテCテuテ`テテテbテg</A><A href="http://www.deriheru.org/">テfテハテwテヒ</A>
# Posted By likaida | 1/22/10 12:22 AM
<A href="http://amcalpha.com/">ユsモᆴホY</A><A href="http://absolute-erotica.com/">テ\?[テvテノテモテh</A><A href="http://11-counter.com/">テAテNテZテXテJテEテモテ^?[</A><A href="http://cholesterol.goteck.com/">テRテフテXテeテ??[テヒ</A><A href="http://cholesterol.goteck.com/">メニ?ᆱホノヨb</A><A href="http://kafunshou.goteck.com/">ノᅯユᄇ?ᅦ</A><A href="http://david-gagnon.com/">?￝ム○テ??[テN</A><A href="http://naisyoku.david-gagnon.com/">モ¢?E</A><A href="http://albait.david-gagnon.com/">?￝ム○テAテヒテoテCテg</A><A href="http://kansouhada.abcd-abcd.com/">ハᆪムヌヤᄃ</A><A href="http://daieto.abcd-abcd.com/">テ_テCテGテbテg?@?Hホヨ</A><A href="http://sapuri.abcd-abcd.com/">テTテvテハテ?テモテg</A><A href="http://uranai.medikaru.com/">ヨᄈラ﾿?│ツᄁ</A><A href="http://1trustmortgage.com/">?oノ￯ツᄁホRフ</A><A href="http://tjj.tjj.jp/">テNテフテWテbテgテJ?[テhフᄏヒ¢ノᄏ</A><A href="http://kck.tjj.jp/">テNテフテWテbテgフᄏヒ¢ノᄏ</A><A href="http://www.bblive.tv/">テノテCテuテ`テテテbテg</A><A href="http://www.117dm.com/">テtテBテハテsテモ</A><A href="http://www.zaitaku-bb.com/index1.shtml">テ`テテテbテgテフテfテB</A><A href="http://pczaitaku.david-gagnon.com/">テpテ\テRテモ?￝ム○テ??[テN</A>
# Posted By likaida | 1/22/10 12:23 AM
<A href="http://nikibi.goteck.com/">テjテLテr</A><A href="http://www.ibf-usba.com/">Cholesterol</A><A href="http://mizumushi.p-qq.net/">?ナメホ</A><A href="http://hieshou.goteck.com/">ラ¬ツᆭ?ᆱ</A><A href="http://mukumi.goteck.com/">ツ￞ツᆳツ￝</A><A href="http://sango.goteck.com/">ホYフ ̄</A><A href="http://wakiga.goteck.com/">ツ■ツᆱツᆰ</A><A href="http://medikaru.com/">ホᅯヤテホ₩</A><A href="http://www.s-tsk.com/">モ]?E</A><A href="http://kareisyu.goteck.com/">ノ￁ラ○?L</A><A href="http://www.x-ppd.com/">モVムRテIテハテSモワ</A><A href="http://p-qq.net/">メᅵ?ナメホ</A><A href="http://anthonypontiacbuick.com/">ビモsツᆱツ¢ツᅩ</A><A href="http://c-kiss.com/">?￝ム○ホdホヨ</A>
# Posted By likaida | 1/22/10 12:24 AM
<A href="http://kumamoto.awdccs.com/">?oノ￯ツᄁフFヨ{</A><A href="http://kyoto.awdccs.com/">?oノ￯ツᄁビモs</A><A href="http://mie.awdccs.com/">?oノ￯ツᄁホO?d</A><A href="http://miyagi.awdccs.com/">?oノ￯ツᄁヒ{?←</A><A href="http://miyazaki.awdccs.com/">?oノ￯ツᄁヒ{?│</A><A href="http://nagano.awdccs.com/">?oノ￯ツᄁメᄋヨ↓</A><A href="http://nagasaki.awdccs.com/">?oノ￯ツᄁメᄋ?│</A><A href="http://nara.awdccs.com/">?oノ￯ツᄁモ￞ラᅦ</A><A href="http://niigata.awdccs.com/">?oノ￯ツᄁ?Vハテ</A><A href="http://oita.awdccs.com/">?oノ￯ツᄁム¥ユᆰ</A><A href="http://okayama.awdccs.com/">?oノ￯ツᄁノᆰホR</A><A href="http://okinawa.awdccs.com/">?oノ￯ツᄁノᆱモ↑</A><A href="http://oosaka.awdccs.com/">?oノ￯ツᄁム¥? ̄</A><A href="http://saga.awdccs.com/">?oノ￯ツᄁ?ᄇノ↑</A><A href="http://saitama.awdccs.com/">?oノ￯ツᄁ?←ヒᅧ</A><A href="http://shiga.awdccs.com/">?oノ￯ツᄁホᅠノ↑</A><A href="http://shimane.awdccs.com/">?oノ￯ツᄁモヌ?ᆰ</A><A href="http://shizuoka.awdccs.com/">?oノ￯ツᄁ?ᅢノᆰ</A><A href="http://tochigi.awdccs.com/">?oノ￯ツᄁモ￈ヨ￘</A><A href="http://tokushima.awdccs.com/">?oノ￯ツᄁモ﾿モヌ</A>
# Posted By likaida | 1/22/10 12:26 AM
<A href="http://tokyo.awdccs.com/">?oノ￯ツᄁモフビ</A><A href="http://tottori.awdccs.com/">?oノ￯ツᄁメᄍホ₩</A><A href="http://toyama.awdccs.com/">?oノ￯ツᄁユxホR</A><A href="http://wakayama.awdccs.com/">?oノ￯ツᄁリaノᅩホR</A><A href="http://yamagata.awdccs.com/">?oノ￯ツᄁホRフ`</A><A href="http://yamaguchi.awdccs.com/">?oノ￯ツᄁホRフ</A><A href="http://yamanashi.awdccs.com/">?oノ￯ツᄁホRラワ</A><A href="http://kitakyuusyuu.awdccs.com/">?oノ￯ツᄁヨkヒ ̄?B</A><A href="http://shimonoseki.awdccs.com/">?oノ￯ツᄁノᄎハᅱ</A><A href="http://kawasaki.awdccs.com/">?oノ￯ツᄁ?↓?│</A><A href="http://koube.awdccs.com/">?oノ￯ツᄁ?_フᅨ</A><A href="http://kurume.awdccs.com/">?oノ￯ツᄁヒvラᆵユᅣ</A><A href="http://mito.awdccs.com/">?oノ￯ツᄁ?ナフᅨ</A><A href="http://nagoya.awdccs.com/">?oノ￯ツᄁヨᄐフᅢノᆴ</A><A href="http://oomuta.awdccs.com/">?oノ￯ツᄁム¥ヨᄡモc</A><A href="http://saitama-shi.awdccs.com/">?oノ￯ツᄁ?←ヒᅧ</A><A href="http://sakai.awdccs.com/">?oノ￯ツᄁ?¦</A><A href="http://sendai.awdccs.com/">?oノ￯ツᄁ?¥ム¦</A><A href="http://yokohama.awdccs.com/">?oノ￯ツᄁノᄀユl</A><A href="http://yokosuka.awdccs.com/">?oノ￯ツᄁノᄀ?{ノ↑</A><A href="http://sapporo.awdccs.com/">?oノ￯ツᄁホDヨy</A><A href="http://kawasaki.awdccs.com/">?oノ￯ツᄁ?↓?│</A>
# Posted By likaida | 1/22/10 12:27 AM
<A href="http://apiencompass.com/fukui/">ユ゚ネ¦?oノ￯ツᄁ</A><A href="http://apiencompass.com/aichi/">ネᄂメm?oノ￯ツᄁ</A><A href="http://apiencompass.com/gifu/">ハ￲ユフ?oノ￯ツᄁ</A><A href="http://apiencompass.com/shizuoka/">?ᅢノᆰ?oノ￯ツᄁ</A><A href="http://apiencompass.com/mie/">ホO?d?oノ￯ツᄁ</A><A href="http://apiencompass.com/hyogo/">ユᄎフ￉?oノ￯ツᄁ</A><A href="http://apiencompass.com/osaka/">ム¥? ̄?oノ￯ツᄁ</A><A href="http://apiencompass.com/wakayama/">リaノᅩホR?oノ￯ツᄁ</A><A href="http://apiencompass.com/shiga/">ホᅠノ↑?oノ￯ツᄁ</A><A href="http://apiencompass.com/kyoto/">ビモs?oノ￯ツᄁ</A><A href="http://apiencompass.com/nara/">モ￞ラᅦ?oノ￯ツᄁ</A><A href="http://apiencompass.com/yamaguchi/">ホRフ?oノ￯ツᄁ</A><A href="http://apiencompass.com/tottori/">メᄍホ₩?oノ￯ツᄁ</A><A href="http://apiencompass.com/shimane/">モヌ?ᆰ?oノ￯ツᄁ</A><A href="http://apiencompass.com/okayama/">ノᆰホR?oノ￯ツᄁ</A><A href="http://apiencompass.com/hiroshima/">?Lモヌ?oノ￯ツᄁ</A><A href="http://apiencompass.com/tokushima/">モ﾿モヌ?oノ￯ツᄁ</A><A href="http://apiencompass.com/kagawa/">???↓?oノ￯ツᄁ</A><A href="http://apiencompass.com/ehime/">ネᄂユQ?oノ￯ツᄁ</A><A href="http://apiencompass.com/kochi/">?ツメm?oノ￯ツᄁ</A>
# Posted By likaida | 1/22/10 12:28 AM
<A href="http://kumamoto.archercomm.com/">?oノ￯ツᄁフFヨ{</A><A href="http://kyoto.archercomm.com/">?oノ￯ツᄁビモs</A><A href="http://mie.archercomm.com/">?oノ￯ツᄁホO?d</A><A href="http://miyagi.archercomm.com/">?oノ￯ツᄁヒ{?←</A><A href="http://miyazaki.archercomm.com/">?oノ￯ツᄁヒ{?│</A><A href="http://nagano.archercomm.com/">?oノ￯ツᄁメᄋヨ↓</A><A href="http://archercomm.com/">?oノ￯ツᄁメᄋ?│</A><A href="http://nara.archercomm.com/">?oノ￯ツᄁモ￞ラᅦ</A><A href="http://niigata.archercomm.com/">?oノ￯ツᄁ?Vハテ</A><A href="http://oita.archercomm.com/">?oノ￯ツᄁム¥ユᆰ</A><A href="http://okayama.archercomm.com/">?oノ￯ツᄁノᆰホR</A><A href="http://okinawa.archercomm.com/">?oノ￯ツᄁノᆱモ↑</A><A href="http://oosaka.archercomm.com/">?oノ￯ツᄁム¥? ̄</A><A href="http://saga.archercomm.com/">?oノ￯ツᄁ?ᄇノ↑</A><A href="http://saitama.archmaganda7ercomm.com/">?oノ￯ツᄁ?←ヒᅧ</A><A href="http://shiga.archercomm.com/">?oノ￯ツᄁホᅠノ↑</A><A href="http://shimane.archercomm.com/">?oノ￯ツᄁモヌ?ᆰ</A><A href="http://shizuoka.archercomm.com/">?oノ￯ツᄁ?ᅢノᆰ</A><A href="http://tochigi.archercomm.com/">?oノ￯ツᄁモ￈ヨ￘</A><A href="http://tokushima.archercomm.com/">?oノ￯ツᄁモ﾿モヌ</A>
# Posted By likaida | 1/22/10 12:28 AM
<A href="http://dks.g-ded.com/fukui/">ユ゚ネ¦?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/aichi/">ネᄂメm?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/gifu/">ハ￲ユフ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/shizuoka/">?ᅢノᆰ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/mie/">ホO?d?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/hyogo/">ユᄎフ￉?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/osaka/">ム¥? ̄?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/wakayama/">リaノᅩホR?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/shiga/">ホᅠノ↑?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/kyoto/">ビモs?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/nara/">モ￞ラᅦ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/yamaguchi/">ホRフ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/tottori/">メᄍホ₩?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/shimane/">モヌ?ᆰ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/okayama/">ノᆰホR?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/hiroshima/">?Lモヌ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/tokushima/">モ﾿モヌ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/kagawa/">???↓?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/ehime/">ネᄂユQ?oノ￯ツᄁテJテtテF</A><A href="http://dks.g-ded.com/kochi/">?ツメm?oノ￯ツᄁテJテtテF</A>
# Posted By likaida | 1/22/10 12:29 AM
<A href="http://www.g-ded.com/fukui/">ユ゚ネ¦テeテフテNテノ</A><A href="http://www.g-ded.com/aichi/">ネᄂメmテeテフテNテノ</A><A href="http://www.g-ded.com/gifu/">ハ￲ユフテeテフテNテノ</A><A href="http://www.g-ded.com/shizuoka/">?ᅢノᆰテeテフテNテノ</A><A href="http://www.g-ded.com/mie/">ホO?dテeテフテNテノ</A><A href="http://www.g-ded.com/hyogo/">ユᄎフ￉テeテフテNテノ</A><A href="http://www.g-ded.com/osaka/">ム¥? ̄テeテフテNテノ</A><A href="http://www.g-ded.com/wakayama/">リaノᅩホRテeテフテNテノ</A><A href="http://www.g-ded.com/shiga/">ホᅠノ↑テeテフテNテノ</A><A href="http://www.g-ded.com/kyoto/">ビモsテeテフテNテノ</A><A href="http://www.g-ded.com/nara/">モ￞ラᅦテeテフテNテノ</A><A href="http://www.g-ded.com/yamaguchi/">ホRフテeテフテNテノ</A><A href="http://www.g-ded.com/tottori/">メᄍホ₩テeテフテNテノ</A><A href="http://www.g-ded.com/shimane/">モヌ?ᆰテeテフテNテノ</A><A href="http://www.g-ded.com/okayama/">ノᆰホRテeテフテNテノ</A><A href="http://www.g-ded.com/hiroshima/">?LモヌテeテフテNテノ</A><A href="http://www.g-ded.com/tokushima/">モ﾿モヌテeテフテNテノ</A><A href="http://www.g-ded.com/kagawa/">???↓テeテフテNテノ</A><A href="http://www.g-ded.com/ehime/">ネᄂユQテeテフテNテノ</A><A href="http://www.g-ded.com/kochi/">?ツメmテeテフテNテノ</A>
# Posted By likaida | 1/22/10 12:29 AM
Yeah, great to see a simple but effective method of dealing with application events in the true jQuery fashion :)

Nova,
http://novablog.co.cc
# Posted By Nova | 1/22/10 10:22 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.