|
实践环境
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>
复制代码 说明:,其中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" >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">
-
- </xpath>
- </template>
- </odoo>
复制代码 最终效果
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
~~~~网站提示文字太少占位~~~~
来源:https://www.cnblogs.com/shouke/p/17135877.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|