1
我在Odoo網站上有一個表單,其中有國家和州選擇字段。這種形式是由一個控制器填充的,我將所有州和國家的記錄集作爲一個字典中的國家和國家作爲關鍵字來傳遞。現在對於選擇字段,它使用t-foreach通過狀態循環以提供選項和值。現在選擇國家後,我需要能夠過濾出該國的狀態。這是可能的,如果是的話,怎麼樣?下面是其中一些我已嘗試的代碼: 以下是其中最初加載的形式,並且所述第二方法,所述控制器被過濾掉的狀態將記錄集傳遞給控制器Odoo模板中的對象
@http.route('/admission_applications', type='http', auth="public", website=True)
def application_form(self):
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
classes=[('lkg',"LKG"),('ukg',"UKG"),('1', "1st"), ('2', "2nd"),('3', "3rd"),('4', "4th"),('5', "5th"),('6', "6th"),('7', "7th"),('8', "8th"),('9', "9th"),('10', "10th"),('11', "11th"),('12', "12th")]
gender=[('male', "Male"), ('female', "Female")]
orm_country = http.request.env['res.country']
state_orm = http.request.env['res.country.state']
countries = orm_country.search([])
#countries = orm_country.browse(cr, SUPERUSER_ID, country_ids, context)
states = state_orm.search([])
#states = state_orm.browse(cr, SUPERUSER_ID, states_ids, context)
return request.render("website_admission_application.admission_form", {'classes':classes, 'gender':gender, 'countries':countries, 'states':states})
@http.route('/action_get_states/<country_id>',type='http', auth="public", website=True)
def states_listing(self,country_id):
states_info=[]
logging.error(country_id)
states=http.request.env['res.country.state'].search([('country_id.id','=',country_id)])
for state in states:
states_info.append({'id':state.id, 'name':state.name})
logging.error(states_info)
return states_info
接下來就是模板相關的代碼:
<div t-attf-class="form-group form-field o_website_form_required_custom margin-0" t-if="countries">
<div class="col-sm-12 col-md-6">
<label class="control-label" for="country_id">Country</label>
<select name="country_id" id="country_id" class="form-control" onchange="countryselection()" required="">
<option value="">Country...</option>
<t t-foreach="countries" t-as="country">
<option t-att-value="country.id"><t t-esc="country.name"/></option>
</t>
</select>
</div>
<div class="col-sm-12 col-md-6">
<label class="control-label" for="state_id">State</label>
<select name="state_id" id="state_id" class="form-control" required="">
<option value="">State...</option>
<t t-foreach="states" t-as="state">
<option t-att-value="state.id"><t t-esc="state.name"/></option>
</t>
</select>
</div>
</div>
的JS調用控制器的方法來過濾狀態是這樣的:
function countryselection() {
alert("hai");
var country = document.getElementById('country_id').value;
alert(country);
$.ajax({
type: "GET",
url: "/action_get_states/"+country,
success: function(data) {
$("states").html(data);
}
});
}
我知道這是不正確的做法,但我ñ確信我可以如何將記錄集返回到已經使用表單初始加載的狀態。
還沒有嘗試過上述方法,我在我的控制器中使用ajax調用一個新方法來處理它,並將狀態作爲帶有id和名稱的json數據返回。 state_id的選項被調整,以便它們具有從控制器方法傳遞的數據。 說了這麼一句話,你的方法似乎更清潔我的感覺。我會稍後再試一試並回復你。感謝你的回答! –