新建按钮

2019-06-26  本文已影响0人  葡萄柚子茶

方法一:需要依赖工具包

在tree视图上创建,导入按钮旁边新添加按钮

<record id="account_asset_ext.view_account_asset_asset_tree_button" model="ir.ui.view">
            <field name="name">account_asset_ext.asset.asset.tree.button</field>
            <field name="model">account.asset.asset</field>
            <field name="inherit_id" ref="account_asset.view_account_asset_asset_purchase_tree"/>
            <field name="arch" type="xml">
                <xpath expr="//tree" position="attributes">
                    <attribute name="buttons">[["btn1", "生成折旧/button1",""]]</attribute>
                </xpath>
            </field>
        </record>

需要将依赖的工具,名为tree_view_button,将其放置在自定义模块相同的文件目录下,如图所示,不要忘记在定义按钮模块的manifest.py文件中depends中添加’tree_view_button’,否则没有效果。

方法二:自定义按钮

1.首先需要创建static/src/xml/***.xml文件
创建template去拓展odoo原有的列表视图和form视图,要记得加载到manifest.py中

<?xml version="1.0" encoding="UTF-8"?>
<template>
    <t t-extend="ListView.buttons">
#o_list_button_save,是odoo的列表视图提供的【保存】按钮的标识
        <t t-jquery="button.o_list_button_save" t-operation="after">
            <button t-if="widget.model == 'account.asset.asset'" type="button"
                    class="btn btn-sm btn-primary o_list_create_depreciation">生成资产折旧
            </button>
        </t>
    </t>
</template>

2.创建wizard/py文件
这个文件定义模型和方法

# -*- coding: utf-8 -*-

from odoo import models, fields, api
import logging
import time
import os
import datetime

_logger = logging.getLogger(__name__)


class GenerateAssetDepreciation(models.TransientModel):
    _name = 'generate.asset.depreciation.wizard'

    date = fields.Date('Date', required=True)

    def generate_depreciation(self):
        pass

3.创建wizard/xml文件
这个xml文件里写点击按钮后弹出的视图

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="generate_asset_depreciation_wizard" model="ir.ui.view">
            <field name="name">generate.asset.depreciation.wizard.form</field>
            <field name="model">generate.asset.depreciation.wizard</field>
            <field name="arch" type="xml">
                <form>
                    <sheet>
                        <group>
                            <field name="date" string="日期" />
                        </group>
                    </sheet>

                    <footer>
                        <button name="generate_depreciation"
                                type="object"
                                string="生成折旧"
                                class="oe_highlight" />
                        <button special="cancel" string="关闭" />
                    </footer>
                </form>
            </field>
        </record>

        <record id="generate_asset_depreciation_wizard_act_window" model="ir.actions.act_window">
            <field name="name">生成资产折旧</field>
            <field name="type">ir.actions.act_window</field>
            <field name="target">new</field>
            <field name="res_model">generate.asset.depreciation.wizard</field>
            <field name="view_id" ref="generate_asset_depreciation_wizard" />
            <field name="view_mode">form</field>
        </record>
    </data>
</odoo>

4.创建static/src/js/***.js文件
这个文件主要监听上面定义的按钮,根据触发的事件,操作后台

odoo.define('create_asset_depreciation', function (require) {
    "use strict";
    var core = require('web.core');
    var ListView = require('web.ListView');
    var Model = require('web.Model');
    var session = require('web.session');
    var web_client = require('web.web_client');
    var ajax = require("web.ajax");
    var Dialog = require('web.Dialog');

    var QWeb = core.qweb;
    var _t = core._t;

    function open_asset_depreciation_wizard() {
        web_client.action_manager.do_action({
                name: "生成折旧",
                type: "ir.actions.act_window",
                res_model: "generate.asset.depreciation.wizard",
                target: 'new',
                xml_id: 'account_asset_asset.generate_asset_depreciation_wizard',
                views: [[false, 'form']]
            });
    }

    ListView.include({
        render_buttons: function () {
            let add_button = false;
            if (!this.$buttons) {
                add_button = true;
            }
            let result = this._super.apply(this, arguments);
            if (add_button) {
                this.$buttons.on('click', '.o_list_create_depreciation', open_asset_depreciation_wizard.bind(this));
            }
            return result;
        }
    });
});

5.创建views/***.xml文件
用于引入上面的JS文件,不要忘了把它加载到manifest.py里面

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="asset_depreciation" name="asset_depreciation_wizard" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/account_asset_extend/static/src/js/asset_detail_month.js" />
        </xpath>
    </template>
</odoo>
上一篇 下一篇

猜你喜欢

热点阅读