Giter VIP home page Giter VIP logo

Comments (5)

elo80ka avatar elo80ka commented on July 21, 2024

From stan.madueke on June 29, 2010 13:17:34

You're quite right -- this should be handled by the plugin internally. I'd appreciate a patch, if you've got the time to code one. Otherwise, I'll get to it sometime this weekend.

Status: Accepted
Owner: stan.madueke

from django-dynamic-formset.

elo80ka avatar elo80ka commented on July 21, 2024

From [email protected] on June 29, 2010 13:52:00

The cheesy fix would look something like this:

@@ -145,6 +146,9 @@
updateElementIndex($(this), options.prefix, formCount);
});
$('#id_' + options.prefix + '-TOTAL_FORMS').val(formCount + 1);

  •            if (formCount+1  >= $('#id_' + options.prefix + '-MAX_NUM_FORMS').val()) {
    
  •                $(buttonRow).hide();
    
  •            }
             // If a post-add callback was supplied, call it with the added form:
             if (options.added) options.added(row);
             return false;
    

Basically just hide the add another link if the form count is bigger than the max.

Making the link re-appear probably wouldn't be that hard. Just add the right code around line 64. The problem is knowing what the "bottomRow" is called since it seems to be either just an 'a' element or sometime inside of a tr. I wasn't confident enough to create a selector which would always work.

For my use-case I won't be letting user remove forms so this cheesy fix should work for me for now.

Let me know if there is anything wrong with my patch, but practically it seems to work.

Thanks for taking this key functionality and creating a jQuery plug in for it. It is really helpful. Hopefully it can continue to mature and become standard and robust.

from django-dynamic-formset.

elo80ka avatar elo80ka commented on July 21, 2024

From [email protected] on June 29, 2010 14:13:56

I posted a bit pre-emptively.

I have an expanded patch with addresses when the number of forms on the page is already the MAX_NUM_FORMS. It also handles MAX_NUM_FORMS not having a value (which is pretty common).

--- jquery.formset.js.orig.js 2010-04-13 02:40:18.000000000 -0400
+++ jquery.formset.js 2010-06-29 17:11:17.000000000 -0400
@@ -64,6 +64,7 @@
// Also update names and IDs for all remaining form controls so they remain in sequence:
var forms = $('.' + options.formCssClass).not('.formset-custom-template');
$('#id_' + options.prefix + '-TOTAL_FORMS').val(forms.length);

                   for (var i=0, formCount=forms.length; i<formCount; i++) {
                       applyExtraClasses(forms.eq(i), i);
                       forms.eq(i).find('input,select,textarea,label').each(function() {

@@ -135,6 +136,11 @@
$$.filter(':last').after('' + options.addText + '');
addButton = $$.filter(':last').next();
}

  •        // Hide the addButton if we already have the max num of forms.
    
  •        if ($('#id_' + options.prefix + '-MAX_NUM_FORMS').val() &&
    
  •            $('#id_' + options.prefix + '-TOTAL_FORMS').val() >= $('#id_' + options.prefix + '-MAX_NUM_FORMS').val()) {
    
  •                $(addButton).hide();
    
  •        }
         addButton.click(function() {
             var formCount = parseInt($('#id_' + options.prefix + '-TOTAL_FORMS').val()),
                 row = options.formTemplate.clone(true).removeClass('formset-custom-template'),
    
    @@ -145,6 +151,10 @@
    updateElementIndex($(this), options.prefix, formCount);
    });
    $('#id_' + options.prefix + '-TOTAL_FORMS').val(formCount + 1);
  •            if ($('#id_' + options.prefix + '-MAX_NUM_FORMS').val() &&
    
  •                formCount+1  >= $('#id_' + options.prefix + '-MAX_NUM_FORMS').val()) {
    
  •                $(buttonRow).hide();
    
  •            }
             // If a post-add callback was supplied, call it with the added form:
             if (options.added) options.added(row);
             return false;
    

from django-dynamic-formset.

elo80ka avatar elo80ka commented on July 21, 2024

From stan.madueke on June 29, 2010 21:34:27

Thanks! I'll merge this in later today.

from django-dynamic-formset.

elo80ka avatar elo80ka commented on July 21, 2024

From stan.madueke on July 03, 2010 18:16:23

This has been fixed in trunk.

Status: Fixed

from django-dynamic-formset.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.