Skip to content

Commit 3698a62

Browse files
Added anchors so this example can be included on devsite. (#9)
1 parent b99ea2a commit 3698a62

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

stepped-config/src/dynamic.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// [start get_config_branching_paths]
2+
var cc = DataStudioApp.createCommunityConnector();
3+
4+
function getConfig(request) {
5+
var configParams = request.configParams;
6+
// For the initial getConfig request, request.configParams will be undefined.
7+
// For subsequent requests, it'll include the configuration so far.
8+
var isFirstRequest = configParams === undefined;
9+
var config = cc.getConfig();
10+
11+
config
12+
.newSelectSingle()
13+
.setId('country')
14+
.setName('Country')
15+
// Set isDynamic to true so any changes to Country will clear the state
16+
// selections.
17+
.setIsDynamic(true)
18+
.addOption(
19+
config
20+
.newOptionBuilder()
21+
.setLabel('United States')
22+
.setValue('USA')
23+
)
24+
.addOption(
25+
config
26+
.newOptionBuilder()
27+
.setLabel('Canada')
28+
.setValue('CA')
29+
);
30+
31+
if (isFirstRequest) {
32+
// Tell DS that this is a stepped config request. This will make the 'NEXT'
33+
// button show.
34+
config.setIsSteppedConfig(true);
35+
} else {
36+
// validate a valid value was selected for configParams.country
37+
if (configParams.country === undefined) {
38+
cc.newUserError()
39+
.setText('You must choose a country.')
40+
.throwException();
41+
}
42+
switch (configParams.country) {
43+
case 'USA': {
44+
config
45+
.newSelectSingle()
46+
.setId('state')
47+
.setName('State')
48+
.addOption(
49+
config
50+
.newOptionBuilder()
51+
.setLabel('New York')
52+
.setValue('NY')
53+
)
54+
.addOption(
55+
config
56+
.newOptionBuilder()
57+
.setLabel('Calfornia')
58+
.setValue('CA')
59+
);
60+
break;
61+
}
62+
case 'CA': {
63+
// No additional configuration is needed for Canada.
64+
break;
65+
}
66+
default: {
67+
cc.newUserError()
68+
.setText('You must either select "CA" or "USA"')
69+
.throwException();
70+
}
71+
}
72+
}
73+
return config.build();
74+
}
75+
// [end get_config_branching_paths]

stepped-config/src/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var cc = DataStudioApp.createCommunityConnector();
2-
31
// https://developers.google.com/datastudio/connector/reference#isadminuser
42
function isAdminUser() {
53
return false;
64
}
75

6+
// [start get_config_dynamic_dropdowns]
7+
var cc = DataStudioApp.createCommunityConnector();
8+
89
function optionsForState(state) {
910
switch (state) {
1011
case 'IL': {
@@ -83,6 +84,7 @@ function getConfig(request) {
8384
}
8485
return config.build();
8586
}
87+
// [end get_config_dynamic_dropdowns]
8688

8789
function getFields() {
8890
var fields = cc.getFields();

0 commit comments

Comments
 (0)