get initial values on autocompletes to work as well as getting title on popup close

This commit is contained in:
Sanj 2012-07-19 03:36:31 +05:30
parent 3390f1e25c
commit 1bf13ef6ef
9 changed files with 29 additions and 10 deletions

View File

@ -33,7 +33,16 @@ class AutocompleteAddWidget(forms.Select):
super(AutocompleteAddWidget, self).__init__(*args, **kwargs) super(AutocompleteAddWidget, self).__init__(*args, **kwargs)
def get_context(self, name, value, attrs, *args, **kwargs): def get_context(self, name, value, attrs, *args, **kwargs):
def get_matched_choice_title(choices, id):
for choice in choices:
if choice[0] == id:
return choice[1]
return ''
ctx = super(AutocompleteAddWidget, self).get_context(name, value, attrs) ctx = super(AutocompleteAddWidget, self).get_context(name, value, attrs)
if value:
ctx['title'] = get_matched_choice_title(self.choices, value)
ctx['ctype'] = self.ctype ctx['ctype'] = self.ctype
return ctx return ctx

View File

@ -83,8 +83,10 @@ class ItfModel(models.Model):
#Returns the title for this object, used as title of page, title of object, and by default, title in left hand display. By default, is a field called 'title', but subclasses can over-ride either title field or the get_title method and return anything. #Returns the title for this object, used as title of page, title of object, and by default, title in left hand display. By default, is a field called 'title', but subclasses can over-ride either title field or the get_title method and return anything.
def get_title(self): def get_title(self):
return self.get(self.title_field) try:
return self.get(self.title_field)
except:
return self.__unicode__()
#Returns "text" for this object - like a "description" or "about" field, or nothing - ideally would be over-ridden by subclasses. #Returns "text" for this object - like a "description" or "about" field, or nothing - ideally would be over-ridden by subclasses.
def get_text(self): def get_text(self):

View File

@ -6,6 +6,7 @@ from ox.django.shortcuts import json_response, render_to_json_response, get_obje
#from api.views import html_snapshot #from api.views import html_snapshot
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
''' '''
def index(request): def index(request):
@ -44,7 +45,7 @@ def popup_form(request, ctype_id):
form = form_class(request.POST, request.FILES) form = form_class(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
instance = form.save() instance = form.save()
text = instance.autocomplete_dict()['text'] text = instance.get_title()
return HttpResponse("<script>opener.dismissAddAnotherPopup(window, '%s', '%s')</script>" % (instance.id, text,)) return HttpResponse("<script>opener.dismissAddAnotherPopup(window, '%s', '%s')</script>" % (instance.id, text,))
else: else:
form = form_class() form = form_class()

View File

@ -15,7 +15,7 @@ class PopupPersonForm(PopupForm):
class Meta: class Meta:
model = Person model = Person
fields = ('first_name', 'last_name', 'email', 'dob',)
class PopupGroupForm(PopupForm): class PopupGroupForm(PopupForm):

View File

@ -20,7 +20,7 @@ class Person(ItfModel):
first_name = models.CharField(max_length=255) first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255)
email = models.EmailField(blank=True, null=True, unique=True, db_index=True) email = models.EmailField(blank=True, null=True, unique=True, db_index=True)
# email_validated = models.BooleanField(default=False, editable=False) email_validated = models.BooleanField(default=False, editable=False)
tel_no = models.CharField(max_length=100, blank=True) tel_no = models.CharField(max_length=100, blank=True)
about = models.TextField(blank=True, null=True) about = models.TextField(blank=True, null=True)
dob = models.DateField(null=True, verbose_name="Date of Birth") dob = models.DateField(null=True, verbose_name="Date of Birth")

View File

@ -77,7 +77,7 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
//console.log(elem); //console.log(elem);
//alert(newRepr); //alert(newRepr);
//alert(newId); //alert(newId);
elem.select2("val", {'id': newId, 'text': newRepr}); elem.select2("val", {'id': newId, 'title': newRepr});
//var elem = document.getElementById(name); //var elem = document.getElementById(name);
//console.log(elem); //console.log(elem);

View File

@ -1,2 +1,2 @@
<input type="hidden" id="id_{{name}}" name="{{name}}" data-ctype="{{ ctype.id }}" class="select2class" value="{{ value }}" style="width:600px;" /> <a href="/popup_form/{{ ctype.id }}" id="add_{{name}}" onclick="return showAddAnotherPopup(this);">Add</a> <input type="hidden" id="id_{{name}}" name="{{name}}" data-ctype="{{ ctype.id }}" class="select2class" data-id="{{ value }}" data-title="{% if title %} {{ title }} {% endif %}" style="width:600px;" /> <a href="/popup_form/{{ ctype.id }}" id="add_{{name}}" onclick="return showAddAnotherPopup(this);">Add</a>

View File

@ -43,9 +43,10 @@ $(function(){
]; ];
*/ */
$('.select2class').each(function() { $('.select2class').each(function() {
var ctype_id = $(this).attr("data-ctype"); var $this = $(this);
var ctype_id = $this.attr("data-ctype");
var url = '/autocomplete/' + ctype_id; var url = '/autocomplete/' + ctype_id;
$(this).select2({ $this.select2({
ajax: { ajax: {
'url': url, 'url': url,
dataType: 'json', dataType: 'json',
@ -75,6 +76,12 @@ $(function(){
//return "<div data-id='" + item.id + "'>" + item.first_name + " " + item.last_name + "</div>"; //return "<div data-id='" + item.id + "'>" + item.first_name + " " + item.last_name + "</div>";
} }
}); });
var id = $this.attr("data-id");
if ($.trim(id) !== '') {
var title = $this.attr("data-title");
$this.select2("val", {'id': id, 'title': title});
}
}); });
/* /*

View File

@ -4,7 +4,7 @@
</head> </head>
<body> <body>
<form action="/popup/person" method="POST"> <form action="" method="POST">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<br /><br /> <br /><br />