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)
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)
if value:
ctx['title'] = get_matched_choice_title(self.choices, value)
ctx['ctype'] = self.ctype
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.
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.
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 django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
'''
def index(request):
@ -44,7 +45,7 @@ def popup_form(request, ctype_id):
form = form_class(request.POST, request.FILES)
if form.is_valid():
instance = form.save()
text = instance.autocomplete_dict()['text']
text = instance.get_title()
return HttpResponse("<script>opener.dismissAddAnotherPopup(window, '%s', '%s')</script>" % (instance.id, text,))
else:
form = form_class()

View File

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

View File

@ -20,7 +20,7 @@ class Person(ItfModel):
first_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_validated = models.BooleanField(default=False, editable=False)
email_validated = models.BooleanField(default=False, editable=False)
tel_no = models.CharField(max_length=100, blank=True)
about = models.TextField(blank=True, null=True)
dob = models.DateField(null=True, verbose_name="Date of Birth")

View File

@ -77,7 +77,7 @@ function dismissAddAnotherPopup(win, newId, newRepr) {
//console.log(elem);
//alert(newRepr);
//alert(newId);
elem.select2("val", {'id': newId, 'text': newRepr});
elem.select2("val", {'id': newId, 'title': newRepr});
//var elem = document.getElementById(name);
//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() {
var ctype_id = $(this).attr("data-ctype");
var $this = $(this);
var ctype_id = $this.attr("data-ctype");
var url = '/autocomplete/' + ctype_id;
$(this).select2({
$this.select2({
ajax: {
'url': url,
dataType: 'json',
@ -75,6 +76,12 @@ $(function(){
//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>
<body>
<form action="/popup/person" method="POST">
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<br /><br />