Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docs for static query parameters in options #1045

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 122 additions & 92 deletions content/app/development/data/options/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to configure Options / Code lists for an app?
toc: true
weight: 300
---

Altinn offers two different ways an application can use code lists - static and dynamic. Both is primarily exposed through the options api from application, and are available at `{org}/{app}/api/options/{optionsId}`.
Checkbox, Dropdown, and RadioButton components will automatically be able to fetch such lists if you connect the component to the option id in question. Not all dynamic codelist have to be fetched from the options api - we can also have codelists based on the values from a repeating structure in the datamodel.

Expand All @@ -15,33 +15,61 @@ This is done by adding the optionId you would like to refer to either through th

```json
{
"id": "8e6f7b2f-fcf0-438d-8336-c1a8e1e03f44",
"type": "Dropdown",
"textResourceBindings": {},
"dataModelBindings": {},
"optionsId": "countries",
"id": "some-dropdown-component",
"type": "Dropdown",
"textResourceBindings": {},
"dataModelBindings": {},
"optionsId": "countries"
}
```

## Pass query parameters when fetching options

Options supports query parameters when making the api call. `language` is added automatically, and you can also add custom parameters by defining `mapping` on the component.
Options supports query parameters when making the api call, the parameter `language` is added automatically.

### Pass static query parameters

You can add static query parameters by setting the `queryParameters` property on the component:

```json
{
"id": "c66d7b69-2e18-4786-af44-1fa913853618",
"type": "Dropdown",
"textResourceBindings": {
"title": "NyGarantiLoyvetype"
},
"dataModelBindings": {
"simpleBinding": "soknad.nyGaranti.loyvetype"
},
"required": true,
"optionsId": "loyvetyper",
"mapping": {
"soknad.transportorOrgnummer": "orgnummer"
}
"id": "some-dropdown-component",
"type": "Dropdown",
"textResourceBindings": {
"title": "NyGarantiLoyvetype"
},
"dataModelBindings": {
"simpleBinding": "soknad.nyGaranti.loyvetype"
},
"required": true,
"optionsId": "loyvetyper",
"queryParameters": {
"loyvetype": "garanti"
}
},
```

In the example above the parameter `?loyvetype=garanti` will be added to the request.

### Pass dynamic query parameters based on the data model

You can add dynamic parameters by setting the `mapping` property on the component:

```json
{
"id": "some-dropdown-component",
"type": "Dropdown",
"textResourceBindings": {
"title": "NyGarantiLoyvetype"
},
"dataModelBindings": {
"simpleBinding": "soknad.nyGaranti.loyvetype"
},
"required": true,
"optionsId": "loyvetyper",
"mapping": {
"soknad.transportorOrgnummer": "orgnummer"
}
},
```

Expand All @@ -51,60 +79,63 @@ If an option is setup with mapping and the given data field changes app-frontend
Passing query parameters from repeating groups is also supported by adding an index indicator for the relevant indexes. Example for a group:

```json
{
"id": "dropdown-group",
"type": "Dropdown",
"textResourceBindings": {
"title": "Select city"
},
"dataModelBindings": {
"simpleBinding": "Group.City"
},
"required": true,
"optionsId": "cities",
"mapping": {
"Group[{0}].Country": "country"
}
},
{
"id": "dropdown-group",
"type": "Dropdown",
"textResourceBindings": {
"title": "Select city"
},
"dataModelBindings": {
"simpleBinding": "Group.City"
},
"required": true,
"optionsId": "cities",
"mapping": {
"Group[{0}].Country": "country"
}
},
```

For nested groups follows the same pattern but with an additional index indicator for the nested group:

```json
{
"id": "dropdown-nested-group",
"type": "Dropdown",
"textResourceBindings": {
"title": "Select city"
},
"dataModelBindings": {
"simpleBinding": "Group.SubGroup.City"
},
"required": true,
"optionsId": "cities",
"mapping": {
"Group[{0}].SubGroup[{1}].Country": "country"
}
},
{
"id": "dropdown-nested-group",
"type": "Dropdown",
"textResourceBindings": {
"title": "Select city"
},
"dataModelBindings": {
"simpleBinding": "Group.SubGroup.City"
},
"required": true,
"optionsId": "cities",
"mapping": {
"Group[{0}].SubGroup[{1}].Country": "country"
}
},
```

For a complete example on how this is setup see our [demo app.](https://altinn.studio/repos/ttd/dynamic-options-rep)

{{%notice warning%}}
**Applies to applications using version 7.4.0 or older of the nuget packages - https://github.com/Altinn/app-lib-dotnet/release**

**Applies to applications using version 7.4.0 or older of the nuget packages** - https://github.com/Altinn/app-lib-dotnet/release

<br>

During PDF-generation the app will try to call the same option endpoint as app-frontend does.
We currently has a weakness where mapping paramteres not are included in this request, see issue [#7903.](https://github.com/Altinn/altinn-studio/issues/7903)
We currently have a weakness where mapping parameters not are included in this request, see issue [#7903.](https://github.com/Altinn/altinn-studio/issues/7903)

A possible workaround here is to return an empty array when the PDF-generator asks for options with empty query params, example:

```c#
string someArg = keyValuePairs.GetValueOrDefault("someArg");
string someOtherArg = keyValuePairs.GetValueOrDefault("someOtherArg");
string someArg = keyValuePairs.GetValueOrDefault("someArg");
string someOtherArg = keyValuePairs.GetValueOrDefault("someOtherArg");

if (string.IsNullOrEmpty(someArg) || string.IsNullOrEmpty(someOtherArg)) {
return await Task.FromResult(new List<AppOption>());
}
if (string.IsNullOrEmpty(someArg) || string.IsNullOrEmpty(someOtherArg)) {
return await Task.FromResult(new List<AppOption>());
}
```

Notice that this wil result in the option value and not the label being present as the end users answer.
Expand All @@ -121,38 +152,37 @@ dynamic code lists. One can also use them in options based on repeating groups i

```json
[
{
"value": "norway",
"label": "Norge",
"description": "This is a description",
"helpText": "This is a help text"
},
{
"value": "denmark",
"label": "Danmark"
}
{
"value": "norway",
"label": "Norge",
"description": "This is a description",
"helpText": "This is a help text"
},
{
"value": "denmark",
"label": "Danmark"
}
]

```

```cs
var options = new AppOptions
{
Options = new List<AppOption>
{
new AppOption
{
new AppOption
{
Label = "Ole",
Value = "1",
Description = "This is a description",
HelpText = "This is a help text"
},
new AppOption
{
Label = "Dole",
Value = "2"
}
Label = "Ole",
Value = "1",
Description = "This is a description",
HelpText = "This is a help text"
},
new AppOption
{
Label = "Dole",
Value = "2"
}
}
};
```

Expand All @@ -161,18 +191,18 @@ same way as labels, described in
[options based on repeating groups](dynamic-codelists).

```json
{
"id": "checkboxes-component-id",
"type": "Checkboxes",
...
"source": {
"group": "some.group",
"label": "checkboxes.label",
"description": "checkboxes.descripiton",
"helpText": "checkboxes.helpText",
"value": "some.group[{0}].someField"
}
},
{
"id": "checkboxes-component-id",
"type": "Checkboxes",
...
"source": {
"group": "some.group",
"label": "checkboxes.label",
"description": "checkboxes.descripiton",
"helpText": "checkboxes.helpText",
"value": "some.group[{0}].someField"
}
},
```

{{<children />}}
Loading