I need to create dropdown boxes for country, state, and city in a web
form, with the state dropdown depending on the selected country, and the
city dropdown depending on the selected state. How can I achieve this?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Boekhouding
- Voorraad
- PoS
- Project
- MRP
Deze vraag is gerapporteerd
Hi,
The following code retrieves the state based on the country_id. Similarly, you can obtain the city based on the state.
XML:
<template id="country_template" name="Country">
<t t-call="website.layout">
<div class="container">
<form class="form" id="create_form" method="post" action="/create/form">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_
<div class="row mb-3">
<label for="country_id" class="col-sm-2 col-form-label">Country</
<div class="col-sm-10">
<select id="country_id" name="country" class="form-control">
</select>
</div>
</div>
<div class="row mb-3">
<label for="state_id" class="col-sm-2 col-form-label">State</label>
<div class="col-sm-10">
<select id="state_id" name="state" class="form-control">
</select>
</div>
</div>
</form>
</div>
</t>
</template>
.py:
class CountryData(Controller):
@route('/data', auth='public', website=True)
def form_data(self):
country_id = request.env['res.country'].
return request.render('module_name.
@route('/create/form', auth='public', website=True)
def create_form(self, **kw):
form = request.env['model.name'].
'country_id': kw.get('country'),
'state_id': kw.get('state'),
'city_id': kw.get('city'),
})
return request.render('module_name.
@route('/get_states', type="json", auth='public', website=True)
def get_states(self, country_id):
states = request.env['res.country.
return states
JS:
publicWidget.registry.
selector: '#create_form',
events: {
'change #country_id': '_onCountryChange',
},
init: function (parent, options) {
this._super.apply(this, arguments);
this.rpc = this.bindService("rpc");
},
_onCountryChange: async function () {
var self = this
var country_id = this.$el.find('#country_id').
await jsonrpc("/get_states", {
country_id: country_id
}).then(function (records) {
self.$el.find('#state_id').
self.$el.find('#state_id').
records.forEach(function (record) {
self.$('#state_id').append(
`<option value="${record.id}">${record.
);
});
});
},
})
Hope it helps.
Thanks. it is working for me.
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
AanmeldenGerelateerde posts | Antwoorden | Weergaven | Activiteit | |
---|---|---|---|---|
|
2
mei 24
|
7923 | ||
|
2
jun. 22
|
3026 | ||
|
0
mei 22
|
2241 | ||
|
1
mei 21
|
2770 | ||
|
4
apr. 18
|
17852 |