實踐環境
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" >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="https://www.cnblogs.com/estate/static/src/js/estate_customer_inline_tree_buttons.js"></script>
</xpath>
</template>
</odoo>
最終效果

~~~~網站提示文字太少占位~~~~
~~~~網站提示文字太少占位~~~~
~~~~網站提示文字太少占位~~~~
~~~~網站提示文字太少占位~~~~
~~~~網站提示文字太少占位~~~~
作者:授客
微信/QQ:1033553122
全國軟體測驗QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額隨意,您的支持將是我繼續創作的源動力,打賞后如有任何疑問,請聯系我!!!
微信打賞
支付寶打賞 全國軟體測驗交流QQ群
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/545038.html
標籤:其他
