Created
June 4, 2013 20:32
-
-
Save lavoiesl/5709334 to your computer and use it in GitHub Desktop.
Symfony Form Collection add/delete links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
// setup an "add a tag" link | |
var addLink = $('<a class="btn btn-primary add"><i class="icon-plus icon-white"></i></a>'); | |
var deleteLink = $('<a class="btn btn-danger remove"><i class="icon-trash icon-white"></i></a>'); | |
function addLinkClick(e) { | |
e.preventDefault(); | |
var collection = $(this).closest('.collection-allow-add'); | |
var count = collection.children().length - 1; | |
var prototype = collection.data('prototype').replace(/__name__/g, count); | |
prototype = $(prototype).wrap('<li>').parent(); | |
if (collection.hasClass('collection-allow-delete')) { | |
prototype.append(deleteLink.clone().click(deleteLinkClick)); | |
} | |
$(this).parent().before(prototype); | |
} | |
function deleteLinkClick(e) { | |
e.preventDefault(); | |
$(this).parent().remove(); | |
} | |
$('form .collection-allow-delete').each(function(){ | |
$(this).children().append(deleteLink.clone().click(deleteLinkClick)); | |
}); | |
$('form .collection-allow-add').each(function(){ | |
if (!$(this).data('prototype').length) return; | |
var li = $('<li/>').append(addLink.click(addLinkClick)); | |
$(this).append(li); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment