diff --git a/.gitignore b/.gitignore index 4321639..4678b88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -*.swp -**/*.swp +*.sw* +**/*.sw* diff --git a/README.md b/README.md index b2e3edd..6a0a902 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Please note: every `
  • ` must have either one or two direct children, the first
    serialize
    -
    Serializes the nested list into a string like setName[item1Id]=parentId&setName[item2Id]=parentId, reading from each item's id formatted as 'setName_itemId' (where itemId is a number). +
    Serializes the nested list into a string like setName[setName_item1Id][parent_id]=parentId&setName[setName_item1Id][position]=position..., reading from each item's id formatted as 'setName_itemId' (where itemId is a number). It accepts the same options as the original Sortable method (key, attribute and expression).
    toArray
    Builds an array where each element is in the form: @@ -97,4 +97,4 @@ Tested with: IE 6/7/8, Firefox 3.6/4, Chrome, Safari 3 This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. This work is *pizzaware*. If it saved your life, or you just feel good at heart, please consider offering me a pizza. This can be done in two ways: (1) use the Paypal button at the bottom of the project's [home page](http://mjsarfatti.com/sandbox/nestedSortable); (2) send me cash via traditional mail to my home address in Italy. Is the second method legal? It is in Italy if you use Posta assicurata. You should check with your local laws if you live elsewhere. - \ No newline at end of file + diff --git a/jquery.ui.nestedSortable.js b/jquery.ui.nestedSortable.js index 7b82b78..5411e92 100644 --- a/jquery.ui.nestedSortable.js +++ b/jquery.ui.nestedSortable.js @@ -1,6 +1,6 @@ /* * jQuery UI Nested Sortable - * v 1.3.5 / 28 apr 2011 + * v 1.3.6 / 28 apr 2011 * http://mjsarfatti.com/sandbox/nestedSortable * * Depends: @@ -200,22 +200,46 @@ $.ui.sortable.prototype._mouseStop.apply(this, arguments); },//_mouseStop serialize: function(o) { + // 'o' is a hash of the options of the plugin (?) + + // items is an array the dom elements lifted from the page, so we can work on them. + // It looks something like [li#menu_item_37.menu_item, li#menu_item_38.menu_item, ...] var items = this._getItemsAsjQuery(o && o.connected), - str = []; o = o || {}; + str = []; o = o || {}; + // Now we go through each of these items $(items).each(function() { var res = ($(o.item || this).attr(o.attribute || 'id') || '') - .match(o.expression || (/(.+)[-=_](.+)/)), - pid = ($(o.item || this).parent(o.listType) - .parent('li') - .attr(o.attribute || 'id') || '') - .match(o.expression || (/(.+)[-=_](.+)/)); - - if (res) { - str.push((o.key || res[1] + '[' + (o.key && o.expression ? res[1] : res[2]) + ']') - + '=' - + (pid ? (o.key && o.expression ? pid[1] : pid[2]) : 'root')); - } + .match( + o.expression || (/(.+)[-=_](.+)/)), + pid = ($(o.item || this).parent(o.listType) + .parent('li') + .attr(o.attribute || 'id') || '') + .match(o.expression || (/(.+)[-=_](.+)/)); + + // At this point... + // 'res' looks like this: ["menu_item_47", "menu_item", "47"] + // pid looks like this, IF the item (res) has a parent: ["menu_item_46", "menu_item", "46"] + + // Here we setup the response's parent_id + if (res) { + // Updated the following line to have res[0] (instead of res[2]), which will make the itemName be menu_item_47 + itemName = (o.key && o.expression ? res[1] : res[0]); + + // itemScope looks like this: menu_item[menu_item_37] + itemScope = o.key || res[1] + '[' + itemName + ']' + + //create the parentId string, which will look like menu_item[menu_item_37][parent_id]=root + parentStr = itemScope + '[parent_id]=' + (pid ? (o.key && o.expression ? pid[1] : pid[2]) : 'root') + + //create the position string, which will look like menu_item[menu_item_37][position]=0 + //this is 0-based position + positionStr = itemScope + '[position]=' + $(this).index(); + + //push the parentStr and positionStr strings into the str array + str.push(parentStr); + str.push(positionStr); + } }); if(!str.length && o.key) {