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
- Contabilità
- Magazzino
- PoS
- Project
- MRP
La domanda è stata contrassegnata
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.
Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!
Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!
RegistratiPost correlati | Risposte | Visualizzazioni | Attività | |
---|---|---|---|---|
|
2
mag 24
|
7920 | ||
|
2
giu 22
|
3025 | ||
|
0
mag 22
|
2237 | ||
|
1
mag 21
|
2769 | ||
|
4
apr 18
|
17841 |