Skip to Content
Menu
This question has been flagged
1 Reply
3385 Views

(using Odoo16)

I can write components and use them as widgets to my fields; but how can I write a component that is independent of any field and just add it to the view?

For example, the simple component in the Odoo tutorial documentation, I can use that if I add it to the field register and then use it as a field widget, but how could I use it on its own? (eg. maybe just an html tag like "hr" for example)

Many thanks!!

Avatar
Discard
Best Answer

In Odoo 16, you can create a standalone component that is independent of any specific field and add it to the view. This can be achieved by creating a custom widget that doesn't require being attached to a field.

Here's an example of how you can create a standalone component and use it in a view:

  1. Define your custom widget:
odoo.define('your_module.YourWidget', function (require) {
"use strict";
var basic_fields = require('web.basic_fields');

var YourWidget = basic_fields.Field.extend({
template: 'your_module.your_widget_template',

// Add any necessary logic and methods specific to your component

});

return YourWidget;
});

note: here i can't add xml with complete format because when i add current format of xml and save the answer i see the blank code,so here i remove the tag like(< >) and edit the file
  1. Create the template for your widget (your_widget_template.xml):
template id="your_module.your_widget_template" inherit_id="web.basic_fields.Field">
div

/div
/template

  1. Create a view and add your standalone component:

record id="view_your_component" model="ir.ui.view"
field name="name">your.component.view field name="model">your.model field name="arch" type="xml"
div>
!-- Add your standalone component by including the widget --
/div
/field
/record





In the above example, you define a custom widget YourWidget that extends the Field widget. You then specify a template for the widget, where you can define the HTML code for your standalone component.

To use this standalone component in a view, you include the widget in the desired location within the XML view definition. In this case, it's added as a field with the widget attribute set to "your_module.YourWidget". This will render your custom component independently in the view.

Make sure to replace "your_module" with the actual module name you're working with, and adjust the code according to your specific component requirements.

By following these steps, you can create a standalone component in Odoo 16 and include it in a view without being tied to a specific field.


Avatar
Discard
Author

Many thanks for the detailed response, unfortunately none of the code made it to the forum, so we can't really know what you did there :(

Related Posts Replies Views Activity
5
Jul 25
7427
0
Mar 25
585
1
Mar 25
1068
5
Mar 25
2671
1
Feb 25
1246