如何将参数传递给web api服务上的reportSource

时间:2014-05-30 13:12:37

标签: telerik reporting reportviewer asp.net-web-api telerik-reporting

报告:报告库

RepoerViewer:Html5

ReportService:Web Api

在Web api服务中,dataSource带来了32,000行数据。但报告中只使用了10行数据。这样,报告速度很慢。如何将参数传递给web api服务?

的ReportViewer:

<div id="reportViewer1" class="k-widget">
    loading...
</div>
<script type="text/javascript">
    $("#reportViewer1")
        .telerik_ReportViewer({
            serviceUrl: "http://localhost:2404/api/StokPaketList/",
            templateUrl: '../ReportViewer/templates/telerikReportViewerTemplate.html',
            reportSource: {
                report: "BeeSp.ReportLibrary.Stok.RprStokPaket, BeeSp.ReportLibrary",
                parameters: {
                    CultureID: "en"
                }
            }
        });
</script>

报告Web Api服务:

class CustomReportResolverWithFallBack : IReportResolver
{
    readonly IReportResolver _parentResolver; private IStokPaketService _stokPaketService;
    public CustomReportResolverWithFallBack(IReportResolver parentResolver, IStokPaketService stokPaketService)
    {
        this._parentResolver = parentResolver;
        _stokPaketService = stokPaketService;
    }
    public ReportSource Resolve(string report)
    {
        var reportDocument = this.CustomReportResolver(report);
        if (null == reportDocument
           && null != this._parentResolver)
        {
            reportDocument = this._parentResolver.Resolve(report);
        }
        return reportDocument;
    }
    public ReportSource CustomReportResolver(string report)
    {
        var type = Type.GetType(report);
        var reportInstance = (Report)Activator.CreateInstance(type);
        //in this case, report data source brings 32,000 rows of data
        var reportData = _stokPaketService.GetCStokPakets("GOLF").ToList();
        var objectDataSouce = new ObjectDataSource { DataSource = reportData }; 
        reportInstance.DataSource = objectDataSouce;
        return reportInstance;
    }
}

1 个答案:

答案 0 :(得分:3)

我假设有一种更好的方法可以做到这一点,但我所做的是通过创建一个序列化的JSON对象将所有参数放入reportSource.report参数。然后在CustomerReportResolver()方法中,我使用Newtonsoft.Json库反序列化对象。如下所示:

var json = {};
json.reportType = "BeeSp.ReportLibrary.Stok.RprStokPaket, BeeSp.ReportLibrary";
json.rows = 10;

$("#reportViewer1")
    .telerik_ReportViewer({
        serviceUrl: "http://localhost:2404/api/StokPaketList/",
        templateUrl: '../ReportViewer/templates/telerikReportViewerTemplate.html',
        reportSource: {
            report: JSON.stringify(json),
            parameters: {
                CultureID: "en"
            }
        }
    });

然后在服务器端:

public ReportSource CustomReportResolver(string report)
{
    JToken token = JObject.Parse(report);

    int rows = (int)token["rows"];
    string reportType = (string)token["reportType"];
}

然后在数据源对象上,您可以像添加任何其他参数一样添加rows参数。