Filter on view field

Hi everyone,

I’m trying to developp a small module, and the issue i’m facing is filtering on a field based on another field’s value.

<entity name="Employee">
    <string name="firstName" title="First Name" required="true" unique="true" min="2"/>
    <string name="lastName" title="Last Name" required="true" unique="true" min="2"/>
    <string name="fullName" namecolumn="true" search="firstName,lastName">
        <![CDATA[
        if (firstName == null && lastName == null)
            return null;
        return firstName + " " + lastName;
        ]]>
    </string>
    <string name="jobtitle" title="Job Title" min="2"/>
    <many-to-one name="site" title="Site" required="true" ref="Site"/>
    <many-to-one name="department" title="Department" ref="Department"/>
    <finder-method name="findByName" using="fullName" />
</entity>


<entity name="Department">
    <string name="name" title="Name" required="true" unique="true" min="2"/>
    <many-to-one name="site" ref="Site"/>
    <string name="description" title="Description" multiline="true" default=""/>
</entity>

<entity name="Site">
    <string name="code" title="Code" required="true" unique="true" min="2"/>
    <string name="name" title="Name" required="true" unique="true" min="2"/>
    <string name="description" title="Description" multiline="true" default=""/>
    <string name="address" title="Address" multiline="true" default=""/>
</entity>


<form name="employee-form" title="Employee" model="com.axelor.assetmanager.db.Employee">
    <panel title="Employee Details">
        <field name="firstName"/>
        <field name="lastName"/>
        <field name="jobtitle"/>
    </panel>
    <panel title="Affectation">
        <field name="site"/>
        <field name="department" **domain="self.site == site"**/>
    </panel>
</form>

And what I want is whene creating an employee, the department field shows only departments that belong to the selected site.

I tried to use domain but it gives me an error.

Can you edit your post to add the error you meet ?

I’ve solved it using action-attrs

<field name="site" onChange="action-employee-site-change"/>


<action-attrs name="action-employee-site-change">
    <attribute for="department" name="domain" expr="self.site = :site"/>
</action-attrs>

You don’t need to use a action-attrs for this, but you should use an action-record to reset your department if you change your site.

<field name="site" onChange="reset-department" />
        <field name="department" domain="self.site = :site" />

<action-record name="reset-department" ...>
<field name="department" expr="eval: null"/>
</action-record>

It’s not about reseting the department, but to be able to select only departments that belongs to the selected site.

Yeah, I get it. But what happen if you select a site A and department A1 whoch belong to A. If you switch to the site B, A1, doesn’t belong to B so you need to reset your department, right ?

Ummm, that’s right.

Ok well, i got it. And the code you’ve provided works just fine.

But i have a question : why using action-record instead of action-attrs ?

<action-attrs name="action-employee-reset-department">
    <attribute for="department" name="value" expr="eval: null"/>
    <attribute for="department" name="domain" expr="self.site = :site" if="site"/>
</action-attrs>

action-record is useful **for create, update data in the current form while action-attrs docus on editing how the form is working.

With action-record, you edit the value of field while with action-attrs you edit how the field is working.

Obvioulsy, you can use action-method to reproduce what action-record and action-attrs but in JAVA. You can manage the ActionResponse object in each method.

Hope my explanations are clear. I can provide you examples in you want more details.