qweb t-if in position attribures - odoo

I am developing a module in Odoo. I need to change the report of invoice to add some details.
I use xpath with position attribute and check if an affair is linked to hide the default table.
<template id="my_invoice" inherit_id="account.report_invoice_document">
<xpath expr="//div[#class='page']/table[#class='table table-condensed']" position="attributes">
<attribute t-if="o.affair_id" name="class">hidden</attribute>
</xpath>
</template>
This is not working. The default table is hidden in every invoices even if the invoice is not linked to an affair.
I do not understand because the condition works in my second template.
<template id="my_invoice2" inherit_id="account.report_invoice_document">
<xpath expr="//div[#class='page']/div[#class='row mt32 mb32']" position="after">
<t t-if="o.affair_id">
<!-- table with my additional detail -->
</t>
</xpath>
</template>
Sorry for my english. I am learning it.

try this one:
<attribute name="t-att-style">'display: none;' if o.affair_id else ''</attribute>
OR even this one also:
<attribute name="t-att-class">'hidden' if o.affair_id else ''</attribute>
it may help in your case.

Related

Change record color on Kanban view Odoo 14

I need help, I want to color the record in the CRM kanban view in red if my date field date_operation is less than current_date. I write this xml code but nothing happens.
N.B : I have created a date field current_date which gets date.today()
This is my code but the condition unable to executed
<xpath expr="//div[hasclass('oe_kanban_content')]" position="attributes">
<t t-if="record.date_operation.raw_value < record.current_date.raw_value">
<attribute name="t-attf-style">color: red;</attribute>
</t>
<t t-else="">
<attribute name="t-attf-style">color: blue;</attribute>
</t>
</xpath>

Odoo List view: set column to be always visible

I'm working with Odoo 14 and would like to know if there is an option in XML template to suppress optional="show"/"hide" setting inherited from parent template. For example I have following code in parent template:
<field name="code" optional="show" readonly="1"/>
On a child template I would like to do something like this:
<xpath expr="//field[#name='code']" position="attributes">
<attribute name="optional">Always on</attribute>
</xpath>
to set the code field to be always on, without option to hide or show the column. Is there any option to do so?
You can inherit that field's attribute and remove it.
<xpath expr="//field[#name='code']" position="attributes">
<attribute name="optional"></attribute>
</xpath>
Solution is in fact easy, just to replace the inherited element with newly defined - without optional attribute. This rewrites all attribute settings.
<!-- In child template: -->
<xpath expr="//field[#name='code']" position="replace">
<field name="code"/>
</xpath>

Change the Position of field - Odoo

I am using Odoo 10 and I am trying to move the position of the mobile field. The below code works but the new mobile field does not have any data. Mobile number is missing. I remove that code and the mobile number comes back.
<xpath expr="//field[#name='mobile']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//field[#name='category_id']" position="after">
<field name="mobile" />
</xpath>
You cannot have same field twice in the view. You need to completely remove the field first and add it to another place after that. Your templates work if you change the first xpath to remove the field, not just hide it. This can be done like this
<xpath expr="//field[#name='mobile']" position="replace">
</xpath>
<xpath expr="//field[#name='category_id']" position="after">
<field name="mobile" />
</xpath>
Veikko`s answer is universal for all versions of Odoo but requires to rewrite full dom structure in new place
For Odoo starting version 12.0 best for move field and other is (described here):
<xpath expr="//field[#name='category_id']" position="after">
<xpath expr="//field[#name='mobile']" position="move"/>
</xpath>

Add xpath in Odoo qweb report

How in inherit qweb add new element after last paragraph in original report:
<p t-if="o.comment">
<strong>Comment:</strong>
<span t-field="o.comment"/>
</p>
//add after <p t-if="o.comment">
<xpath expr="??" position="after">
<p>new</p>
</xpath>
Make sure to inherit the template id as below and then after add your xpath. Hope this helps you.
<template id="report_saleorder_inherit" inherit_id="report.external_layout_footer">
<xpath expr="//p[#t-if='o.comment']" position="after">
<p>new</p>
</xpath>
</template>
You can find the last p-element in your report by using xpath like this:
<xpath expr="(//p)[position()=last()]" position="after">
The (//p) part finds all p-elements and the filter [position()=last()] selects the last of them.
I assumed that the p element is in your base report and the xpath-part is in your inherited report.
Please keep in mind that the p element exists only if your model had data in comment field. The xpath does not know if the last one is the comment or not. It just blindly takes the last one from report and in your example it does not appear in the report if the t-if="o.comment" is not true.
Hope this helped you.
Br,
Veikko

Invisible two buttons using xpath in Odoo 10

In sale.order form view, there are two buttons Send by Email. One for draft state and another for sent, sale. I need to hide both buttons. I tried below code:
<xpath expr="//button[#name='action_quotation_send']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
The result is: The button in draft state become invisible, another button is still visible.
How can I hide that button too?
Use below code how it is, to hide both buttons by replacing with nothing.
<xpath expr="//button[#name='action_quotation_send']" position="replace"/>
<xpath expr="//button[#name='action_quotation_send']" position="replace"/>
I know it looks weird cause its exactly the same code twice but it worked for me.
We have to set attribute for both buttons.
Try with following code:
<xpath expr="//button[#name='action_quotation_send' and #states='sent']" position="attributes">
<attribute name="states" /> <!-- delete states attribute, it's influencing invisible behaviour -->
<attribute name="invisible">1</attribute>
</xpath
<xpath expr="//button[#name='action_quotation_send' and #states='draft']" position="attributes">
<attribute name="states" /> <!-- delete states attribute, it's influencing invisible behaviour -->
<attribute name="invisible">1</attribute>
</xpath>
I know it's late, but might help someone in future.
<xpath expr="(//button[#name='action_quotation_send'])[1]" position="attributes">
...
</xpath>
<xpath expr="(//button[#name='action_quotation_send'])[2]" position="attributes">
...
</xpath>
Refer How can you identify multiple elements with the same name in XPath?