1. 程式人生 > 其它 >odoo 向qweb report中傳遞上下文context

odoo 向qweb report中傳遞上下文context

在列印報表的時候發現沒法將context傳入到qweb模板中,有時候又需要用到臨時的變數來做判斷:

調整js程式碼如下:

    /**
     * Generates an object containing the report's urls (as value) for every
     * qweb-type we support (as key). It's convenient because we may want to use
     * another report's type at some point (for example, when `qweb-pdf` is not
     * available).
     *
     * @param {Object} action
     * @returns {Object}
     */
    _makeReportUrls: function (action) {
        var reportUrls = {
            html: '/report/html/' + action.report_name,
            pdf: '/report/pdf/' + action.report_name,
            text: '/report/text/' + action.report_name,
        };
        // We may have to build a query string with `action.data`. It's the place
        // were report's using a wizard to customize the output traditionally put
        // their options.
        if (_.isUndefined(action.data) || _.isNull(action.data) ||
            (_.isObject(action.data) && _.isEmpty(action.data))) {
            if (action.context.active_ids) {
			//我改了這裡
                var activeIDsPath = '/' + action.context.active_ids.join(',')+'?context='+encodeURIComponent(JSON.stringify(action.context));
                reportUrls = _.mapObject(reportUrls, function (value) {
                    return value += activeIDsPath;
                });
            }
        } else {
            var serializedOptionsPath = '?options=' + encodeURIComponent(JSON.stringify(action.data));
            serializedOptionsPath += '&context=' + encodeURIComponent(JSON.stringify(action.context));
            reportUrls = _.mapObject(reportUrls, function (value) {
                return value += serializedOptionsPath;
            });
        }
        return reportUrls;
    },

改了這部分js 之後,就可以直接使用context裡邊的值了
比如我的context為:
{"lang":"en_US","tz":"Canada/Pacific","uid":232,"params":{"id":22677,"action":545,"model":"res.partner","view_type":"form","menu_id":366},"active_model":"res.partner","search_disable_custom_filters":true,"active_id":22677,"active_ids":[22677],"date_end":"2021-10-04"}


那麼要在qweb中獲取date_end 的值:
<span t-esc="date_end"/>

效果:



懂得,原來世界如此簡單!