diff --git a/content/app/development/data/options/_index.en.md b/content/app/development/data/options/_index.en.md index b6ebaa55270..90991639505 100644 --- a/content/app/development/data/options/_index.en.md +++ b/content/app/development/data/options/_index.en.md @@ -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. @@ -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" + } }, ``` @@ -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 + +
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()); - } +if (string.IsNullOrEmpty(someArg) || string.IsNullOrEmpty(someOtherArg)) { + return await Task.FromResult(new List()); +} ``` Notice that this wil result in the option value and not the label being present as the end users answer. @@ -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 + { + 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" } + } }; ``` @@ -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" + } +}, ``` {{}} diff --git a/content/app/development/data/options/_index.nb.md b/content/app/development/data/options/_index.nb.md index a41ed64c42c..eed1ff7f55d 100644 --- a/content/app/development/data/options/_index.nb.md +++ b/content/app/development/data/options/_index.nb.md @@ -15,84 +15,114 @@ Dette gjøres ved å legge til feltet optionsId som referer til hvilken option ( ```json { - "id": "8e6f7b2f-fcf0-438d-8336-c1a8e1e03f44", - "type": "Dropdown", - "componentType": 4, - "textResourceBindings": {}, - "dataModelBindings": {}, - "optionsId": "biler", + "id": "dropdown-komponent", + "type": "Dropdown", + "componentType": 4, + "textResourceBindings": {}, + "dataModelBindings": {}, + "optionsId": "biler" } ``` ## Sende med query parametere ved henting av options -Options støtter query parameters når det gjøres api kall. `language` er satt opp automatisk, men man kan også legge til egendefinerte parametere ved å sette opp `mapping` på den aktuelle komponenten. +Options støtter query parametre når det gjøres api kall; parameteren `language` sendes med automatisk. + +### Sende med statiske query parametre + +Man kan legge til statiske query parametre ved å sette opp `queryParameters` på den aktuelle komponenten: ```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": "dropdown-komponent", + "type": "Dropdown", + "textResourceBindings": { + "title": "NyGarantiLoyvetype" + }, + "dataModelBindings": { + "simpleBinding": "soknad.nyGaranti.loyvetype" + }, + "required": true, + "optionsId": "loyvetyper", + "queryParameters": { + "loyvetype": "garanti" + } +}, +``` + +I eksempelet over vil parameteren `?loyvetype=garanti` bli sendt med i api kallet. + +### Sende med dynamiske query parametre basert på datamodellen + +Man kan legge til dynamiske query parametre ved å sette opp `mapping` på den aktuelle komponenten: + +```json +{ + "id": "dropdown-komponent", + "type": "Dropdown", + "textResourceBindings": { + "title": "NyGarantiLoyvetype" + }, + "dataModelBindings": { + "simpleBinding": "soknad.nyGaranti.loyvetype" + }, + "required": true, + "optionsId": "loyvetyper", + "mapping": { + "soknad.transportorOrgnummer": "orgnummer" + } }, ``` I eksempelet over vil det bli satt på et query parameter `orgnummer={nr}`, hvor `{nr}` er verdien på feltet `soknad.transportorOrgnummer`. Om man setter opp en kobling til et datafelt og dette feltet endrer seg så vil app-frontend hente options på nytt. På denne måten kan man dynamisk styre hvilke valg som vises basert på informasjon gitt av sluttbruker. - Å sende med query parametere fra repeterende grupper er også støttet ved å legge ved en indeks-indikator for de relevante gruppene. Eksempel: +Å sende med query parametere fra repeterende grupper er også støttet ved å legge ved en indeks-indikator for de relevante gruppene. Eksempel: ```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 nøsta repeterende grupper vil man følge det samme mønsteret, men med en ekstra indikator for den nøsta gruppa: ```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 et komplett eksempel kan du se vår [demo app.](https://altinn.studio/repos/ttd/dynamic-options-rep) {{%notice warning%}} -**Gjelder applikasjoner som benytter versjon 7.4.0 eller eldre av nuget pakkene - se https://github.com/Altinn/app-lib-dotnet/release** +**Gjelder applikasjoner som benytter versjon 7.4.0 eller eldre av nuget pakkene** - se https://github.com/Altinn/app-lib-dotnet/release + +
Under PDF-generering vil appen prøve å kalle det samme options-endepunktet som app-frontend gjør. Vi har foreløpig en svakhet ved at eventuelle mapping-parametere ikke blir inkludert i denne forespørselen, se issue [#7903.](https://github.com/Altinn/altinn-studio/issues/7903) @@ -100,12 +130,12 @@ Vi har foreløpig en svakhet ved at eventuelle mapping-parametere ikke blir inkl En mulig workaround her er å returnere en tom array i det PDF-generatoren spør om options med tomme query-parametere, eksempel: ```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()); - } +if (string.IsNullOrEmpty(someArg) || string.IsNullOrEmpty(someOtherArg)) { + return await Task.FromResult(new List()); +} ``` Merk at dette vil resultere i at PDF vil vise verdien valgt og ikke label som sluttbrukers svar. @@ -122,16 +152,16 @@ dynamiske kodelister. Man kan også bruke dem i options basert på repeterende g ```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" + } ] ``` @@ -139,20 +169,20 @@ dynamiske kodelister. Man kan også bruke dem i options basert på repeterende g var options = new AppOptions { Options = new List + { + new AppOption + { + Label = "Ole", + Value = "1", + Description = "This is a description", + HelpText = "This is a help text" + }, + 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 = "Dole", + Value = "2" } + } }; ``` @@ -161,18 +191,18 @@ text-ressurser på samme måte som `label`, som er beskrevet i [options basert på repeterende grupper](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" + } +}, ``` {{}}