由于页面这些按钮是异步加载的,故使用MutationObserver监控
odoo.define('my_module.my_script', function (require) {
'use strict';
let domReady = require('web.dom_ready');
domReady(() => {
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
if (node.children) {
let button = Array.from(node.children)
.filter(function (node) {
return node.classList.contains('o_list_export_xlsx');
});
if (button) {
replaceExportButton(button[0]);
}
}
});
}
});
});
let config = {childList: true, subtree: true};
observer.observe(document.body, config);
});
// 替换导出按钮的函数
function replaceExportButton(button) {
if (button && button.nodeName === 'BUTTON' && button.classList.contains('o_list_export_xlsx')) {
// 创建一个新的按钮元素
let newButton = document.createElement('button');
newButton.textContent = 'Custom Export'; // 设置新的文本
newButton.className = 'btn btn-primary o_list_export_xlsx'; // 设置新的类名
newButton.type = 'button'; // 设置类型
// 复制原有按钮的事件监听器到新按钮(可选)
// ...
// 替换旧按钮
button.parentNode.replaceChild(newButton, button);
}
}
});