when adding a new inline to my formset, my datepickers stop working

落爺英雄遲暮 提交于 2020-04-29 10:08:52

问题


Bit of a strange one today.

I've a very basic project. A form and added to that I have an inline form. It's books to authors.

When the form loads(/authors/create/) I have a author name and underneath I have a form where I can add books to that author.

To be able to have dynamic inlines(able to add or remove on the form) I'm using http://code.google.com/p/django-dynamic-formset/.

My problem now is that when I add a new inline, all my datepickers(for the date published fields) for the inline form stops working.

I have attached my sample project showing exactly what is wrong. You can download and run it here.

Why and how can I fix this?


回答1:


I am sure there is cleaner and more clever way to do it. But working proof of concept:

function renewDatePickers() {
    $('.datepicker').datepicker('destroy');
    $(".datepicker").datepicker({dateFormat: 'yy-mm-dd'});
}

$(renewDatePickers);

$(function() {
    $('.form-row.inline.{{ book_form.prefix }}').formset({
        prefix: '{{ book_form.prefix }}',
        added: renewDatePickers // Event -- read inline docs
    });
})

And take Your JS code from base.html to author_form.html



来源:https://stackoverflow.com/questions/6719063/when-adding-a-new-inline-to-my-formset-my-datepickers-stop-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!