在 Power BI 工作区集合中在报表的查看和编辑模式之间切换Toggle between view and edit mode for reports in Power BI Workspace Collections

了解在 Power BI 工作区集合中如何在报表的查看和编辑模式之间切换。Learn how to toggle between view and edit mode for your reports within Power BI Workspace Collections.

重要

Power BI 工作区集合已弃用,到 2018 年 6 月 或合同指示时可用。Power BI Workspace Collections is deprecated and is available until June 2018 or when your contract indicates. 建议你规划到 Power BI Embedded 的迁移以避免应用程序中断。You are encouraged to plan your migration to Power BI Embedded to avoid interruption in your application. 有关如何将数据迁移到 Power BI Embedded 的信息,请参阅如何将 Power BI 工作区集合内容迁移到 Power BI EmbeddedFor information on how to migrate your data to Power BI Embedded, see How to migrate Power BI Workspace Collections content to Power BI Embedded.

创建访问令牌Creating an access token

需要创建一个访问令牌,令牌可授予你查看和编辑报表的能力。You need to create an access token that gives you the ability to both view and edit a report. 要编辑并保存报表,需要 Report.ReadWrite 令牌权限。To edit and save a report, you need the Report.ReadWrite token permission. 有关详细信息,请参阅在 Power BI 工作区集合中进行身份验证和授权For more information, see Authenticating and authorizing in Power BI Workspace Collections.

备注

这可以让你编辑现有报表并保存更改。This allows you to edit and save changes to an existing report. 如果还需要支持“另存为”的功能,则需要提供额外的权限。If you would also like the function of supporting Save As, you need to supply additional permissions. 有关详细信息,请参阅范围For more information, see Scopes.

using Microsoft.PowerBI.Security;

// rlsUsername and roles are optional
string scopes = "Report.ReadWrite";
PowerBIToken embedToken = PowerBIToken.CreateReportEmbedTokenForCreation(workspaceCollectionName, workspaceId, datasetId, null, null, scopes);

var token = embedToken.Generate("{access key}");

嵌入配置Embed configuration

为了在编辑模式下看到“保存”按钮,需要提供权限和 viewMode。You need to supply permissions and a viewMode in order to see the save button when in edit mode. 有关详细信息,请参阅 Embed 配置详细信息For more information, see Embed configuration details.

例如,在 JavaScript 中:For example, in JavaScript:

   <div id="reportContainer"></div>

    <script>
    // Get models. Models, it contains enums that can be used.
    var models = window['powerbi-client'].models;

    // Embed configuration used to describe the what and how to embed.
    // This object is used when calling powerbi.embed.
    // This also includes settings and options such as filters.
    // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
    var config= {
        type: 'report',
        accessToken: 'eyJ0eXAiO...Qron7qYpY9MI',
        embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed',
        id:  '5dac7a4a-4452-46b3-99f6-a25915e0fe55',
        permissions: models.Permissions.ReadWrite /*both save & save as buttons will be visible*/,
        viewMode: models.ViewMode.View,
        settings: {
            filterPaneEnabled: true,
            navContentPaneEnabled: true
        }
    };

    // Get a reference to the embedded report HTML element
    var reportContainer = $('#reportContainer')[0];

    // Embed the report and display it within the div container.
    var report = powerbi.embed(reportContainer, config);
    </script>    

这表示会根据设置为 models.ViewMode.View 的 viewMode 在查看模式下嵌入报表。This indicates to embed the report in view mode based on viewMode being set to models.ViewMode.View.

查看模式View mode

如果处于编辑模式下,可以使用以下 JavaScript 切换到查看模式。You can use the following JavaScript to switch into view mode, if you are in edit mode.

// Get a reference to the embedded report HTML element
var reportContainer = $('#reportContainer')[0];

// Get a reference to the embedded report.
report = powerbi.get(reportContainer);

// Switch to view mode.
report.switchMode("view");

编辑模式Edit mode

如果处于查看模式下,可以使用以下 JavaScript 切换到编辑模式。You can use the following JavaScript to switch into edit mode, if you are in view mode.

// Get a reference to the embedded report HTML element
var reportContainer = $('#reportContainer')[0];

// Get a reference to the embedded report.
report = powerbi.get(reportContainer);

// Switch to edit mode.
report.switchMode("edit");

另请参阅See also

示例入门Get started with sample
嵌入报表Embed a report
在 Power BI 工作区集合中进行身份验证和授权Authenticating and authorizing in Power BI Workspace Collections
CreateReportEmbedTokenCreateReportEmbedToken
JavaScript 嵌入示例JavaScript Embed Sample
PowerBI-CSharp Git 存储库PowerBI-CSharp Git Repo
PowerBI-Node Git 存储库PowerBI-Node Git Repo

有更多问题?More questions? 试用 Power BI 社区Try the Power BI Community