odoo 给form表单视图内联列表添加按钮

yizhihongxing

实践环境

Odoo 14.0-20221212 (Community Edition)

代码实现

模块文件组织结构

说明:为了更好的表达本文主题,一些和主题无关的文件、代码已略去

odoo14\custom\estate
│  __init__.py
│  __manifest__.py
│
├─models
│  estate_customer.py
│  estate_property_offer.py
│  __init__.py
│
├─static
│  │
│  └─src
│      └─xml
│             estate_customer_inline_tree_buttons.js
│
└─views
      estate_customer_views.xml
      webclient_templates.xml

测试模型定义

odoo14\custom\estate\models\estate_customer.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-


class EstateCustomer(models.Model):
    _name = 'estate.customer'
    _description = 'estate customer'

    name = fields.Char(required=True)
    age = fields.Integer()
    description = fields.Text()
    property_ids = fields.One2many("estate.property", "customer_id", string="Property")

odoo14\custom\estate\models\estate_property.py

class EstateProperty(models.Model):
    _name = 'estate.property'
    _description = 'estate property'
    name = fields.Char()
    status = fields.Char()
    customer_id = fields.Many2one('estate.customer')

测试模型视图定义

odoo14\custom\estate\views\estate_customer_views.xml

<?xml version="1.0"?>
<odoo>
    <!--此处代码略-->
    <record id="estate_customer_view_form" model="ir.ui.view">
        <field name="name">estate.customer.form</field>
        <field name="model">estate.customer</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name" />
                        <field name="age"/>
                        <field name="property_ids" widget="my_field_one_2_many">
                            <tree>
                                <field name="name"/>
                                <field name="status"/>
                            </tree>
                        </field>
                    </group>
                </sheet>
            </form>
        </field>
    </record>
</odoo>

说明:<field name="property_ids" widget="my_field_one_2_many">,其中my_field_one_2_many为下文javascript中定义的组件,实现添加自定义按钮;

my_field_one_2_many 组件定义

js实现

为列表视图添加自定义按钮

odoo14\custom\estate\static\src\js\estate_customer_inline_tree_buttons.js

odoo.define('estate.customer.fieldOne2Many', function (require) {
"use strict";
    var registry = require('web.field_registry');
    var FieldOne2Many = require('web.relational_fields').FieldOne2Many;
    var viewRegistry = require('web.view_registry');

    var MyFieldOne2Many = FieldOne2Many.extend({
        supportedFieldTypes: ['one2many'],
        events: _.extend({}, FieldOne2Many.prototype.events, {
            'click .o_button_upload_estate_customer': '_on_your_button_clicked'
        }),

        // 重写渲染按钮函数,添加按钮
        _renderButtons: function () {
             this._super.apply(this, arguments);
             this.$buttons = $('<button type="button" class="btn btn-primary o_button_upload_estate_customer">CustomButton</button>');
        },

        _on_your_button_clicked(){
            console.log('button clicked');
        },
    });

    registry.add('my_field_one_2_many', MyFieldOne2Many)
});

加载js脚本xml文件定义

odoo14\custom\estate\views\webclient_templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
         <xpath expr="//script[last()]" position="after">
             <script type="text/javascript" src="/estate/static/src/js/estate_customer_inline_tree_buttons.js"></script>
        </xpath>
    </template>
</odoo>

最终效果

odoo 给form表单视图内联列表添加按钮

~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:odoo 给form表单视图内联列表添加按钮 - Python技术站

(0)
上一篇 2023年3月31日
下一篇 2023年3月31日

相关文章

  • odoo 开发入门教程系列-添加修饰

    添加修饰 我们的房地产模块现在从商业角度来看是有意义的。我们创建了特定的视图,添加了几个操作按钮和约束。然而,我们的用户界面仍然有点粗糙。我们希望为列表视图添加一些颜色,并使一些字段和按钮有条件地消失。例如,当房产已出售或取消时,“已售出”和“取消”按钮应消失,因为此时不再允许更改状态。 参考: 文档关联的主题可以查看 Views. 内联视图(Inline …

    python 2023年4月18日
    00
  • odoo 给列表视图添加按钮实现数据文件导入

    实践环境 Odoo 14.0-20221212 (Community Edition) 代码实现 模块文件组织结构 说明:为了更好的表达本文主题,一些和主题无关的文件、代码已略去 odoo14customestate │ __init__.py │ __manifest__.py │ ├─models │ estate_customer.py │ __ini…

    2023年3月31日
    00
  • Odoo 基于Win10搭建基于Win10搭建odoo14开发环境搭建

    实践环境 win10 Python 3.6.2 odoo_14.0.latest.tar.gz下载地址: https://download.odoocdn.com/download/14/src?payload=MTY3MDg1MTM3Ni4xNC5zcmMud0tZRWZLX2I5UVF0Tm51UUVqT1lQVE5PbGRyYW5zWTc4dHhuOW…

    2023年4月2日
    00
  • Python 基于xml.etree.ElementTree实现XML对比

    测试环境 Python 3.6 Win10 代码实现 #!/usr/bin/env python 3.4.0 #-*- encoding:utf-8 -*- __author__ = ‘shouke’ import xml.etree.ElementTree as ET def compare_xml_node_attributes(xml_node1, x…

    Python开发 2023年4月2日
    00
  • 网络基础 CAS协议学习总结

    架构介绍 系统组件 CAS服务器和客户端构成了CAS系统体系结构的两个物理组件,它们通过各种协议进行通信。 CAS服务器 CAS服务器是基于Spring Framework构建的Java servlet,其主要职责是通过签发和验证ticket来验证用户并授予对启用CAS认证了的服务(通常称为CAS客户端)的访问权限。当用户成功登录(即认证通过)时,CAS服务…

    Java 2023年5月8日
    00
  • Odoo 自定义form表单按钮点击事件处理程序

    实践环境 Odoo 14.0-20221212 (Community Edition) 代码实现 方案1 通过研究发现,点击odoo form表单按钮时,会调用odoo14\odoo\addons\web\static\src\js\views\form\form_controller.js文件中的_onButtonClicked函数,在该函数中响应点击事件…

    Python开发 2023年3月31日
    00
  • Python 大数据量文本文件高效解析方案代码实现

    大数据量文本文件高效解析方案代码实现 测试环境 Python 3.6.2 Win 10 内存 8G,CPU I5 1.6 GHz 背景描述 这个作品来源于一个日志解析工具的开发,这个开发过程中遇到的一个痛点,就是日志文件多,日志数据量大,解析耗时长。在这种情况下,寻思一种高效解析数据解析方案。 解决方案描述 1、采用多线程读取文件 2、采用按块读取文件替代按…

    Python开发 2023年4月2日
    00
  • Python 基于win32com客户端实现Excel操作

    测试环境 Python 3.6.2 代码实现 非多线程场景下使用 新建并保存EXCEL import win32com.client from win32api import RGB def save_something_to_excel(result_file_path): excel_app = win32com.client.Dispatch(‘Exc…

    python 2023年4月30日
    00
合作推广
合作推广
分享本页
返回顶部