【问题标题】:How to update field for multiple selection in TreeView of Odoo如何在 Odoo 的 TreeView 中更新多选字段
【发布时间】:2019-01-17 10:51:00
【问题描述】:

我正在尝试在树视图中选择多个员工并希望更新他们的状态(选择字段有两个选项,即出席、缺席)。

status = fields.Selection(string="Status", selection=[('present', "Present"), ('absent', "Absent")])

我的第一种方法:

我使用act_window在“动作”下拉菜单中创建当前选项(选择多个复选框时出现)

<act_window name="Present"
    res_model="hr.employee"
    src_model="hr.employee"
    multi="True"
    key2="client_action_multi"
    id="employee_present"
/>

并尝试在 python 文件中为它定义一个函数,但我无法链接 act_window 到那个函数。

我的第二种方法:

然后我尝试另一种创建按钮并将其链接到 javascript 文件的方法,但这种方法也无法解决我的问题。

下面是文件hr_attendance/static/src/xml/qweb.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<templates id="sync_template" xml:space="preserve">
    <t t-extend="ListView.buttons">
        <t t-jquery="button.o_list_button_add" t-operation="after">
            <button class="btn btn-sm btn-default sync_button" type="button">Present</button>
        </t>
    </t>
</templates>

下面是sync.js hr_attendance/static/src/js的内容

openerp.hr_attendance = function(instance) {
    var ListView = instance.web.ListView;
    console.log('Inside Javascript Code method call...');
    ListView.include({
        render_buttons: function() {
            // GET BUTTON REFERENCE
            this._super.apply(this, arguments)
            if (this.$buttons) {
                var btn = this.$buttons.find('.sync_button')
            }
            
            // PERFORM THE ACTION
            btn.on('click', this.proxy('do_sync'))
        },
        do_sync: function() {
            new instance.web.Model('hr.attendance')
            .call('present', [[]])
            .done(function() {
                alert('done')
            })
        }
    });
}

下面是我在 javascript 代码中调用的函数

@api.multi
def present(self):
    self.write({'status': 'present'})

【问题讨论】:

  • 请编辑您的问题,向我们展示您目前拥有的代码(与您的问题相关)。

标签: javascript xml odoo odoo-8


【解决方案1】:

OCA Repositories 中有一个名为“批量编辑”的不错的社区模块。您可以非常轻松地为员工等业务对象创建批量编辑操作,只需在操作菜单中激活它们(Odoo V8 中的“更多”)。

另一种解决方案是使用 python 代码创建服务器操作。也可以为操作菜单激活服务器操作(odoo V8 服务器操作的右上角有一个按钮)。但是您将需要两个操作,其中第一个解决方案与一个操作一起工作。

此服务器操作的代码非常简单:

obj.browse(env.context.get('active_ids')).write({'my_field': my_value})

您显然必须在您的选择字段上写下,并且一个服务器操作的值是“存在”,另一个是“缺席”。

可以在 Settings/technical/Actions/Server Actions 中找到服务器操作。

【讨论】:

  • 谢谢@CZoellner。我可以在操作菜单中看到服务器操作。但是我必须在哪里放置 python 逻辑来更新状态?
  • 您必须更改操作的类型,以使文本字段可见。
  • 实际上,默认情况下,您应该已经看到一个包含大量注释 python 行的文本字段。只需将我的答案的一行放在它们之后或覆盖它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多