簡體   English   中英

[ODOO15]打印來自 JavaScript 調用的報告

[英][ODOO15]Print a report from a JavaScript call

我制作了一個向導,它為一些stock.move.line元素創建報告,並從列表視圖中調用。 它運作良好。

現在,我想通過條碼界面中的按鈕打印該報告。 因此,它將從 JavaScript 調用中打印出來。 (當點擊每個stock.move.line上的按鈕時)

當我試圖從 JavaScript 調用我的報告時,我可以看到我的 python function 調用了創建報告的(我在控制台中添加了一些print()在里面但實際上我可以看到打印)。 我獨立測試了 python function,它工作正常。

我的問題:為什么從 JavaScript 打印報告不起作用?

我的 JavaScript 文件正在調用我的 python function 以進行報告打印:

odoo.define("colona_custo.print_product_label", function(require) {
"use strict";

var rpc = require("web.rpc");

function call_print_function(nb_printing, move_line_id)
{
    console.log(nb_printing);
    console.log(move_line_id);

    
    return rpc.query({
        model: 'print.product.label.wizard',
        method: 'print_product_label_from_js',
        args: [[], nb_printing, move_line_id]
    });



}

function print_product_label()
{
    var move_line_id = $(this).next().text();

    // Asking the user how many times he wants to print the label.
    $(document.body).append("<div id='warning_popup' style='background: rgba(255, 255, 255, 0.9); width: 50vw; height: 50vh; border-radius: 5px; padding: 10px; z-index: 1000; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);'><h2 style='position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);'>How many times do you want to print the product label?</h2><input id='user_entry' style='position:relative;top:50%;left:45%;'/><button id='action_print' style='position: absolute; bottom: 5px; left: 100px;' class='btn btn-primary'>Print</button><button id='close_popup_printing' style='position: absolute; bottom: 5px; left: 5px;' class='btn btn-primary'>Close</button></div>")
    $('#close_popup_printing').on('click', function() {
        $('#warning_popup').hide();
    });

    $('#action_print').on('click', function() {
        var nb_printing = $('#user_entry').val();
        if (nb_printing > 0) {
            call_print_function(nb_printing, move_line_id);
        }

    });
}

$(document).on('click', '.o_button_print_product_label', print_product_label)});

我的 python 文件位於 function 打印報告(它是_js之一):

from odoo import api, fields, models, api
from odoo.exceptions import UserError

class SmlPrintProductLabelWizard(models.TransientModel):

    _name = 'print.product.label.wizard'

    nb_printing = fields.Integer()

    def print_product_label(self):
        if (self.nb_printing >= 0):
            return self.env.ref('colona_custo.action_report_product_label').report_action([], data={'stock_move_line_id': self.env.context.get('active_id'), 'nb_printing': self.nb_printing})
        else:
            raise UserError("The number of printing is incorrect.")

    def print_product_label_from_js(self, nb_printing=1, move_line_id=34):
        print("===========================")
        print("Hello, function is launched.")
        return self.env.ref('colona_custo.action_report_product_label').report_action([], data={'stock_move_line_id': int(move_line_id), 'nb_printing': int(nb_printing)})

當我單擊界面中的相關按鈕時,我可以看到打印到導航器控制台中的 nb_printing 和 stock_move_line 的正確值。 我還可以在經典控制台中看到“功能已啟動”。

謝謝你的幫助。 問候,

The print_product_label_from_js function calls the report_action and returns an action definition which is handled by the web client when using for example a button of type object (The web client will execute the action for you)

rpc.query的情況下,Odoo 將運行 function 並返回Promise object,它將返回操作而不執行它。 所以你需要多一步,處理 promise object 來執行動作

例子:

call_print_function(nb_printing, move_line_id).then(function(action) {
    Component.env.services.report.do_action(action);
});

要使用Component ,請將其聲明如下:

var { Component } = owl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM