odoo12在tree视图自定义按钮并传值

odoo12在tree视图自定义按钮并传值

代码示例:

template.xml:

<?xml version="1.0" encoding="UTF-8"?>
<template id="template" xml:space="preserve">
  <t t-name="WageManageListView.employee_performance_manage">
        <button type="button" class="btn btn-secondary employee_performance_manage_submit_review">
            批量提交审核
        </button>
        <button type="button" class="btn btn-secondary employee_performance_manage_review_confirmed" groups="wage_manage.performance_review_group">
            批量确认审核
        </button>
    </t>

    <t t-extend="ListView.buttons" t-name="WageManageListView.employee_performance_manage_buttons">
        <t t-jquery="button.o_list_button_add" t-operation="after">
            <t t-js="ctx">
                if (window.odoo._modules.indexOf("base_import") >= 0) {
                    r.push(context.engine.tools.call(context, 'ImportView.import_button', dict));
                };
            </t>
            <t t-call="WageManageListView.employee_performance_manage"/>
        </t>
    </t>
</template>

创建对应的Js文件,主要是监听上面定义的按钮,根据触发的事件,操作后台。

odoo.define('employee.performance.manage.tree.button', function (require) {
    "use strict";

    let ListController = require('web.ListController');
    let ListView = require('web.ListView');
    let viewRegistry = require('web.view_registry');

    let EmployeePerformanceManageViewController = ListController.extend({
        buttons_template: 'WageManageListView.employee_performance_manage_buttons',
        renderButtons: function () {
            this._super.apply(this, arguments);
            if (this.$buttons) {
                var self = this;
                // 提交审核
                this.$buttons.on('click', '.employee_performance_manage_submit_review', function () {
                    let view_type = self.viewType;
                    let actived_ids = [];
                    let state = self.model.get(self.handle, {raw: true});
                    for (let i = 0; i < $('tbody .o_list_record_selector input').length; i++) {
                        if ($('tbody .o_list_record_selector input')[i].checked === true) {
                            actived_ids.push(state.res_ids[i]);
                        }
                    }
                    console.log(actived_ids);
                    // 至此已经获取到了勾选的项的ID,可以去调用后台的方法,或者打开一个新的页面,一个新的wizard
                    let ctx = state.context;
                    ctx['active_ids'] = actived_ids;
                      // 我这里是打开了一个新的wizard 将选中的id通过context传递过去进行相应的处理
                    self.do_action({
                        type: 'ir.actions.act_window',
                        res_model: 'employee.performance.manage.review',
                        target: 'new',
                        views: [[false, 'form']],
                        context: {
                            view_type: view_type,
                            active_ids: actived_ids,
                        },
                    },{
                        on_reverse_breadcrumb: function () {
                            self.reload();
                        },
                        on_close: function () {
                            self.reload();
                        }
                    });
                });
                // 确认审核
                this.$buttons.on('click', '.employee_performance_manage_review_confirmed', function () {
                    let view_type = self.viewType;
                    let actived_ids = [];
                    let state = self.model.get(self.handle, {raw: true});
                    for (let i = 0; i < $('tbody .o_list_record_selector input').length; i++) {
                        if ($('tbody .o_list_record_selector input')[i].checked === true) {
                            actived_ids.push(state.res_ids[i]);
                        }
                    }
                    // 至此已经获取到了勾选的项的ID,可以去调用后台的方法,或者打开一个新的页面,一个新的wizard
                    let ctx = state.context;
                    ctx['active_ids'] = actived_ids;
                      // 我这里是打开了一个新的wizard 将选中的id通过context传递过去进行相应的处理
                    self.do_action({
                        type: 'ir.actions.act_window',
                        res_model: 'employee.performance.manage.confirmed',
                        target: 'new',
                        views: [[false, 'form']],
                        context: {
                            view_type: view_type,
                            active_ids: actived_ids,
                        },
                    },{
                        on_reverse_breadcrumb: function () {
                            self.reload();
                        },
                        on_close: function () {
                            self.reload();
                        }
                    });
                });
            }
        }
    });

    let EmployeePerformanceeView = ListView.extend({
        config: _.extend({}, ListView.prototype.config, {
            Controller: EmployeePerformanceManageViewController,
        }),
    });

    viewRegistry.add('employee_performance_manage_class', EmployeePerformanceeView);
});

对传递的id进行处理:

# -*- coding: utf-8 -*-
import logging
from odoo import api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


class EmployeePerformanceTran(models.TransientModel):
    _name = 'employee.performance.manage.review'
    _description = "批量提交审核"

    performance_ids = fields.Many2many('employee.performance.manage', 'performance_manage_review_list_rel', string=u'员工绩效管理')

    @api.multi
    def submit_review(self):
        """
        批量提交审核
        :return:
        """
        self.ensure_one()
        self.performance_ids.write({'state': 'wait'})
        return {'type': 'ir.actions.act_window_close'}

    @api.model
    def default_get(self, fields):
        context = dict(self._context or {})
        view_type = context.get('view_type')
        active_ids = context.get('active_ids')
        if not active_ids:
            raise UserError("未选择要提交的记录!")
        result = super(EmployeePerformanceTran, self).default_get(fields)
        if 'performance_ids' in fields:
            result['performance_ids'] = [(6, 0, active_ids)]
        return result

本文省略了部分引入文件和其他的一些操作,最重要的是通过js在前端显示按钮并通过按钮点击将值传递到后台进行处理的js文件。

另外,我看到一个比较详细的文章:https://blog.csdn.net/tsoTeo/article/details/90716027 可以参照一下

效果图:

treeshutu2.png

treeshitu.png

Last modification:September 26th, 2019 at 08:46 am
If you think my article is useful to you, please feel free to appreciate

One comment

  1. 覃志辉

    很实用|´・ω・)ノ

Leave a Comment