【问题标题】:Remove download button from odoo's pdf_viewer widget从 odoo pdf 查看器小部件中删除下载按钮
【发布时间】:2021-05-04 13:57:31
【问题描述】:

我正在尝试禁用位于 Odoo (Pdf_viewer) 附件预览小部件中的下载按钮,如下面的代码所示:

 <field name="preview" attrs="{'readonly': [('preview', '=', True)]}" widget="pdf_viewer" />

PS:我尝试将该字段设置为只读,但仍将整个文件提供给读者。

我从朋友那里得到了一点帮助,他建议更改包含此按钮模板的 JS 文件,但我不知道这样做的步骤! 感谢您的帮助。

【问题讨论】:

    标签: javascript python widget odoo pdf-viewer


    【解决方案1】:

    您可以覆盖_disableButtons 函数并隐藏下载按钮。

    示例

    var basic_fields = require('web.basic_fields');
    
    basic_fields.FieldPdfViewer.include({
        _disableButtons: function (iframe) {
            $(iframe).contents().find('button#download').hide();
            // $(iframe).contents().find('button#secondaryDownload').hide();
            this._super(iframe);
        },
    });
    

    如果您需要使用上下文属性控制下载按钮的可见性,请尝试以下代码:

    var basic_fields = require('web.basic_fields');
    var Context = require('web.Context');
    
    basic_fields.FieldPdfViewer.include({
        _disableButtons: function (iframe) {
            var self = this;
            if (self.attrs.context) {
                var context = new Context(self.attrs.context).eval();
                if(!context.download) {
                    $(iframe).contents().find('button#download').hide();
                    // $(iframe).contents().find('button#secondaryDownload').hide();
                }
            }
            this._super(iframe);
        },
    });
    

    编辑:

    创建一个包含以下内容的 XML 文件,并将其添加到清单文件中的 data 条目中:

    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
            <template id="assets_backend" inherit_id="web.assets_backend" name="assets_backend">
                <xpath expr="." position="inside">
                    <script type="text/javascript"
                            src="/module_name/static/src/js/pdf_viewser.js"></script>
                </xpath>
            </template>
        </data>
    </odoo>
    

    static/src/js下创建pdf_viewser.js并添加上面的代码:

    odoo.define('module_name.PDFViewer', function (require) {
        "use strict";
        
        var basic_fields = require('web.basic_fields');
    
        basic_fields.FieldPdfViewer.include({
            _disableButtons: function (iframe) {
                $(iframe).contents().find('button#download').hide();
                // $(iframe).contents().find('button#secondaryDownload').hide();
                this._super(iframe);
            },
        });
    
    });
    

    Adding files in an asset bundle 部分中列出了这些步骤。有关更多详细信息,请查看Assets Management 文档。

    【讨论】:

    • 非常感谢@kenly,我只是将这一行添加到现有的 _disableButtons 函数中,如下所示:_disableButtons: function (iframe) { $(iframe).contents().find('button#openFile').hide(); $(iframe).contents().find('button#download').hide(); }, 它可以工作,问题是如果我想使用覆盖方法,我应该把你的代码放在下面var FieldPdfViewer = FieldBinaryFile.e.. 还是外面?
    • 您想应用您的修改而不编辑原始文件吗?
    • 是的,就像这个链接中显示的那样:How to extend / add new widget in chrome in pos
    • 检查我的编辑。我还添加了官方文档的链接。
    猜你喜欢
    • 2016-09-25
    • 2011-06-28
    • 2017-01-23
    • 2018-05-15
    • 2017-05-25
    • 1970-01-01
    • 2020-06-24
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多