在树视图odoo中显示html

时间:2017-07-14 11:54:11

标签: openerp odoo-9 odoo-10

是否可以在树状视图中显示html?

例如,将strong添加到字符串<强大的>我的STRING< / strong>

我尝试使用widget =“html”,但强标签可见!

的.py

 @api.depends('name')
 def _get_html(self):
     self.html_text = "<strong>" + str(self.name) + "</strong>"

     html_text = fields.Char(compute='_get_html')

.XML

<field name="html_text"/>   

1 个答案:

答案 0 :(得分:9)

要在列表视图中启用HTML,您需要覆盖方法_format(),如下所示。(对于Odoo v10)

<强> JS

odoo.define('html_in_tree_field.web_ext', function (require) {
    "use strict";
    var Listview = require('web.ListView');
    var formats = require('web.formats');

    Listview.Column.include({
        _format: function (row_data, options) {
        // Removed _.escape() function to display html content.
        // Before : return _.escape(formats.format_value(row_data[this.id].value, this, options.value_if_empty));
        return formats.format_value(row_data[this.id].value, this, options.value_if_empty);
        }
    });
});

要在JS上面添加的XML。

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_ext" inherit_id="web.assets_backend">
      <xpath expr="." position="inside">
          <script type="text/javascript" src="/html_in_tree_field/static/src/js/web_ext.js"></script>
      </xpath>
    </template>
</odoo>

<强> __清单__。PY

{
...
...
'data': [
        ...
        'views/above_xml_filename.xml',
    ],
....
}