Odoo17_widget继承
1.js
/** @odoo-module **/ import { registry } from "@web/core/registry"; import { FloatField, floatField } from "@web/views/fields/float/float_field"; export class FloatDisplay extends FloatField { showformattedValue() { let value = this.props.record.data[this.props.name] // 处理所有可能的空值情况 if (value === false || value === undefined || value === null) { return ""; } // 转换字符串为数字 const numValue = typeof value === "string" ? parseFloat(value) : value; // 确保是有效数字 if (typeof numValue !== "number" || isNaN(numValue)) { return ""; } // 格式化数字 return numValue.toFixed(2); } } FloatDisplay.template = "yzl_base.float_display"; export const floatDisplay = { ...floatField, component: FloatDisplay, }; registry.category("fields").add("float_display", floatDisplay);
2.xml
<?xml version="1.0" encoding="UTF-8"?> <templates> <t t-name="yzl_base.float_display"> <span t-if="props.readonly" t-esc="this.showformattedValue()"/> <input t-else="" t-on-focusin="onFocusIn" t-on-focusout="onFocusOut" t-att-id="props.id" t-ref="numpadDecimal" t-att-placeholder="props.placeholder" t-att-type="props.inputType" inputmode="decimal" class="o_input" autocomplete="off" t-att-step="props.step" /> </t> </templates>