elo80ka / django-dynamic-formset

A jQuery plugin that allows you dynamically add new forms to a rendered django formset.
677 stars 311 forks source link

TypeError: options.removed is not a function #138

Open Pulkit-Sharma opened 7 years ago

Pulkit-Sharma commented 7 years ago

Thanks for this wonderful plugin. Everything works fine except when I try to pass a function for removed action, it gives below error:

"TypeError: options.removed is not a function"

<script type="text/javascript">
    $(function() {
        $('#formset tbody tr').formset({
            prefix: '{{ new_formset.prefix }}',
            deleteText: '<button type="button" class="btn btn-default btn-circle btn-icon" id="formsetbutton">-</i></button>',
            addText: '<button type="submit" class="btn btn-default btn-circle btn-icon">+</button>',
            removed: 'recalculate()'
        });
    })
</script>

My function:

<script>
function recalculate(){
            var gross_sale = 0;
            $("input[id*='taxable']:visible").each(function(){
            gross_sale += +$(this).val();
            });
            var gstaxes = 0;
            $("input[id*='gst']:visible").each(function(){
            gstaxes += +$(this).val();
            });
            var totalinvalue = 0;
            $("input[id*='total_amount']:visible").each(function(){
            totalinvalue += +$(this).val();
            });

            $('#id_sale').val(gross_sale);
            $('#id_tax').val(gstaxes);
            $('#id_due').val(totalinvalue);
};
</script>
elo80ka commented 7 years ago

Hi @Pulkit-Sharma

You should pass the function object, not a string (as you've done). So, for example, your code should read:

<script type="text/javascript">
    $(function() {
        $('#formset tbody tr').formset({
            prefix: '{{ new_formset.prefix }}',
            deleteText: '<button type="button" class="btn btn-default btn-circle btn-icon" id="formsetbutton">-</i></button>',
            addText: '<button type="submit" class="btn btn-default btn-circle btn-icon">+</button>',
            removed: recalculate   // not 'recalculate()'
        });
    })
</script>

Sorry for the late response 😄