javascript - Odoo:odoo树视图标题中的自定义按钮不会触发python函数
问题描述
基本信息:
Odoo 版本:10.0
模块名称:simcard
型号名称:simcard.simcard
目的:在树视图的标题中添加一个同步按钮并将其链接到 python 函数。
我的模板文件(template.xml):
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-extend="ListView.buttons">
<t t-jquery="button.o_list_button_add" t-operation="replace">
<t t-if="widget.model=='simcard.simcard'">
<button class="btn btn-sm btn-default sync_button" type="button" >Sync</button>
</t>
</t>
</t>
</templates>
我的 javascript 文件(tree_view_button.js):
odoo.define('simcard.tree_view_button', function (require){"use strict";
var ListView = instance.web.ListView;
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('simcard.simcard')
.call('my_function', [[]])
.done(function(result) {
alert('done')
})
}
});
}
我的python文件函数(models.py):
def my_function(self):
print 'fooooooooooooooo'
我的视图文件(view.xml):
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<template id="assets_backend" name="tree view menu"
inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="simcard/static/js/tree_view_button.js">
</script>
</xpath>
</template>
</data>
</odoo>
我的清单文件:
# -*- coding: utf-8 -*-
{
'name': "simcard",
'summary': """
Store them""",
'description': """
Store them""",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/master/odoo/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['base'],
# always loaded
'data': [
# 'security/ir.model.access.csv',
'views/views.xml',
'views/templates.xml',
],
'qweb': ['static/xml/tree_view_button.xml'],
# only loaded in demonstration mode
'demo': [
'demo/demo.xml',
],
'installable': True,
'auto_install': False,
'application': True,
}
同步按钮出现在树视图标题中,但是当我单击该按钮时,我无法调用我的函数。有什么我想念的吗?
解决方案
您的 js 代码中有一些错误,以下代码应该可以按预期工作
odoo.define('simcard_piavita.tree_view_button', function (require){
"use strict";
var ListView = require('web.ListView');
var Model = require('web.DataModel');
ListView.include({
render_buttons: function() {
this._super.apply(this, arguments)
if (this.$buttons) {
var btn = this.$buttons.find('.sync_button')
btn.on('click', this.proxy('do_sync'))
}
},
do_sync: function() {
new Model('simcard_piavita.simcard_piavita')
.call('my_function', [[]])
.done(function(result) {
alert('done')
})
}
});
});
推荐阅读
- reactjs - 为什么 onPress 在渲染新视图后不执行,但在渲染本机渲染之前执行?
- python - C4365:'return':从 'std::size_t' 转换为 'Py_ssize_t',有符号/无符号不匹配错误
- reactjs - React.js - 显示模式时禁用背景滚动
- python - 每 15 分钟发送一条关于不和谐的消息
- java - 创建多个列表时指向同一个元素
- google-forms - 网站中集成的 google 表单无法在 Mac 上打开,但可以在 PC 上使用。错误消息“forms.gle 拒绝连接。”
- python - 如何在 gui 屏幕内的 tkinter GUI 中显示熊猫数据框?
- javascript - 有没有办法在 switch 语句中转换 if 语句?
- r - 如何在 R 中重现预测函数的结果
- css-selectors - 使用 GTM 从 Div 标签中获取 ID