Skip to content

Instantly share code, notes, and snippets.

@ramontayag
Created June 8, 2011 07:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramontayag/1013977 to your computer and use it in GitHub Desktop.
Save ramontayag/1013977 to your computer and use it in GitHub Desktop.
serialize.js
serialize: function(o) {
o = o || {};
var ret = [];
$(this.element).children('li').each(function() { _recursiveItems($(this)); });
return ret.join("&");
function _recursiveItems(li) {
var id = li.attr("id");
var parentId = 'root';
var parent = li.parent(o.listType).parent('li');
if (parent.length > 0) {
parentId = parent.attr('id');
}
children_list_items = li.children(o.listType).children('li')
var position = li.index();
if (children_list_items.length > 0) {
children_list_items.each(function() {
_recursiveItems($(this));
});
}
var resource = li.attr("id").match(o.expression || (/(.+)[-=_](.+)/));
var resourceName = o.key || resource[1];
var serializedId = '[' + id +']';
var container = resourceName + serializedId;
ret.push(container + '[parent_id]='+ parentId);
ret.push(container + '[position]='+ position);
//return item;
}
},
@tkremmel
Copy link

Hi,
just stumbled over this source. It is exactly what I was looking for. Thanks for that!
I have only one question which you might can answer:

http://stackoverflow.com/questions/11714299/jquery-nested-sortable-how-to-get-position-and-parent-where-node-was-dropped

Would be great if you could have a look :)
All the best,
Tom

@ramontayag
Copy link
Author

Done :)

@tkremmel
Copy link

tkremmel commented Jul 30, 2012 via email

@ramontayag
Copy link
Author

Haha :) When you come to the Philippines, sure!

@tkremmel
Copy link

tkremmel commented Jul 30, 2012 via email

@ramontayag
Copy link
Author

ramontayag commented Jul 30, 2012 via email

@stringPuller
Copy link

Hello ramontayag,

sorry for being so late to this gist, but it so happens that I just stumbled upon your improvements and they really helped me.
Could you give me a hint on how to get the level of

  • into the serialized array?
    My database asks for
    ~ item_id
    ~ parent_id
    ~ position
    and
    ~ level (depth)
    I don't know how to get this last one working.
    There is a "_getLevel" function in the code, but I can't get it to work.

    If you could help me with this, I would really appreciate.

    Kind Regards,
    Stefan

  • Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment