odoo17 tree视图添加按钮

需求描述

点击下图中tree视图上的同步退货单按钮,弹出相应的form视图进行退货单同步,然后点击同步按钮调用后端python代码处理。

在这里插入图片描述

实现步骤

主要文件目录结构

在这里插入图片描述

js文件的创建
/** @odoo-module **/
import { registry } from "@web/core/registry";
import { listView } from "@web/views/list/list_view";
import { ListController } from "@web/views/list/list_controller";
import { useService } from "@web/core/utils/hooks";
export class OwlDemoTest extends ListController {

    setup() {
        super.setup();
        this.orm = useService("orm");
    }
    async message_button() {
        await this.orm.call('template.mini.program', 'update_message', [])
        await this.model.load();
     }

}
registry.category("views").add("owl_demo_test", {
    ...listView,
    Controller: OwlDemoTest,
    buttonTemplate: "OwlDemoButtons",
});

define方法的第一个参数为名称,自行定义,require方法表示所用到的依赖,然后对按钮的点击事件绑定相应的方法,这个方法返回一个动作视图(就是所定义的瞬态模型的向导)

创建按钮模板

也就是定义在/static/xml 文件夹下面的xml文件,用来定义所加的按钮

<?xml version="1.0" encoding="utf-8"?>
<templates id="template" xml:space="preserve">

<t t-name="OwlDemoButtons" t-inherit="web.ListView.Buttons" t-inherit-mode="primary" owl="1">
    <xpath expr="(//div/*)[last()]" position="after">
        <button class="btn btn-primary" type="button" t-on-click="message_button">
            测试按钮
        </button>
    </xpath>
</t>

</templates>
xml、js文件引入到__manifest__.py
    "assets": {
        "web.assets_backend":[
            "erp_app/static/src/js/test.js",
            "erp_app/static/src/xml/test.xml",
        ]
    }
对应模型添加方法
    @api.model
    def update_message(self):
        print('执行的代码')
tree视图添加 js_class
<tree js_class="owl_demo_test">
	...
</tree>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Odootree视图可以通过以下方进行编写: 1. 定义模型 首先,需要定义模型对象。可以使用Odoo的模型类来定义模型对象,例如: ``` from odoo import models, fields class MyModel(models.Model): _name = 'my.model' name = fields.Char('Name') description = fields.Text('Description') ``` 2. 定义tree视图 接下来,需要定义tree视图。可以使用Odoo视图类来定义tree视图,例如: ``` <odoo> <data> <record model="ir.ui.view" id="my_model_tree_view"> <field name="name">My Model Tree View</field> <field name="model">my.model</field> <field name="arch" type="xml"> <tree> <field name="name"/> <field name="description"/> </tree> </field> </record> </data> </odoo> ``` 在上面的示例中,我们定义了一个名为“my_model_tree_view”的tree视图,并指定了模型对象“my.model”。在tree中,我们定义了两个字段:“name”和“description”。 3. 注册视图 最后,需要将tree视图注册到Odoo中。可以使用Odoo的模块类来注册视图,例如: ``` from odoo import api, SUPERUSER_ID class MyModule(models.Model): _name = 'my.module' @api.model def _register_hook(self): # Register tree view self.env['ir.ui.view'].search([ ('name', '=', 'My Model Tree View'), ('model', '=', 'my.model') ]).write({ 'arch': self.env.ref('my_module.my_model_tree_view').arch }) ``` 在上面的示例中,我们在模块类中注册了tree视图。我们通过搜索现有的视图来找到我们定义的tree视图,并将其arch更新为我们定义的视图arch。 这样,我们就完成了Odootree视图的编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁小红馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值