Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize select2 widget #31

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions advanced_filters/static/advanced-filters/advanced-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ var OperatorHandlers = function($) {
MODEL_LABEL) + '/' + field;
var input = $(elm).parents('tr').find('input.query-value');
input.select2("destroy");
var value = input.val();
$.get(choices_url, function(data) {
if (value) {
data.results.push({'id': value, 'text': value})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the currently selected value already exist in the results? This does feel redundant, as you mentioned.

Copy link
Contributor Author

@pjpassa pjpassa Aug 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value actually might not exist in the results. One example is choices being disabled for a field. To avoid duplication though, there should be a check to see if the value is currently in data. No reason to add it if it is already there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In that case, how about we add the value into results only if it does not exist already ?

}
input.select2({'data': data, 'createSearchChoice': function(term) {
return { 'id': term, 'text': term };
Copy link
Member

@asfaltboy asfaltboy Aug 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could simply call input.val(value)" before the return call to select the pre-initialized value as current.

}});
Expand Down Expand Up @@ -126,6 +130,7 @@ var OperatorHandlers = function($) {
if ($(this).val() != before_change) self.field_selected(this);
$(this).data('pre_change', $(this).val());
}).change();
self.initialize_select2(this)
});
};

Expand Down
8 changes: 3 additions & 5 deletions advanced_filters/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.conf.urls import patterns, url
from django.conf.urls import url

from advanced_filters.views import GetFieldChoices

urlpatterns = patterns(
# API
'',
urlpatterns = [
url(r'^field_choices/(?P<model>.+)/(?P<field_name>.+)/?',
GetFieldChoices.as_view(),
name='afilters_get_field_choices'),
Expand All @@ -13,4 +11,4 @@
url(r'^field_choices/$',
GetFieldChoices.as_view(),
name='afilters_get_field_choices'),
)
]