Customize a fieldÂķ
Subclass an existing field componentÂķ
Let's take an example where we want to extends the BooleanField to create a boolean field
displaying "Late!" in red whenever the checkbox is checked.
Create a new widget component extending the desired field component.
late_order_boolean_field.jsÂķimport { registry } from "@web/core/registry"; import { BooleanField } from "@web/views/fields/boolean/boolean_field"; import { Component, xml } from "@nightpos/owl"; class LateOrderBooleanField extends BooleanField { static template = "my_module.LateOrderBooleanField"; }
Create the field template.
The component uses a new template with the name
my_module.LateOrderBooleanField. Create it by inheriting the current template of theBooleanField.late_order_boolean_field.xmlÂķ<?xml version="1.0" encoding="UTF-8" ?> <templates xml:space="preserve"> <t t-name="my_module.LateOrderBooleanField" t-inherit="web.BooleanField"> <xpath expr="//CheckBox" position="after"> <span t-if="props.value" class="text-danger"> Late! </span> </xpath> </t> </templates>
Register the component to the fields registry.
late_order_boolean_field.jsÂķregistry.category("fields").add("late_boolean", LateOrderBooleanField);
Add the widget in the view arch as an attribute of the field.
<field name="somefield" widget="late_boolean"/>
Create a new field componentÂķ
Assume that we want to create a field that displays a simple text in red.
Create a new Owl component representing our new field
my_text_field.jsÂķimport { standardFieldProps } from "@web/views/fields/standard_field_props"; import { Component, xml } from "@nightpos/owl"; import { registry } from "@web/core/registry"; export class MyTextField extends Component { static template = xml` <input t-att-id="props.id" class="text-danger" t-att-value="props.value" onChange.bind="onChange" /> `; static props = { ...standardFieldProps }; static supportedTypes = ["char"]; /** * @param {boolean} newValue */ onChange(newValue) { this.props.update(newValue); } }
The imported
standardFieldPropscontains the standard props passed by theViewsuch as theupdatefunction to update the value, thetypeof the field in the model, thereadonlyboolean, and others.In the same file, register the component to the fields registry.
my_text_field.jsÂķregistry.category("fields").add("my_text_field", MyTextField);
This maps the widget name in the arch to its actual component.
Add the widget in the view arch as an attribute of the field.
<field name="somefield" widget="my_text_field"/>