翼度科技»论坛 编程开发 python 查看内容

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

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
实践环境

Odoo 14.0-20221212 (Community Edition)
代码实现

模块文件组织结构

说明:为了更好的表达本文主题,一些和主题无关的文件、代码已略去
  1. odoo14\custom\estate
  2. │  __init__.py
  3. │  __manifest__.py
  4. ├─models
  5. │  estate_customer.py
  6. │  estate_property_offer.py
  7. │  __init__.py
  8. ├─static
  9. │  │
  10. │  └─src
  11. │      └─xml
  12. │             estate_customer_inline_tree_buttons.js
  13. └─views
  14.       estate_customer_views.xml
  15.       webclient_templates.xml
复制代码
测试模型定义

odoo14\custom\estate\models\estate_customer.py
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class EstateCustomer(models.Model):
  4.     _name = 'estate.customer'
  5.     _description = 'estate customer'
  6.     name = fields.Char(required=True)
  7.     age = fields.Integer()
  8.     description = fields.Text()
  9.     property_ids = fields.One2many("estate.property", "customer_id", string="Property")
复制代码
odoo14\custom\estate\models\estate_property.py
  1. class EstateProperty(models.Model):
  2.     _name = 'estate.property'
  3.     _description = 'estate property'
  4.     name = fields.Char()
  5.     status = fields.Char()
  6.     customer_id = fields.Many2one('estate.customer')
复制代码
测试模型视图定义

odoo14\custom\estate\views\estate_customer_views.xml
  1. <?xml version="1.0"?>
  2. <odoo>
  3.    
  4.     <record id="estate_customer_view_form" model="ir.ui.view">
  5.         <field name="name">estate.customer.form</field>
  6.         <field name="model">estate.customer</field>
  7.         <field name="arch" type="xml">
  8.             <form>
  9.                 <sheet>
  10.                     <group>
  11.                         <field name="name" />
  12.                         <field name="age"/>
  13.                         <field name="property_ids" widget="my_field_one_2_many">
  14.                             <tree>
  15.                                 <field name="name"/>
  16.                                 <field name="status"/>
  17.                             </tree>
  18.                         </field>
  19.                     </group>
  20.                 </sheet>
  21.             </form>
  22.         </field>
  23.     </record>
  24. </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
  1. odoo.define('estate.customer.fieldOne2Many', function (require) {
  2. "use strict";
  3.     var registry = require('web.field_registry');
  4.     var FieldOne2Many = require('web.relational_fields').FieldOne2Many;
  5.     var viewRegistry = require('web.view_registry');
  6.     var MyFieldOne2Many = FieldOne2Many.extend({
  7.         supportedFieldTypes: ['one2many'],
  8.         events: _.extend({}, FieldOne2Many.prototype.events, {
  9.             'click .o_button_upload_estate_customer': '_on_your_button_clicked'
  10.         }),
  11.         // 重写渲染按钮函数,添加按钮
  12.         _renderButtons: function () {
  13.              this._super.apply(this, arguments);
  14.              this.$buttons = $('<button type="button" >CustomButton</button>');
  15.         },
  16.         _on_your_button_clicked(){
  17.             console.log('button clicked');
  18.         },
  19.     });
  20.     registry.add('my_field_one_2_many', MyFieldOne2Many)
  21. });
复制代码
加载js脚本xml文件定义

odoo14\custom\estate\views\webclient_templates.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <odoo>
  3.     <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
  4.          <xpath expr="//script[last()]" position="after">
  5.             
  6.         </xpath>
  7.     </template>
  8. </odoo>
复制代码
最终效果


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

来源:https://www.cnblogs.com/shouke/p/17135877.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具