页面树结构
转至元数据结尾
转至元数据起始

该宏示例在 V10.5上 验证通过

提示:本文档的示例代码仅适用于本文档中的示例报表/场景。若实际报表/场景与示例代码无法完全适配(如使用功能不一致,或多个宏代码冲突等),需根据实际需求开发代码。

示例说明

在即席查询中切换一个参数,不同的参数值打开不同的报表。如下图:

切换【报表】参数,值为1的时候显示报表1,值为2的时候显示报表2, 值为3的时候显示报表3:

设置方法

  1. 在公共设置中,创建【报表】参数。
     
     
  2. 创建一个“报表”过滤器,如下图:
      
  3. 在“分析展现”节点下分别创建即席查询报表1、报表2、报表3。
  4. 切换到 公共设置 > 宏资源包 节点下,新建一个宏资源包,类型选择即席查询,适用范围选择报表1、报表2和报表3。
     
  5. 双击此宏资源包进入报表宏界面。
  6. 在报表宏界面新建客户端模块,在弹出的新建模块对话框中选择对象为simpleReport;事件为onParamValueChanged;并把下面宏代码复制到代码区域。

宏类型

类型

对象

事件

ClientSide

simpleReport

onParamValueChanged

宏代码

// 以下宏适用于10.5.8及以上版本

// 宏类型
// 类型: ClientSide    对象:simpleReport    事件: onRender

// 宏代码
// 注意事项
// 1. 切换报表参数, 备选值是静态列表, 真实值从0开始, 显示值自定义为什么都行
// 2. reportIds放置需要切换的报表的id, 第一个对应切换报表参数的真实值为0的参数

function main(simpleReport, simpleReportContext) {
    var reportIds = ['I8a8a9fd30183e906e90649ae0183ea57d1160a3f', 'I8a8a9fd30183e906e90649ae0183ea594eeb0ab5'];
    var switchReportParam = '切换报表'
    redefinedParamChangeFn(simpleReport, simpleReportContext, reportIds, switchReportParam);
}
// 重定义切换参数逻辑
function redefinedParamChangeFn (combinedQV, combinedQVNavigator, reportIds, switchReportParam) {
    if (combinedQV.paramPanelObj) {
        combinedQV.paramPanelObj.onSelectChange.unsubscribe(combinedQV.doParamChange, combinedQV);
        combinedQV.paramPanelObj.onSelectChange.subscribe(function(paramPanel, param, oldId, newId, oldValue, newValue, notRefresh){
            if (isTargetReport(reportIds, this.combinedQuery.datas.queryId) && param.name === switchReportParam) {
                if (reportIds[param.value]) {
                    var args = Array.prototype.concat.apply([reportIds],arguments);
                    switchReport.apply(this, args);
                } else {
                    alert("切换报表宏中 reportIds[" + param.value + "] 没指定报表id");
                }
                return;
            }
            this.doParamChange.apply(this, arguments);
        }, combinedQV);
    }
}
// 是否是目标报表
function isTargetReport (reportIds, id) {
    return reportIds.findIndex(e => id === e) > -1;
}
// 切换报表
function switchReport(reportIds, paramPanel, param, oldId, newId, oldValue, newValue, notRefresh) {
    var targetReportId = reportIds[param.value];
    var currentReportId = this.combinedQuery.datas.queryId;
    if (targetReportId !== currentReportId) {
        // 关闭旧报表的后端对象
        util.remoteInvokeEx('CombinedQueryService', 'closeCombinedQuery', [ this.combinedQuery.datas.clientId ]);
        // 打开新报表
        var combinedQVCommand = this.combinedQuery.queryCmd;
        var node = combinedQVCommand.node;
        var commandFactory = node && node.tree && node.tree.commandFactory;
        if (commandFactory) {
            var command = commandFactory.getCommand('CombinedQueryCommand');
        } else {
            var CombinedQueryCommand = jsloader.resolve('smartbi.combinedquery.CombinedQueryCommand');
            command = new CombinedQueryCommand();
        }
        command.execute('OPENWITHNOREFRESH', {
            _id: targetReportId,
            _type: 'COMBINED_QUERY',
            tree: {
                commandFactory: commandFactory
            },
            userParamsInfo: [{name: param.name, value: newId, displayValue: newValue}]
        });
        var timeout = null;
        var refreshFn = function() {
            if (command.combinedQuery.getQueryView()) {
                command.combinedQuery.getQueryView().doRefresh();
                clearTimeout(timeout);
            } else {
                timeout = setTimeout(refreshFn, 10);
            }
        }
        timeout = setTimeout(refreshFn, 10);
    }
}

资源下载

资源:migrate.xml