Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to filter second selector depending on the answer of the first selector?

By
Jesús Alan Ramos Rodríguez
on 12/7/15, 6:19 PM 439 views

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>

You need JS here.

Vauxoo S.A. de C.V., Oscar Alcala
on 12/7/15, 11:42 PM
0
Oscar Alcala
On 12/7/15, 11:48 PM

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.

1
Shivam Mahajan
On 12/8/15, 1:36 AM

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>

 



0

Ray Carnes

--Ray Carnes--
15717
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

Ray Carnes
On 12/7/15, 8:26 PM

Checkout crm_claim - the module that does this:


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.

JARSA Sistemas, S.A. de C.V., Jesús Alan Ramos Rodríguez
on 12/7/15, 9:02 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/7/15, 6:19 PM
Seen: 439 times
Last updated: 12/8/15, 3:36 PM