I have a value stored in Google App Engine datastore, selected from a dropdown select control.
But every time I load the html page, it does not show me the stored datastore value. Instead it shows the original dropdown select with no option selected.
HTML:
<select name="items">
<option>Select</option>
</select>
JavaScript:
(function($) {
$.fn.changeType = function() {
var data = [{"user":"a","name":"Mr A"},
{"user":"b","name":"Miss B"},
{"user":"c","name":"Mrs C"}];
var tlist = '<option>Select<\/option>';
$.each(data, function(i,d){
tlist += '<option value="' + d.user + '">' + d.name + '<\/option>';
});
$("select", this).html(tlist);
};
})(jQuery);
main.py:
# assume model s
user = db.StringProperty()
...
updated_user = self.request.get('items')
s.user = updated_user
s.put()
Note that I only store the user (value) part (eg a) in the datastore, but I would like to have the name part (eg Mr A). Is this possible?
Many thanks!
Update:
Sorry for not being clear. I was using jinja2 with a template value p which draws from the model s
<select name="items">
<option>{{ p.user }}</option>
</select>
and also tried putting this in the jQuery:
var tlist = '<option>{{ p.user }}<\/option>';
but there was no effect (well it sort of shows the user (value) part for a split second). Essentially I would like to ask what and where do I put the Google App Engine datastore value in my HTML and jQuery. Thanks.
Update 2:
I changed the JavaScript to
(function($) {
$.fn.changeType = function() {
var data = [{"user":"a","name":"Mr A"},
{"user":"b","name":"Miss B"},
{"user":"c","name":"Mrs C"}];
var tselected = $("select option:selected").val();
var tlist = '<option>Select<\/option>';
$.each(data, function(i,d){
if (d.user === tselected)
tlist += '<option value="' + d.user + 'selected="selected">' + d.name + '<\/option>';
else
tlist += '<option value="' + d.user + '">' + d.name + '<\/option>';
});
$("select", this).html(tlist);
};
})(jQuery);
but tselected does not appear to return the current selected value. Any ideas? Thanks.
Solved using input hidden value.