首页 > 其他分享 >odoo 给form表单视图内联列表添加按钮

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

时间:2023-02-25 22:24:08浏览次数:40  
标签:customer xml form estate fields odoo14 py 视图 odoo

实践环境

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>

最终效果

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

标签:customer,xml,form,estate,fields,odoo14,py,视图,odoo
From: https://www.cnblogs.com/shouke/p/17135877.html

相关文章

  • Android Studio 设置视图的间距
    首先区分两个类型marginpaddingmargin是指当前视图与平级视图只见的关系距离有layout_marginlayout_marginLeftlayout_marginToplayout_marginRightlayout_marginB......
  • Android Studio 设置视图宽高
    这里有两种方法而第一种方法又分不同的类型以下是具体内容1、采用wrap_content定义wrap_content表示和自身一样的长度按照内容的多少去设定空间大小,然后按照权重的比......
  • avformat_seek_file函数介绍
    在做音视频数据分析的时候,经常会遇到这样的需求,每隔5分钟抽取一帧数据进行分析。在做播放器开发的时候,也会遇到这种情况,就是拖动进度条跳转到某个位置进行播放。如果直接用......
  • 视图解析、模板引擎
    视图解析指springboot在处理完请求想要跳转到某一个页面的过程,转发或者重定向,跳转到某个页面springboot默认不支持JSP,需要引入第三方模板引擎技术实现页面渲染、跳转。......
  • Improving Zero-Shot Coordination Performance Based on Policy Similarity 2023-
    基于策略相似度的零样本协调表现改进总结:这篇论文本质上是研究智能体的泛化性能,文中涉及的问题是在一个常规多智能体系统中的智能体如果要与新加入的或者说没有交互过的......
  • pytorch transforms
    transforms.Resize(size,interpolation=2)功能:改变图片大小为指定的尺寸size:输出图片的大小,如果size为(h,w)则输出尺寸和(h,w)一致,若为单值x,输出二维值图片时,则较小的边......
  • java代码上传文件,接口consumes = "multipart/*",headers = "content-type=multipart/f
    publicstaticStringdoPost(Stringurl,StringfileName,Filefile){CloseableHttpClienthttpClient=null;CloseableHttpResponseresponse=null;Str......
  • WinForm中DataGridView的单元格的显示格式DefaultCellStyle.Format自定义DefaultCellS
    第一步:继承ICustomFormatter,IFormatProvider接口,并实现实例代码:ICustomFormatter,IFormatProvider接口实现类publicclassKmFormatProvider:ICustomFormatte......
  • Odoo14_pdf下载功能实现
    1.安装wkhtmltopdfsudoaptinstallwkhtmltopdf2.安装pip包pip3installpdfkit3.代码实现#-*-coding:utf-8-*-fromodooimporthttpimportpdfkitcl......
  • pandas中的agg&transform方法
    pandas中的agg&transform方法1聚合函数agg1.1介绍agg方法是pandas中用于数据集汇总的函数,它可以将聚合行为应用于一组函数(字符串、函数或名称),这些函数将被应用于每一......