跳至內容
選單
此問題已被標幟
7 回覆
24468 瀏覽次數

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

頭像
捨棄

Hello,

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


最佳答案

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.
頭像
捨棄

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>

最佳答案

Try

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

    <strong>Note: </strong>

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

</div>

頭像
捨棄

Have you tried this ?

最佳答案

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

 



頭像
捨棄

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*

最佳答案

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

頭像
捨棄
最佳答案

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>

頭像
捨棄
最佳答案

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"/>
頭像
捨棄
作者

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 ?

作者 最佳答案

Hello,


I still have this problem, Any help please !


BR

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
5月 25
688
1
4月 25
1799
1
2月 25
989
0
4月 25
995
0
10月 24
785