Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Added additional data attributes for collection type output #10071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

[Form] Added additional data attributes for collection type output #10071

wants to merge 1 commit into from

Conversation

leevigraham
Copy link
Contributor

Refactored and up to date w/ master version of: #7713

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets na
License MIT
Doc PR WIP
  • fix the tests as they have not been updated yet
  • submit changes to the documentation

I've added 4 extra data-* attributes to collections in twig and php views for easier js manipulation.

New attributes:

  • data-form-widget='collection': This is a collection and all children are form types
  • data-collection-allow-add: Allow adding of new elements
  • data-collection-allow-delete: Allow deletion of elements (new and existing)
  • data-prototype-name: The prototype name

This should make javascript integration much easier.

Example js:

$('[data-form-widget=collection]').each(function(){

    var $collection = $(this),
        prototypeString = $collection.data('prototype'),
        prototypeName = $collection.data('prototypeName') || '__name__',
        prototypeNameRegx = new RegExp(prototypeName+"(label__)?","g"),
        count = $collection.children().length;

    console.log(this, count, prototypeString);

    var $deleteLink = $("<span />", {
        'class': 'js-delete',
        'text': 'Delete'
    });

    var $addLink = $("<span />", {
        'class': 'js-add',
        'text': 'Add'
    });

    if($collection.data('collectionAllowDelete')) {

        $collection
            .children()
            .append($deleteLink.clone(true));

        $collection.on('click', '.js-delete', function(){
            event.preventDefault();
            $(this).parent().remove();
        });
    }

    if($collection.data('collectionAllowAdd')) {

        $addLink.click(function(event){
            event.preventDefault();
            var newPrototypeString = prototypeString.replace(prototypeNameRegx, count),
                $newPrototype = $(newPrototypeString).append($deleteLink.clone(true));
            $newPrototype.insertBefore($addLink);
            count++;
        });

        $collection.append($addLink);
    }
});

Replaces #7713

@leevigraham
Copy link
Contributor Author

Regarding the docs updates. Currently there are two spots which describe the javascript functionality required to add and remove collection items:

Should both of thee pages be cleaned up moving the examples to the cookbook?

@webmozart webmozart changed the title Added additional data attributes for collection type output [Form] Added additional data attributes for collection type output Apr 8, 2014
@ignace
Copy link

ignace commented Oct 13, 2014

ping @leevigraham Are you still doing anything with this?

@webmozart
Copy link
Contributor

@leevigraham Sorry for the late feedback. What's the status of this PR?

@leevigraham
Copy link
Contributor Author

I'll have to check the PR again and rebase against master. I've been doing a lot of work with collections and forms so I might have to update the PR.

@ghost
Copy link

ghost commented Dec 29, 2014

@leevigraham : ping?

@wouterj
Copy link
Member

wouterj commented Jun 14, 2015

Now we are in the dev phase of 2.8 & 3.0, do you have some time to look at this PR again @leevigraham ?

@leevigraham
Copy link
Contributor Author

@wouterj I'm about to start on a big form based project in the next few weeks. I'm sure there will be some updates.

I'm thinking these will need to be 3.0 updates as they will probably break BC.

@webmozart
Copy link
Contributor

@leevigraham Ok, sounds good! I'm closing this PR for now. Please open a new one when you have your updates ready.

@webmozart webmozart closed this Jun 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants