宜兴通达竭诚为您服务。

通达科技 - 宜兴电脑维护及IT服务外包服务商

自定义视图类型
  • 上传者: Administrator
  • 上传时间:2023年12月06日 07时28分47秒
摘要:
子类化现有视图 None [1] ** 假设我们需要创建一个通用视图的自定义版本。例如,一个带有一些额外的带状小部件的看板视图(用于显示一些特定的自定义信息)。在这种情况下,可以按照以下几个步骤完成: 扩展看板控制器/渲染器/模型并在视图注册表中注册。 custom_kanban_controlle ......
正文 相关文章 请喝咖啡

    子类化现有视图

    假设我们需要创建一个通用视图的自定义版本。例如,一个带有一些额外的带状小部件的看板视图(用于显示一些特定的自定义信息)。在这种情况下,可以按照以下几个步骤完成:

    1. 扩展看板控制器/渲染器/模型并在视图注册表中注册。

      custom_kanban_controller.js
      /** @odoo-module */
      
      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 {
          // Your logic here, override or insert new methods...
          // if you override setup(), don't forget to call super.setup()
      }
      
      CustomKanbanController.template = "my_module.CustomKanbanView";
      
      export const customKanbanView = {
          ...kanbanView, // contains the default Renderer/Controller/Model
          Controller: CustomKanbanController,
      };
      
      // Register it to the views registry
      registry.category("views").add("custom_kanban", customeKanbanView);
      

      在我们的自定义看板中,我们定义了一个新的模板。我们可以继承看板控制器模板并添加我们的模板片段,或者我们可以定义一个全新的模板。

      custom_kanban_controller.xml
      <?xml version="1.0" encoding="UTF-8" ?>
      <templates>
          <t t-name="my_module.CustomKanbanView" t-inherit="web.KanbanView" owl="1">
              <xpath expr="//Layout" position="before">
                  <div>
                      Hello world !
                  </div>
              </xpath>
          </t>
      </templates>
      
    2. 在arch中使用具有 js_class 属性的视图。

      <kanban js_class="custom_kanban">
          <templates>
              <t t-name="kanban-box">
                  <!--Your comment-->
              </t>
          </templates>
      </kanban>
      

    扩展视图的可能性是无限的。虽然我们只在这里扩展了控制器,但您还可以扩展渲染器以添加新的按钮,修改记录的呈现方式,或自定义下拉菜单,以及扩展其他组件,如模型和 buttonTemplate

    从头开始创建一个新视图

    创建新视图是一个高级主题。本指南仅强调必要的步骤。

    1. 创建控制器。

      控制器的主要作用是促进视图的各个组件之间的协调,如渲染器、模型和布局。

      beautiful_controller.js
      /** @odoo-module */
      
      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 {
          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();
              });
          }
      }
      
      BeautifulController.template = "my_module.View";
      BeautifulController.components = { Layout };
      

      控制器的模板显示带有布局和渲染器的控制面板。

      beautiful_controller.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <templates xml:space="preserve">
          <t t-name="my_module.View" owl="1">
              <Layout display="props.display" className="'h-100 overflow-auto'">
                  <t t-component="props.Renderer" records="model.records" propsYouWant="'Hello world'"/>
              </Layout>
          </t>
      </templates>
      
    2. 创建渲染器。

      渲染器的主要功能是通过渲染包含记录的视图来生成数据的可视化表示。

      beautiful_renderer.js
      import { Component } from "@odoo/owl";
      export class BeautifulRenderer extends Component {}
      
      BeautifulRenderer.template = "my_module.Renderer";
      
      beautiful_renderer.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <templates xml:space="preserve">
          <t t-name="my_module.Renderer" owl="1">
              <t t-esc="props.propsYouWant"/>
              <t t-foreach="props.records" t-as="record" t-key="record.id">
                  // Show records
              </t>
          </t>
      </templates>
      
    3. 创建模型。

      模型的作用是在视图中检索和管理所有必要的数据。

      beautiful_model.js
      /** @odoo-module */
      
      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,它被其他视图使用。

    4. 创建arch解析器。

      arch解析器的作用是解析arch视图,以便视图可以访问信息。

      beautiful_arch_parser.js
      /** @odoo-module */
      
      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,
              };
          }
      }
      
    5. 创建视图并将所有部分组合在一起,然后在视图注册表中注册视图。

      beautiful_view.js
      /** @odoo-module */
      
      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);
      
    6. 在arch中声明 view

      ...
      <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>
      ...
    本文章从网上收集,如有侵权请联系tderp@tderp.com删除
  • 微信扫一扫,一分也是爱:
  • 微信

服务原则及地区范围

宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。

我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!

通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。

宜兴地区提供上门服务:

  • 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
  • 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。
  • 联系电话:189-21-343434
  • 在线沟通: