Forum Discussion

teroman's avatar
teroman
Frequent Visitor
8 years ago
Solved

Embedded Reports error if multiple objects are embedded on a page

Hi all,   I'm trying to embed multiple reports in a single web page. When I do so the "dataSelected" and "rendered" events do not work. If I embed a single report then all is well.   If I add an ...
  • teroman's avatar
    teroman
    8 years ago

    Hi Eric,

     

    Thanks for taking a look at this.

     

    I tried to repro and of course my reduced example worked just fine! Got stuck into the debugger and found it was because my divs didn't have id attributes. When embedding Power BI uses the id to decide if the embed is going into the same element.

     

    I can work around for now by assigning a random id to the div, I don't know if you'd want to fix this or at least call out the requirement in the docs. There the examples have an id, but don't state it is mandatory.

     

    Function where the problem occurs: (powerbi-client v2.4.2)

     

      addOrOverwriteEmbed(component: embed.Embed, element: HTMLElement): void {
        // remove embeds over the same div element.
        this.embeds = this.embeds.filter(function(embed) {
          return embed.element.id !== element.id;
        });
    
        this.embeds.push(component);
      }

     

    Full Example:

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="x-ua-compatible" content="IE=Edge">
        <script src="../Js/promise.min.js"></script>
        <!--PM> Install-Package Microsoft.PowerBI.JavaScript-->
        <script src="../scripts/powerbi.min.js"></script>
        <script>
            function loadPowerBIReport(embedUrl, accessToken, div){
    
                var models = window['powerbi-client'].models;
    
                var view = models.ViewMode.View;
                var display = models.DisplayOption.FitToPage;
                var filterPane = false;
                var navPane = false;
                var tokenType = models.TokenType.Aad;
                var type = "report";
    
                var config = {
                    view: view,
                    displayOption: display,
                    type: type,
                    tokenType: tokenType,
                    accessToken: accessToken,
                    embedUrl: embedUrl,
                    settings:{
                        filterPaneEnabled:filterPane,
                        navContentPaneEnabled:navPane
                    }
                };
    
                report = powerbi.embed(div, config);
    
                //report.on("filtersApplied", function(){ });
                report.on("dataSelected", function(e){
                    alert("dataSelected!");
                });
    
                report.on("rendered", function(event){
                    alert("rendered!");
                });
            }
            
            window.onload = function(){
                var accessToken = "my token";
                var report1 = "https://app.powerbi.com/reportEmbed?reportId=some guid";
                var report2 = "https://app.powerbi.com/reportEmbed?reportId=another guid";
                var divs = document.body.querySelectorAll("div")
                var div1 = divs[0];
                var div2 = divs[1];
                loadPowerBIReport(report1, accessToken, div1);
                loadPowerBIReport(report2, accessToken, div2);
            }
        </script>
    </head>
    
    <body>
        <div style="height:500px;width:500px"></div>
        <div style="height:500px;width:500px"></div>
    </body>
    
    </html>