Skip to content
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

Limit add link #69

Open
Awatatah opened this issue Jul 5, 2016 · 4 comments
Open

Limit add link #69

Awatatah opened this issue Jul 5, 2016 · 4 comments

Comments

@Awatatah
Copy link

Awatatah commented Jul 5, 2016

is there a way to limit f.add_nested_fields_link to a number? Meaning, let's say someone clicks the f.add_nested_fields_link link, I only want the person to add 1 nested object. How can I make the link not be shown after clicked x amount of times?

@ncri
Copy link
Owner

ncri commented Jul 6, 2016

You can use the fields_added and fields_removed events for this. Just hide the link when there are x nested forms on the page and show when there are x-1 nested forms. Alternatively, you can also use the click event of the add_nested_fields_link. Just make sure when you count the nested forms you only count the visible forms because when you remove a nested form, it is only hidden, not removed, until the form is submitted.

@jeremysenn
Copy link

Great gem!

Can you elaborate on how either of those solutions could be performed? I've got multiple nested forms on one page, and I'd like to limit the number of nested_fields that can be created for each of them.

I see the CoffeeScript samples in the Readme, but I'm having a hard time connecting those with what would need to happen in this situation.

Thanks!

@ncri
Copy link
Owner

ncri commented Dec 8, 2016

You can roughly do something like this for example:

$your_container.on 'click', '.add_nested_fields_link', ->
  if $your_container.find('.some_class_in_your_nested_fields:visible').length == max_items - 1
    $(this).hide()
  true

$your_container.on 'click', '.remove_nested_fields_link', ->
  if $your_container.find('.some_class_in_your_nested_fields:visible').length == max_items
      $your_container.find('.add_nested_fields_link').show()
  true

@jeremysenn
Copy link

Great! Thank you very much for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants