I've created a custom widget in Odoo, and display it for a form field. My template looks like this:
我在Odoo中创建了一个自定义小部件,并将其显示为表单字段。我的模板看起来像这样:
<t t-name="ImageDisplayer">
<img t-att-src="?"/>
</t>
How can I put the field's value into the <img>
tag's src
attribute?
如何将字段的值放入标签的src属性中?
2 个解决方案
#1
8
After spending a day digging in the source code, I've found the solution! It doesn't really involve the template, but I got the idea from the source code of default text field widget, so I think it shouldn't be considered as "hacking".
花了一天时间挖掘源代码后,我找到了解决方案!它并不真正涉及模板,但我从默认文本字段小部件的源代码中得到了这个想法,所以我认为它不应该被视为“黑客”。
Here's my custom widget class:
这是我的自定义窗口小部件类:
openerp.mymodule = function(instance, local) {
instance.ImageDisplayer = instance.web.form.AbstractField.extend({
template: "ImageDisplayer",
init: function (view, code) {
this._super(view, code);
},
// The key part:
render_value: function() {
this.$el[0].src = this.get("value");
}
});
instance.web.form.widgets.add('ImageDisplayer', 'instance.ImageDisplayer');
}
My template now does not contain anything special:
我的模板现在不包含任何特殊内容:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="ImageDisplayer">
<img />
</t>
</templates>
Works like a charm. It even updates the page whenever I do a change on server-side.
奇迹般有效。每当我在服务器端进行更改时,它甚至会更新页面。
Odoo documentation should really be more talkative!!!
Odoo文档真的应该更健谈!
Update: the answer applies to Odoo 8. It may work slightly differently in Odoo 9, because they've revised the UI framework in the new version.
更新:答案适用于Odoo 8.它在Odoo 9中可能略有不同,因为他们在新版本中修改了UI框架。
#2
1
We can do like that
我们可以这样做
<img t-att-src="kanban_image('model.name', 'image_small', record.id.value)"/>
Where
哪里
model.name
is table name,
model.name是表名,
image_small
is a field name which will hold/store binary type of data.
image_small是一个字段名称,用于保存/存储二进制数据类型。
EDIT:
编辑:
To display value of field in template, you may try with this
要在模板中显示字段的值,您可以尝试使用它
<img t-att-src="record.field_name"/>
#1
8
After spending a day digging in the source code, I've found the solution! It doesn't really involve the template, but I got the idea from the source code of default text field widget, so I think it shouldn't be considered as "hacking".
花了一天时间挖掘源代码后,我找到了解决方案!它并不真正涉及模板,但我从默认文本字段小部件的源代码中得到了这个想法,所以我认为它不应该被视为“黑客”。
Here's my custom widget class:
这是我的自定义窗口小部件类:
openerp.mymodule = function(instance, local) {
instance.ImageDisplayer = instance.web.form.AbstractField.extend({
template: "ImageDisplayer",
init: function (view, code) {
this._super(view, code);
},
// The key part:
render_value: function() {
this.$el[0].src = this.get("value");
}
});
instance.web.form.widgets.add('ImageDisplayer', 'instance.ImageDisplayer');
}
My template now does not contain anything special:
我的模板现在不包含任何特殊内容:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="ImageDisplayer">
<img />
</t>
</templates>
Works like a charm. It even updates the page whenever I do a change on server-side.
奇迹般有效。每当我在服务器端进行更改时,它甚至会更新页面。
Odoo documentation should really be more talkative!!!
Odoo文档真的应该更健谈!
Update: the answer applies to Odoo 8. It may work slightly differently in Odoo 9, because they've revised the UI framework in the new version.
更新:答案适用于Odoo 8.它在Odoo 9中可能略有不同,因为他们在新版本中修改了UI框架。
#2
1
We can do like that
我们可以这样做
<img t-att-src="kanban_image('model.name', 'image_small', record.id.value)"/>
Where
哪里
model.name
is table name,
model.name是表名,
image_small
is a field name which will hold/store binary type of data.
image_small是一个字段名称,用于保存/存储二进制数据类型。
EDIT:
编辑:
To display value of field in template, you may try with this
要在模板中显示字段的值,您可以尝试使用它
<img t-att-src="record.field_name"/>