This is almost entirely a javascript thing, using ajax to populate the location dropdown. On the server side, you'll need to write a small function that takes a customer id, and returns locations for them. I don't know what database wrapper you're using, so you'll have to obviously change a few things to make it work, but it'll end up looking roughly like: (I'm freehanding this code without trying any of it, so there might be obvious syntax errors fyi)
1 2 3 4 5 6 7 8 |
from flask import jsonify
@route ( "/location/:customer_id" )
def lookup_location( self , customer_id):
locations = self .db.Locations.find_by_customer(customer_id)
return jsonify(locations)
|
Then, in js, create an event handler to watch for change events, and update the locations accordingly. With jquery, it'd look a little like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<script type = "text/javascript" >
$(function() {
$( "#customer_dropdown" ).change(function() {
var customer_id = $(this).val();
$.ajax(
{
url: "/location/" ,
type : "post" ,
dataType: "json" ,
data: {
customer_id: customer_id
}
})
.done(function(data) {
/ / clear any locations currently there
$( "#location_dropdown" ).html("");
for (var ndx = 0 ; ndx < data.length; ndx + + ) {
var item = data[ndx];
var option = document.createElement( "option" );
option.value = item[ "value" ];
option.text = item[ "description" ];
$( "#location_dropdown" ).append(option);
}
});
});
});
< / script>
|