Уведомления

Группа в Telegram: @pythonsu

#1 Окт. 30, 2013 13:18:19

StimuL
Зарегистрирован: 2013-10-30
Сообщения: 4
Репутация: +  0  -
Профиль   Отправить e-mail  

Autocomletefield для formset'ов.

Здравствуйте!

Автокомплит работает только для тех форм которые я проинитил в представлении. Когда я с помощью jquery добваляю новую форму получаю по функционалу обычный charfield (все классы необходимые для автокомплита ему присваиваются).

Код:

#Form

class RegionLookup(ModelLookup):
model = Region
search_fields = ('name_ru__icontains', 'name_en__icontains')
registry.register(RegionLookup)
class ProfileSettingsIncRegionForm(forms.Form):
include = AutoCompleteSelectField(required=False, label='', lookup_class=RegionLookup, widget=AutoCompleteSelectWidget(lookup_class=RegionLookup, attrs={'class': 'text small long include_region', 'placeholder': _('Choose region')}))
# include = forms.IntegerField(required=False, widget=forms.HiddenInput(attrs={'class': 'chosen_region'}), label='')
class Meta:
model = ProfileSettingsRegion
exclude = ('profile', 'name', 'exclude')

#template
{% load selectable_tags %}
{{ region_include_form_set.media }}
{{ region_exclude_form_set.media }}
{% block extrahead %}

<script type="text/javascript">
$(document).ready(function() {
$('.include_row').formset({
prefix: '{{ region_include_form_set.prefix }}',
added: function(row){
bindSelectables($(row));
$('.delete-row').text('');
},
removed: function(row){
if (!$('.include_row').is(':visible')) {
$('.include_row').next('.add-row').trigger('click');
}}
});
$('.include input[type=text]').djselectable();
$('.add-row').text('');
$('.delete-row').text('');
});
</script>
{% endblock %}
            
<table style="width: 100%;">
                <tr>
                    <td style="vertical-align: top;width: 275px">
                        <div class="title include">{% trans "Please select the region for work" %}:</div>
                    </td>
                    <td style="vertical-align: top">
                        {% for form in region_include_form_set.forms %}
                            <div class="include_row">
                                <div class="col" style="width: 55%">
                                    {{ form.include }}
                                    {% if form.include.errors %} <div class="error">{{ form.include.errors }}</div>{% endif %}
                                    <div style="display: none;">{{ form.id }} {% if region_include_form_set.can_delete %}{{ form.DELETE }}{% endif %}</div>
                                </div>
                            </div>
                        {% endfor %}
                    </td>
                </tr>
            </table>
            {{ region_include_form_set.management_form }}

В библиотеке jquery-ui-v1.10.2.js

        this.each(function() {
            var instance = $.data( this, fullName );
            if ( instance ) {
                instance.option( options || {} )._init();
            } else {
                $.data( this, fullName, new object( options, this ) );
            }
        });
в строке var instance = $.data( this, fullName ); в this хранится id свежесозданного элемента,

а в if ( instance ) {

в instance уже id предыдущего (проинициализированного в представлении).


З.Ы. Извиняюсь за форматирование кода. первый пост.

Отредактировано StimuL (Окт. 31, 2013 11:55:56)

Офлайн

#2 Окт. 31, 2013 12:29:02

StimuL
Зарегистрирован: 2013-10-30
Сообщения: 4
Репутация: +  0  -
Профиль   Отправить e-mail  

Autocomletefield для formset'ов.

Решил проблему так. Не красиво и не хорошо, но работает.

				if ( instance ) {
                    $.data( this, fullName, new object( options, this ) );
//					instance.option( options || {} )._init();
				} else {
					$.data( this, fullName, new object( options, this ) );
				}

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version