django crispy forms accordion issue

2019-09-02 18:38发布

I have simple Accordion below:

I want to setup active=True or active=False conditionally basing on value which is on reason_yes and reason_no fileds:

def __init__(self, *args, **kwargs):
    super(MyUpdateForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper(self)
    self.helper.layout = Layout(

        TabHolder(
            Tab(
                'Tab1',
                Accordion(
                    AccordionGroup(
                                   Field('reason_yes'),
                                   active=False
                    ),
                    AccordionGroup(
                                   Field('reason_no'),
                                   active=True
                    )
                )
            ),
            Tab(
                'Tab2',
                Field('other fileds')
            )
        ),
        FormActions(
            Submit('save_changes', 'Save changes', ),
            Button('cancel', 'Cancel', onclick="window.history.back()"),
        )
    )

1条回答
趁早两清
2楼-- · 2019-09-02 18:52

Finally I ended up with jQuery like below

var reasonYesValue = $('#id_reason_yes option:selected').val();
var reasonNoValue = $('#id_reason_no option:selected').val();
if (reasonYesValue == 'N/A' & reasonNoValue != 'N/A') {
    $('#yes-id').removeClass('in');
    $('#no-id').addClass('in').prev('.panel-heading').addClass('activeTab');
}
else {
    $('#yes-id').prev('.panel-heading').addClass('activeTab');
}
$('.panel-group')
    .on('show.bs.collapse', function (e) {
        $(e.target).prev('.panel-heading').addClass('activeTab');
    })
    .on('hide.bs.collapse', function (e) {
        $(e.target).prev('.panel-heading').removeClass('activeTab');
    });
查看更多
登录 后发表回答