This question has been flagged

I have been working in the singup form view where I define new fields that are selectors, I need that when you select the first field (Brand), the second shows data depending on the first selector (Brand/Model). To get the data from the database to the selectors I use.

<t t-foreach="request.env['fleet.vehicle.model.brand'].search([])" t-as="brand">

It works perfectly, but the problem is that I need the second selector only shows Models from the brand I selected in  the first selector.

How can I get the data from the first selector to use in a search([('brand_id', '=', brand_id)])?

There is on_change in website views?

I paste the code below:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="auth_signup_motomanic.fields"
inherit_id="auth_signup.fields"
name="Auth Signup/ResetPassword form fields">
<xpath expr="//div[@class='form-group field-confirm_password']" position="after">
<div t-attf-class="form-group #{error and 'brand_id' in error and 'has-error' or ''}">
<label class="control-label" for="brand_id">Brand</label>
<select name="brand_id" class="form-control">
<option value="">Brand...</option>
<t t-foreach="request.env['fleet.vehicle.model.brand'].search([])" t-as="brand">
<option t-att-value="brand.id" t-att-selected="brand.id == (int(brand_id) if brand_id else website.user_id.partner_id.vehicle_id.model_id.brand_id.id)">
<t t-esc="brand.name"/>
</option>
</t>
</select>
</div>
<div t-attf-class="form-group #{error and 'model_id' in error and 'has-error' or ''}">
<label class="control-label" for="model_id">Model</label>
<label class="control-label" for="model_id"><t t-esc="brand_id"/></label>
<select name="model_id" class="form-control">
<option value="">Model...</option>
<option value=""><t t-esc="request.params.copy()"/></option>
<t t-foreach="request.env['fleet.vehicle.model'].search([])" t-as="model">
<option t-att-value="model.id" t-att-selected="model.id == (int(model_id) if model_id else website.user_id.partner_id.vehicle_id.model_id.id)">
<t t-esc="model.name"/>
</option>
</t>
</select>
</div>
</data>
</openerp>

Avatar
Discard

You need JS here.

Best Answer

Dude there is no on_change for the website templates, but here is a workaround that will work for you in JS:

     $("select[name='brand_id']").change(function(){

     var $select = $("select[name='model_id']");

     $select.find("option:not(:first)").hide();

     var nb = $select.find("option[data-brand_id="+($(this).val() || 0)+"]").show().size();

     $select.val(0);

     });

you also need to add this two atttributes to your selection field on his <option> tag: `style="display:none;" t-att-data-brand_id="model.brand_id.id"`


Hope it works, haven't tested on a real project.

Avatar
Discard
Best Answer

Well you can use the attrs attribute of the field .Depending upon the value in the first selection box the value in the second box changes automatically . 

The demo code for simplifying your need is :

class selector(models.Model):

_name='select.selector'

selector1=fields.Selection([('BLY','Bareilly'),('NDA','Noida'),('DL','Delhi')],"Brands")

selector21=fields.Selection([('IU','Invertis University'),('SRMS','SRMS')],"Models")

selector22=fields.Selection([('GU','Galgotia University'),('AU','Amity')],"Models")

selector23=fields.Selection([('DCE','DU'),('IIT','IIT DELHI')],"Models")



View.xml

<group><field name="selector1"/></group>

<group><field name="selector21" attrs="{'invisible': [('selector1', '!=', 'BLY')]}"/>

<field name="selector22" attrs="{'invisible': [('selector1', '!=', 'NDA')]}"/>

<field name="selector23" attrs="{'invisible': [('selector1', '!=', 'DL')]}"/></group>

 



Avatar
Discard
Best Answer

Checkout crm_claim - the module that does this:


Avatar
Discard
Author

thanks for your response. The main problem is that Sign Up is a front end (Qweb) view. It's created with auth_signup module and Allow external users to sign up = True, I cannot upload a picture of the view but when you register name, email and password I added two new fields "Brand" and "Model" that are after "Confirm Password", when you select the brand the models related to that brand need to be shown in the second selector.