2017-07-28 11 views
1

私は、国と州の選択フィールドを持つOdooのWebサイトにフォームを持っています。このフォームは、すべての州と国のレコードセットを、州と国のキーとしてのdictの値として渡すコントローラから生成されます。選択フィールドの場合は、t-foreachを使用して状態をループして、オプションと値を提供します。今すぐ国を選ぶと、その国の州を除外することができなくてはなりません。これは可能ですか?ここで私が試みたコードの一部されている:テンプレート内のオブジェクトにレコードセットを渡すコントローラOdoo

: 以下は、最初にフォームをロードし、第2の方法は、次のテンプレートに関連するコードである状態を

@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); 
     } 
    });  
} 

私は、これは正しい方法ではないですけど、私はn個です私はすでにフォームを最初にロードした状態にレコードセットをどのように戻すことができるのか分かりません。

答えて

1
//This is just an example which I used on Odoo website. 
// Just make your proper changes by changing field names.  
$(document).ready(function() { 

     $('select[name=country_id]').change(function() { 
     $('#state_id option').remove(); 
     var states = new Model('res.country.state').call('search_read',[[['country_id', '=', parseInt($('select[name=country_id]').val())]]]).then(function(result){ 
     for (var i=0; i<result.length; i++){ 
      $('#state_id').append($('<option>', 
       { 
        value: result[i].id, 
        text : result[i].name 
       })); 
     } 
     }) 

     }); 
+0

私はコントローラで新しいメソッドへのajax呼び出しを使って処理し、IDと名前のjsonデータとして状態を返しました。 state_idのオプションは、コントローラーメソッドから渡されたデータを持つように調整されました。 あなたの方法はすべてき​​れいだと感じています。私はそれを後で試してみるとあなたに戻ってきます。答えをありがとう! –

関連する問題