自定义视图类型¶
为现有视图创建子类¶
假设我们需要创建一个通用视图的自定义版本。例如,在看板视图顶部添加一个类似丝带的额外小部件(用于显示某些特定的自定义信息)。在这种情况下,只需几个步骤即可完成:
扩展看板控制器/渲染器/模型,并将其注册到视图注册表中。
custom_kanban_controller.js¶import { KanbanController } from "@web/views/kanban/kanban_controller"; import { kanbanView } from "@web/views/kanban/kanban_view"; import { registry } from "@web/core/registry"; // the controller usually contains the Layout and the renderer. class CustomKanbanController extends KanbanController { static template = "my_module.CustomKanbanView"; // Your logic here, override or insert new methods... // if you override setup(), don't forget to call super.setup() } export const customKanbanView = { ...kanbanView, // contains the default Renderer/Controller/Model Controller: CustomKanbanController, }; // Register it to the views registry registry.category("views").add("custom_kanban", customKanbanView);
在自定义看板中,我们定义了一个新模板。我们既可以继承看板控制器模板并添加自己的模板片段,也可以定义一个全新的模板。
custom_kanban_controller.xml¶<?xml version="1.0" encoding="UTF-8" ?> <templates> <t t-name="my_module.CustomKanbanView" t-inherit="web.KanbanView"> <xpath expr="//Layout" position="before"> <div> Hello world ! </div> </xpath> </t> </templates>
在 arch 中通过
js_class属性使用该视图。<kanban js_class="custom_kanban"> <templates> <t t-name="card"> <!--Your comment--> </t> </templates> </kanban>
扩展视图的可能性无穷无尽。虽然这里我们只扩展了控制器,但您也可以扩展渲染器以添加新按钮、修改记录的呈现方式或自定义下拉菜单,还可以扩展模型和 buttonTemplate 等其他组件。
从零创建新视图¶
创建新视图属于高级主题。本指南仅重点介绍必要的步骤。
创建控制器。
控制器的主要职责是协调视图的各个组件,例如渲染器(Renderer)、模型(Model)和布局(Layout)。
beautiful_controller.js¶import { Layout } from "@web/search/layout"; import { useService } from "@web/core/utils/hooks"; import { Component, onWillStart, useState} from "@odoo/owl"; export class BeautifulController extends Component { static template = "my_module.View"; static components = { Layout }; setup() { this.orm = useService("orm"); // The controller create the model and make it reactive so whenever this.model is // accessed and edited then it'll cause a rerendering this.model = useState( new this.props.Model( this.orm, this.props.resModel, this.props.fields, this.props.archInfo, this.props.domain ) ); onWillStart(async () => { await this.model.load(); }); } }
控制器的模板使用 Layout 显示控制面板,同时也显示渲染器。
beautiful_controller.xml¶<?xml version="1.0" encoding="UTF-8"?> <templates xml:space="preserve"> <t t-name="my_module.View"> <Layout display="props.display" className="'h-100 overflow-auto'"> <t t-component="props.Renderer" records="model.records" propsYouWant="'Hello world'"/> </Layout> </t> </templates>
创建渲染器。
渲染器的主要作用是渲染包含记录的视图,从而生成数据的可视化呈现。
beautiful_renderer.js¶import { Component } from "@odoo/owl"; export class BeautifulRenderer extends Component { static template = "my_module.Renderer"; }
beautiful_renderer.xml¶<?xml version="1.0" encoding="UTF-8"?> <templates xml:space="preserve"> <t t-name="my_module.Renderer"> <t t-esc="props.propsYouWant"/> <t t-foreach="props.records" t-as="record" t-key="record.id"> // Show records </t> </t> </templates>
创建模型。
模型的职责是获取并管理视图中所有必需的数据。
beautiful_model.js¶import { KeepLast } from "@web/core/utils/concurrency"; export class BeautifulModel { constructor(orm, resModel, fields, archInfo, domain) { this.orm = orm; this.resModel = resModel; // We can access arch information parsed by the beautiful arch parser const { fieldFromTheArch } = archInfo; this.fieldFromTheArch = fieldFromTheArch; this.fields = fields; this.domain = domain; this.keepLast = new KeepLast(); } async load() { // The keeplast protect against concurrency call const { length, records } = await this.keepLast.add( this.orm.webSearchRead(this.resModel, this.domain, [this.fieldsFromTheArch], {}) ); this.records = records; this.recordsLength = length; } }
注解
对于高级场景,除了从零创建模型之外,也可以使用
RelationalModel,其他视图已经在使用它。创建 arch 解析器。
arch 解析器的职责是解析 arch 视图,以便视图能够访问其中的信息。
beautiful_arch_parser.js¶import { XMLParser } from "@web/core/utils/xml"; export class BeautifulArchParser extends XMLParser { parse(arch) { const xmlDoc = this.parseXML(arch); const fieldFromTheArch = xmlDoc.getAttribute("fieldFromTheArch"); return { fieldFromTheArch, }; } }
创建视图并将所有组成部分组合到一起,然后将该视图注册到视图注册表中。
beautiful_view.js¶import { registry } from "@web/core/registry"; import { BeautifulController } from "./beautiful_controller"; import { BeautifulArchParser } from "./beautiful_arch_parser"; import { BeautifylModel } from "./beautiful_model"; import { BeautifulRenderer } from "./beautiful_renderer"; export const beautifulView = { type: "beautiful", display_name: "Beautiful", icon: "fa fa-picture-o", // the icon that will be displayed in the Layout panel multiRecord: true, Controller: BeautifulController, ArchParser: BeautifulArchParser, Model: BeautifulModel, Renderer: BeautifulRenderer, props(genericProps, view) { const { ArchParser } = view; const { arch } = genericProps; const archInfo = new ArchParser().parse(arch); return { ...genericProps, Model: view.Model, Renderer: view.Renderer, archInfo, }; }, }; registry.category("views").add("beautifulView", beautifulView);
在 arch 中声明 视图。
... <record id="my_beautiful_view" model="ir.ui.view"> <field name="name">my_view</field> <field name="model">my_model</field> <field name="arch" type="xml"> <beautiful fieldFromTheArch="res.partner"/> </field> </record> ...