Filter python list based on JavaScript variable in views

30 views
Skip to first unread message

Mujeeb Farees

unread,
Oct 4, 2017, 4:20:22 AM10/4/17
to web2py-users
Hi,
The controller returns rows from building table via which the dropdown is populated. When a user selects a building from the dropdown, the address of the building should be displayed in the paragraph tag. Here is what I have done so far,
  
Controller
def index():
   
return db(db.building).select()

View
<select id="buildings" onchange="displayBuidlingDetails()">
{{for building in buildings:}}
<option value="{{=building.id}}">{{=building.address}}</option>
{{pass}}
</select>
<p id="buildingAddress"></p>

<script>
function displayHomeDetails() {
       
var selected_building = $('#buildings').val();
        $
('#buildingAddress').text({{=(next(building.address for building in buildings if building.id == selected_building)}});
</script>

Is there a way to select an item from the buildings list based on dropdown value (something related to what's highlighted in yellow above)? I am already familiar with how to use an ajax request for this, but I would like to do this without an additional request (if possible).
Thanks
Reply all
Reply to author
Forward
0 new messages