Forum Discussion
Dynamic Dropdown Properties in Power BI Custom Visuals
Hi NileshBhayani,
I see you JSON code has enumeration: [] wich means no value unless you manually add them (thats why dropdown is blank).
So How to fix it?
1-You cannot make dynamic dropdowns only using capabilities.json
So instead of
"enumeration": []
you should do something like that:
patternSlice.items = [
{ value: "dots", displayName: "Dots" },
{ value: "stripes", displayName: "Stripes" }
];
Cause capabilities.json is only supports static values
So in Final You should:
1- Import the formatting helper
import { formattingSettings } from "powerbi-visuals-utils-formattingmodel";
2- Defining your setting card
export class PatternSettings extends formattingSettings.Card {
name = "fillPatterns";
displayName = "Fill Patterns";
3-Making a dropdown
patternSlice = new formattingSettings.SimpleSlice<string>({
name: "patternTpe",
displayName: "Pattern Type",
type: formattingSettings.ValueType.enumeration,
value: "none"
});
4-Grouping the slice
groups = [
new formattingSettings.Group({
name: "patterns",
displayName: "Patterns",
slices: [this.patternSlice],
}),
];
}
So the full PatternSettings class should be like that:
import { formattingSettings } from "powerbi-visuals-utils-formattingmodel";
export class PatternSettings extends formattingSettings.Card {
name = "fillPatterns";
displayName = "Fill Patterns";
patternSlice = new formattingSettings.SimpleSlice<string>({
name: "patternType",
displayName: "Pattern Type",
type: formattingSettings.ValueType.enumeration,
value: "none"
});
groups = [
new formattingSettings.Group({
name: "patterns",
displayName: "Patterns",
slices: [this.patternSlice],
}),
];
}
And finally the Visual.ts add wherever you build formatting Like :
public getFormattingModel(): powerbi.visuals.FormattingModel {
// This is just an example
const patternList = [
{ id: "dots", name: "Dots" },
{ id: "stripes", name: "Stripes" },
{ id: "grid", name: "Grid" }
];
this.patternSettings.patternSlice.items = patternList.map(p => ({
value: p.id,
displayName: p.name
}));
return this.formattingSettingsService.buildFormattingModel(this.patternSettings);
}
In that way you are telling PowerBI here is my cards and groups and slices dropitdown.
that's all, If you need any assistance just reply to me and if you find this useful tell me 🙂 .