Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
2 Besvarelser
2079 Visninger

I need to override the parse(value) function in the js FloatField class on odoo 16. How to write a class that extends the FloatField class without declaring registry.category(“field”).add…
This is class js FloatField:
src: addons/web/static/src/views/fields/float/float_field.js

/** @odoo-module **/

import { _lt } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
import { useInputField } from "../input_field_hook";
import { useNumpadDecimal } from "../numpad_decimal_hook";
import { formatFloat } from "../formatters";
import { parseFloat } from "../parsers";
import { standardFieldProps } from "../standard_field_props";

const { Component } = owl;

export class FloatField extends Component {
setup() {
this.inputRef = useInputField({
getValue: () => this.formattedValue,
refName: "numpadDecimal",
parse: (v) => this.parse(v),
});
useNumpadDecimal();
}

parse(value) {
debugger
return this.props.inputType === "number" ? Number(value) : parseFloat(value);
}

get formattedValue() {
debugger
if (this.props.inputType === "number" && !this.props.readonly && this.props.value) {
return this.props.value;
}
return formatFloat(this.props.value, { digits: this.props.digits });
}
}

FloatField.template = "web.FloatField";
FloatField.props = {
...standardFieldProps,
inputType: { type: String, optional: true },
step: { type: Number, optional: true },
digits: { type: Array, optional: true },
placeholder: { type: String, optional: true },
};
FloatField.defaultProps = {
inputType: "text",
};

FloatField.displayName = _lt("Float");
FloatField.supportedTypes = ["float"];

FloatField.isEmpty = () => false;
FloatField.extractProps = ({ attrs, field }) => {
return {
inputType: attrs.options.type,
step: attrs.options.step,
// Sadly, digits param was available as an option and an attr.
// The option version could be removed with some xml refactoring.
digits: (attrs.digits ? JSON.parse(attrs.digits) : attrs.options.digits) || field.digits,
placeholder: attrs.placeholder,
};
};

registry.category("fields").add("float", FloatField);

Please help me.Thank you very much.



Avatar
Kassér
Bedste svar

Hi,


For overriding a function without directly changing the source code, we can use the patching method.


For example, use the below code and add your logic inside the function parse:


/** @odoo-module */

import { patch } from '@web/core/utils/patch';

import { FloatField } from '@web/views/fields/float/float_field';


patch(FloatField.prototype, {

    async setup() {

        super.setup();

    },

    parse(value) {

        // you can write your logic here

    }

});


Hope it helps.

Avatar
Kassér
Forfatter Bedste svar

Thank you very much! 
I have solved the problem

Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
2
maj 24
1399
1
jun. 24
4024
2
maj 24
2253
2
maj 24
3475
1
mar. 22
8453