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
- Buchhaltung
- Lager
- PoS
- Project
- MRP
Diese Frage wurde gekennzeichnet
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.
Diskutieren Sie gerne? Treten Sie bei, statt nur zu lesen!
Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!
RegistrierenVerknüpfte Beiträge | Antworten | Ansichten | Aktivität | |
---|---|---|---|---|
|
2
Mai 24
|
7921 | ||
|
2
Juni 22
|
3025 | ||
|
0
Mai 22
|
2238 | ||
|
1
Mai 21
|
2770 | ||
|
4
Apr. 18
|
17842 |