当我想在Qweb报告中打印日期时,我使用这个简单的小部件:
<span t-field="o.event_date" t-field-options='{"widget": "date"}' />
但是当我想要打印时间时我不能使用这个其他小部件,因为它不起作用,我认为它不存在:
<span t-field="o.event_time" t-field-options='{"widget": "float_time"}' />
有一种简单的方法可以让这个小部件在Qweb报告中运行吗?
我必须使用这种方式而不是那个小部件:
<t t-esc="'%02d:%02d' % (int(str(o.event_time).split('.')[0]), int(float(str('%.2f' % o.event_time).split('.')[1])/100*60))" />
顺便说一句,我将event_time
声明为浮动:
event_time = fields.Float("Time", size=5)
答案 0 :(得分:3)
您可以为float_time小部件添加新模块,您可以在模块中添加此format.js
openerp.float_time_hms = function(instance){
original_format_value = instance.web.format_value;
instance.web.format_value = function (value, descriptor, value_if_empty) {
switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) {
case 'float_time':
var pattern = '%02d:%02d:%02d';
if (value < 0) {
value = Math.abs(value);
pattern = '-' + pattern;
}
var hour = Math.floor(value);
var min = (value % 1) * 60;
min = Math.round((min + 0.0001)*1000)/1000;
if (min >= 60){
min = 0;
hour = hour + 1;
}
sec = (min % 1) * 60;
sec = Math.round((sec + .0001)*10000)/10000;
if (sec >= 60){
sec = 0;
min = min + 1;
}
return _.str.sprintf(pattern, hour, min, sec);
}
return original_format_value(value, descriptor, value_if_empty);
};
original_parse_value = instance.web.parse_value;
instance.web.parse_value = function (value, descriptor, value_if_empty) {
switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) {
case 'float_time':
var factor = 1;
if (value[0] === '-') {
value = value.slice(1);
factor = -1;
}
var float_time_triad = value.split(":");
if (float_time_triad.length != 3)
return factor * original_parse_value(value, {type: "float"});
var hours = original_parse_value(float_time_triad[0], {type: "integer"});
var minutes = original_parse_value(float_time_triad[1], {type: "integer"});
var seconds = original_parse_value(float_time_triad[2], {type: "integer"});
return factor * (hours + (minutes / 60) + (seconds / 3600) + .0001);
}
return original_parse_value(value, descriptor, value_if_empty);
};
};
之后将.js添加到.xml文件中。
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<template id="assets_backend" name="float_time_hms assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/float_time_hms/static/src/js/format.js"></script>
</xpath>
</template>
</data>
</openerp>