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:
- Müşteri İlişkileri Yönetimi
- e-Commerce
- Muhasebe
- Envanter
- PoS
- Project
- MRP
Bu soru işaretlendi
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.
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Üye Olİlgili Gönderiler | Cevaplar | Görünümler | Aktivite | |
---|---|---|---|---|
|
2
May 24
|
7923 | ||
|
2
Haz 22
|
3027 | ||
|
0
May 22
|
2241 | ||
|
1
May 21
|
2770 | ||
|
4
Nis 18
|
17852 |