This question has been flagged
7 Replies
20614 Views

Hello,

I have an html field and I want to display the field on Qweb report if it is not empty.

python code:

note= fields.Html(string="Note")

Qweb:

            <p t-if="doc.note">

                <strong>Note: </strong>

                <span t-raw="doc.note"/>

            </p>


if the the field is empty I got always the result:

Note:

Which is not as should be expected, I don't know if t-if tag is working on html.fields, please help !

Best regards

Avatar
Discard

Hello,

Thank you for yourreply, I tried your code but it does not work


Best Answer

You need to escape the < > in XML.

For me the following works:

                    <div t-if="html-field != '&lt;p&gt;&lt;br&gt;&lt;/p&gt;'">

                    </div>

Wich is essentialle probing for a content of <p><br/></p>.
The HTML field is filled with that string by odoo automatically if you leave the HTML form widget edit area empty.
Avatar
Discard

There is a better way nowadays :

<p t-if="not is_html_empty(doc.payment_term_id.note)">
<strong>Note :</strong>
<span t-field="doc.payment_term_id.note"/>
</p>

Best Answer

Try

<div t-if="doc.note != '<p><br/></p>'">

    <strong>Note: </strong>

    <span t-raw="doc.note"/>

</div>

Avatar
Discard

Have you tried this ?

Best Answer

Hi,


Since Odoo V14 there is a new helper called "is_html_empty". This built-in helper in the Odoo core now allows you to check if a field is empty or not. You can use this "is_html_empty" function in QWeb statements. You can then simply wrap this in a t-if statement as such: "t-if="is_html_empty(doc.note)" and do a t-else statement for example.

P.S: Sorry I wanted to paste a whole code block sample but sadly the official forum editor is completely broken again. You can see a sample in the core from Odoo here

 



Avatar
Discard

Does this work for mail.template?

Trying to make a template that fills note field from a module if not empty. Still haven't found a way to make it work if it matches '&lt;p&gt;&lt;br&gt;&lt;/p&gt;' aswell.

I mean for UI web template editor*

Best Answer

HI Jean

You just try <span> or <div> tag instead of <p> tag

I mean,

             <span t-if="doc.note">

                <strong>Note: </strong>

                <span t-raw="doc.note"/>

            </span>


or


            <div t-if="doc.note">

                <strong>Note: </strong>

                <span t-raw="doc.note"/>

            </div>

Avatar
Discard
Best Answer

This how Odoo does it, did you try their method?

https://github.com/odoo/odoo/blob/11.0/addons/website_quote/views/website_quote_templates.xml#L649

<div t-field="option_line.website_description" class="oe_no_empty"/>
Avatar
Discard
Author

Hello,

in my code Qweb there is also the string "Note", I don't want to display it the field note is emty, how I can do that please ?

Author Best Answer

Hello,


I still have this problem, Any help please !


BR

Avatar
Discard
Best Answer

salam jean44;

in order to check if the html.field is empty you need to add this code:

<t t-foreach="docs" t-as="doc">
                <t t-if="doc.html.field == '&lt;br&gt;'">
                    <t t-set="style" t-value=" 'display: none;' "/>
                </t>

<span  t-att-style="style" t-raw="html.field"/> 

</t>

where &lt;br&gt; is <br> decoded

Avatar
Discard