Forum Discussion
Save toggle state in custom Power BI visual without using data fields
- 1 year ago
Hi DavidAnthony,
Thank you for Using Microsoft Fabric Community Forum.
To save the toggle state (on/off) in your custom Power BI visual using bookmarks without relying on data fields, you can use persistProperties and enumerateObjectInstances in your visual’s source code.- Use the visual settings (object model) to store the toggle state.
this.host.persistProperties({
merge: [{
objectName: "toggleSettings",
properties: {
isToggledOn: this.toggleState
},
selector: null
}]
}); - Define object enumeration for the state:
public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
const instances: VisualObjectInstance[] = [];
if (options.objectName === "toggleSettings") {
instances.push({
objectName: "toggleSettings",
properties: {
isToggledOn: this.toggleState
},
selector: null
});
}
return instances;
} - Declare toggle settings in capabilities.json:
"objects": {
"toggleSettings": {
"displayName": "Toggle Settings",
"properties": {
"isToggledOn": {
"type": { "bool": true }
}
}
}
} - Restore toggle state in update method:
this.toggleState = options.properties?.toggleSettings?.toggleState ?? false;
When the visual’s toggle is switched, the state is persisted in the visual’s object model. When a bookmark is created, Power BI captures this state. Restoring the bookmark restores the toggle’s UI state.
I hope this helps you build intuitive and bookmark-friendly toggles in your Power BI custom visuals!
This link explains how to use the persistProperties method to save and restore visual settings, which is essential for maintaining UI state across sessions and bookmarks.
Visual API for Power BI Visuals - Power BI | Microsoft Learn
This link says how custom visuals can support bookmarks by correctly saving and retrieving the necessary information when required.
Add bookmark support for Power BI custom visuals - Power BI | Microsoft LearnBelow are some resolved issues related to the use of persistProperties that you may refer to, they might help:
Solved: Power BI Custom Visual state persistence - Microsoft Fabric Community
Solved: persistProperties and stored data persistency - Microsoft Fabric Community
I hope my suggestions provided valuable insights. If you have any further questions, don’t hesitate to ask in a follow-up message.
If this post helped, please mark it as "Accept as Solution" so others can benefit as well.Best regards,
SahasraCommunity Support Team.
- Use the visual settings (object model) to store the toggle state.
Hi DavidAnthony,
Thank you for Using Microsoft Fabric Community Forum.
To save the toggle state (on/off) in your custom Power BI visual using bookmarks without relying on data fields, you can use persistProperties and enumerateObjectInstances in your visual’s source code.
- Use the visual settings (object model) to store the toggle state.
this.host.persistProperties({
merge: [{
objectName: "toggleSettings",
properties: {
isToggledOn: this.toggleState
},
selector: null
}]
}); - Define object enumeration for the state:
public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
const instances: VisualObjectInstance[] = [];
if (options.objectName === "toggleSettings") {
instances.push({
objectName: "toggleSettings",
properties: {
isToggledOn: this.toggleState
},
selector: null
});
}
return instances;
} - Declare toggle settings in capabilities.json:
"objects": {
"toggleSettings": {
"displayName": "Toggle Settings",
"properties": {
"isToggledOn": {
"type": { "bool": true }
}
}
}
} - Restore toggle state in update method:
this.toggleState = options.properties?.toggleSettings?.toggleState ?? false;
When the visual’s toggle is switched, the state is persisted in the visual’s object model. When a bookmark is created, Power BI captures this state. Restoring the bookmark restores the toggle’s UI state.
I hope this helps you build intuitive and bookmark-friendly toggles in your Power BI custom visuals!
This link explains how to use the persistProperties method to save and restore visual settings, which is essential for maintaining UI state across sessions and bookmarks.
Visual API for Power BI Visuals - Power BI | Microsoft Learn
This link says how custom visuals can support bookmarks by correctly saving and retrieving the necessary information when required.
Add bookmark support for Power BI custom visuals - Power BI | Microsoft Learn
Below are some resolved issues related to the use of persistProperties that you may refer to, they might help:
Solved: Power BI Custom Visual state persistence - Microsoft Fabric Community
Solved: persistProperties and stored data persistency - Microsoft Fabric Community
I hope my suggestions provided valuable insights. If you have any further questions, don’t hesitate to ask in a follow-up message.
If this post helped, please mark it as "Accept as Solution" so others can benefit as well.
Best regards,
Sahasra
Community Support Team.
- v-sgandrathi1 year agoCommunity Support
Hi DavidAnthony,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Thank oyu for using Microsoft Fabric Community Forum.
- v-sgandrathi1 year agoCommunity Support
Hi DavidAnthony,
We wanted to follow up since we haven't heard back from you regarding our last response. We hope your issue has been resolved.
If my answer resolved your query, please mark it as "Accept Answer" and give Kudos if it was helpful.
If you need any further assistance, feel free to reach out.
Thank you for being a valued member of the Microsoft Fabric Community Forum!
- DavidAnthony1 year agoHelper II
Thanks for your reply
- v-sgandrathi1 year agoCommunity Support
Hi DavidAnthony,
Since we haven't heard back from you yet, I'd like to confirm if you've successfully resolved this issue or if you need further help?
If you've already resolved the issue, you can mark the helpful reply as a "solution" so others know that the question has been answered and help other people in the community. Thank you again for your cooperation!
If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.Thank you.