elo80ka / django-dynamic-formset

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

Input field id show NaN instead of increased number even with prefix set #195

Open ccunhafinance opened 3 years ago

ccunhafinance commented 3 years ago

my View

`def offer(request):

if request.method == "GET":
    main_form = OfferRfForm()
    form_factory = inlineformset_factory(OfferRf, SerieRf, form=SeriesRfForm, extra=3)
    form_series = form_factory(prefix='serierf_set')

    context = {
        'main_form': main_form,
        'form_series': form_series,
        # Crumbs First Page Config
        'first_page_name': 'Ofertas',
        'first_page_link': '',
        # Crumbs Second Page Config
        'second_page_name': 'Renda Fixa',
        'second_page_link': '',
        # Crumbs Third Page Config
        'third_page_name': 'Add',
        'third_page_link': '',
        # Current Page
        'icon': main_icon,
        'page_name': 'Ofertas (RF)',
        'subtitle': 'Add',
        'sticker': 'Novo',
        'page_description': 'Formulário de cadastro de ofertas de Renda Fixa'
    }

    return render(request, 'offers/rf/create_view.html', context)

elif request.method == 'POST':
    print(request.POST)
    main_form = OfferRfForm(request.POST)
    form_factory = inlineformset_factory(OfferRf, SerieRf, form=SeriesRfForm, can_delete=True)
    form_series = form_factory(request.POST, prefix='serierf_set')
    if main_form.is_valid() and form_series.is_valid():

        oferta = main_form.save()
        form_series.instance = oferta
        form_series.save()

        return redirect(reverse('offers:listar-rf'))

    else:
        context = {
            'main_form': main_form,
            'form_series': form_series,
            # Crumbs First Page Config
            'first_page_name': 'Ofertas',
            'first_page_link': '',
            # Crumbs Second Page Config
            'second_page_name': 'Renda Fixa',
            'second_page_link': '',
            # Crumbs Third Page Config
            'third_page_name': 'Add',
            'third_page_link': '',
            # Current Page
            'icon': main_icon,
            'page_name': 'Ofertas (RF)',
            'subtitle': 'Add',
            'sticker': 'Novo',
            'page_description': 'Formulário de cadastro de ofertas de Renda Fixa'
        }

        return render(request, 'offers/rf/create_view.html', context)`

my template

`

{% csrf_token %}

Ofertas


{{ main_form|crispy }}

Séries


{% for form in form_series.forms %}
{{ form|crispy }}
{% endfor %}
                    </form>`

my js

<script type="text/javascript"> $(function() { $('.inlineform').formset({ prefix: '{{ form_series.prefix }}', addText: '<div style="margin-top: 30px">Adicionar nova Série</div>', deleteText: 'Remover Série' }); }) </script>

Please, how can i fix

prantoamt commented 2 years ago

Have you found any solution yet?

andreccorrea commented 2 years ago

Same problem here, any solutions?

prantoamt commented 2 years ago

@andreccorrea, Not yet. However, I have found an alternative solution. Check if that helps you- https://stackoverflow.com/a/69287239/6092533

andreccorrea commented 2 years ago

Ok, I'll take a look. Thank you!

andreccorrea commented 2 years ago

Does anybody know what happened to Stanislaus Madueke? All his activities ceased by the end of 2020. All his 20 repos are frozen in time with tons of PRs ready to be merged.

andreccorrea commented 2 years ago

@andreccorrea, Not yet. However, I have found an alternative solution. Check if that helps you- https://stackoverflow.com/a/69287239/6092533

I have found a solution. Had to change totalForms on line 95 if hideLastAddForm is true:

line 95: totalForms.val(forms.length);

if (options.hideLastAddForm) { totalForms.val(forms.length + 1); } else { totalForms.val(forms.length); }