Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
2121 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor Nejlepší odpověď

Thank you very much! 
I have solved the problem

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
2
kvě 24
1428
1
čvn 24
4080
2
kvě 24
2292
2
kvě 24
3517
Owl Tutotrial Vyřešeno
1
bře 22
8466