设置ABP框架中Angular UI动态表单的下拉菜单默认值

huangapple go评论99阅读模式
英文:

Setting Default Value for Dropdown in ABP Framework with Angular UI Dynamic Forms

问题

I'm working on ABP framework with Angular UI, and I'm currently working dynamic forms, so I have a dropdown as:

我正在使用ABP框架和Angular UI工作,目前正在处理动态表单,所以我有一个下拉框如下:

const timeZoneProp = new FormProp({
type: ePropType.Enum,
name: 'TimeZoneId',
displayName: '::TimeZone',
isExtra: true,
id: 'TimeZoneId',
autocomplete: 'off',
validators: () => [Validators.required],
options: (data: PropData): Observable<Option[]> => {
const service = data.getInjected(TimeZoneService);
return service
.getList()
.pipe(
map(
response =>
response.items?.map(
item =>
({ key: item.description, value: item.id } as Option)
) || []
)
);
}
});

propList.addByIndex(timeZoneProp, 6);

This is working as expected; I have a dropdown with data displayed, and now I want to auto-select a value on the dropdown load, so I noticed there is a defaultValue option in the documentation:

这正如预期的那样工作;我有一个下拉框显示数据,现在我想在加载下拉框时自动选择一个值,所以我注意到文档中有一个defaultValue选项:

  • defaultValue是字段将具有的初始值。 (默认值:null)

That default accepts multiple types of values, it is declared as:

该默认值接受多种类型的值,声明如下:

readonly defaultValue: boolean | number | string | Date;

So I tried to set it via index like defaultValue: 0 or via the key value like defaultValue: '(UTC+00:00) Coordinated Universal Time ETC/GMT', but it does not work, it still showing the options but is not selecting an option on load. So I try something different, I try to set it inside the options definition as:

因此,我尝试通过索引设置它,例如 defaultValue: 0 或通过键值设置它,例如 defaultValue: '(UTC+00:00) Coordinated Universal Time ETC/GMT',但它不起作用,它仍然显示选项,但在加载时不选择任何选项。因此,我尝试了一些不同的方法,我尝试在options定义内设置它:

const timeZoneProp = new FormProp({
type: ePropType.Enum,
name: 'TimeZoneId',
displayName: '::TimeZone',
isExtra: true,
id: 'TimeZoneId',
autocomplete: 'off',
validators: () => [Validators.required],
options: data => {
const service = data.getInjected(TimeZoneService);
return service.getList().pipe(
map(response => {
const options = response.items?.map(item => ({
key: item.description,
value: item.id
})) || [];

  1. // Find the item where the key equals "(UTC+00:00) Coordinated Universal Time ETC/GMT"
  2. const defaultValueItem = options.find(
  3. item => item.key === '(UTC+00:00) Coordinated Universal Time ETC/GMT'
  4. );
  5. // If the defaultValueItem is found, assign its value to defaultValue
  6. if (defaultValueItem) {
  7. timeZoneProp.defaultValue = defaultValueItem.value;
  8. }
  9. return options;
  10. })
  11. );

}
});

As you can see I'm trying to set in this line:

如您所见,我尝试在这一行中设置:

timeZoneProp.defaultValue = defaultValueItem.value;

But the property is read-only, so it can not be overridden in the options definition.

但是属性是只读的,因此不能在选项定义中重写它。

How can I achieve this?

我该如何实现这个?

UPDATE

I think the default value is not displayed because the options are being loaded asynchronously, and the default value is set before the options are available, how can I do a workaround to achieve this commitment?

我认为默认值没有显示是因为选项是异步加载的,并且在选项可用之前设置了默认值,如何解决这个问题以实现这一承诺?

I try to set defaultValue as a promise like:

我尝试将defaultValue设置为Promise,如下:

const timeZoneProp = new FormProp({
type: ePropType.Enum,
name: 'TimeZoneId',
displayName: '::TimeZone',
isExtra: true,
id: 'TimeZoneId',
autocomplete: 'off',
defaultValue: new Promise(resolve => {
const data = {};
const optionsPromise = this.options(data).toPromise();

  1. optionsPromise.then(options => {
  2. if (options.length > 0) {
  3. resolve(options[0].value);
  4. } else {
  5. resolve(null); // Set default value to null if options array is empty
  6. }
  7. });
  8. }).then(
  9. resolvedValue =>
  10. resolvedValue as string | number | boolean | Date | undefined
  11. ),
  12. validators: () => [Validators.required],
  13. options: (data: PropData<IdentityUserDto>): Observable<Option<any>[]> => {
  14. const service = data.getInjected(TimeZoneService);
  15. return service
  16. .getList()
  17. .pipe(
  18. map(
  19. response =>
  20. response.items?.map(
  21. item =>
  22. ({ key: item.description, value: item.id } as Option<any>)
  23. ) || []
  24. )
  25. );
  26. }

});

But this throws an error in defaultValue prop:

但是这在defaultValue属性中引发了错误:

Type 'Promise<string | number | boolean | Date | undefined>' is not assignable to type 'string | number | boolean | Date | undefined'.

类型 'Promise<string | number | boolean | Date | undefined>' 无法分配给类型 'string | number | boolean | Date | undefined'。

const optionsPromise = this.options(data).toPromise();

it throws:

它引发了以下错误:

this' implicitly has type 'any' because it does not have a type annotation.

因为它没有类型注释,所以“this”隐式具有类型“any”。

英文:

I'm working on ABP framework with Angular UI, and I'm currently working dynamic forms, so I have a dropdown as:

  1. const timeZoneProp = new FormProp&lt;IdentityUserDto&gt;({
  2. type: ePropType.Enum,
  3. name: &#39;TimeZoneId&#39;,
  4. displayName: &#39;::TimeZone&#39;,
  5. isExtra: true,
  6. id: &#39;TimeZoneId&#39;,
  7. autocomplete: &#39;off&#39;,
  8. validators: () =&gt; [Validators.required],
  9. options: (data: PropData&lt;IdentityUserDto&gt;): Observable&lt;Option&lt;any&gt;[]&gt; =&gt; {
  10. const service = data.getInjected(TimeZoneService);
  11. return service
  12. .getList()
  13. .pipe(
  14. map(
  15. response =&gt;
  16. response.items?.map(
  17. item =&gt;
  18. ({ key: item.description, value: item.id } as Option&lt;any&gt;)
  19. ) || []
  20. )
  21. );
  22. }
  23. });
  24. propList.addByIndex(timeZoneProp, 6);

This is working as expected; I have a dropdown with data displayed, and now I want to auto-select a value on the dropdown load, so I noticed there is a defaultValue option in the documentation:

  • defaultValue is the initial value the field will have. (default: null)

That default accept multiple type of values, it is declared as:

  1. readonly defaultValue: boolean | number | string | Date;

So I tried to set it via index like defaultValue: 0 or via the key value like defaultValue: &#39;(UTC+00:00) Coordinated Universal Time ETC/GMT&#39;, but it does not work, it still showing the options but is not selecting an option on load. So I try something different, I try to set it inside de options definition as:

  1. const timeZoneProp = new FormProp&lt;IdentityUserDto&gt;({
  2. type: ePropType.Enum,
  3. name: &#39;TimeZoneId&#39;,
  4. displayName: &#39;::TimeZone&#39;,
  5. isExtra: true,
  6. id: &#39;TimeZoneId&#39;,
  7. autocomplete: &#39;off&#39;,
  8. validators: () =&gt; [Validators.required],
  9. options: data =&gt; {
  10. const service = data.getInjected(TimeZoneService);
  11. return service.getList().pipe(
  12. map(response =&gt; {
  13. const options = response.items?.map(item =&gt; ({
  14. key: item.description,
  15. value: item.id
  16. })) || [];
  17. // Find the item where the key equals &quot;(UTC+00:00) Coordinated Universal Time ETC/GMT&quot;
  18. const defaultValueItem = options.find(
  19. item =&gt; item.key === &#39;(UTC+00:00) Coordinated Universal Time ETC/GMT&#39;
  20. );
  21. // If the defaultValueItem is found, assign its value to defaultValue
  22. if (defaultValueItem) {
  23. timeZoneProp.defaultValue = defaultValueItem.value;
  24. }
  25. return options;
  26. })
  27. );
  28. }
  29. });

As you can see I'm trying to set in this line:

  1. timeZoneProp.defaultValue = defaultValueItem.value;

But the property is read-only, so it can not be overridden in the options definition.

How can I achieve this?

UPDATE

I think the default value is not displayed because the options are being loaded asynchronously, and the default value is set before the options are available, how can I do a workaround to achieve this commitment?

I try to set defaultValue as a promise like:

  1. const timeZoneProp = new FormProp&lt;IdentityUserDto&gt;({
  2. type: ePropType.Enum,
  3. name: &#39;TimeZoneId&#39;,
  4. displayName: &#39;::TimeZone&#39;,
  5. isExtra: true,
  6. id: &#39;TimeZoneId&#39;,
  7. autocomplete: &#39;off&#39;,
  8. defaultValue: new Promise(resolve =&gt; {
  9. const data = {};
  10. const optionsPromise = this.options(data).toPromise();
  11. optionsPromise.then(options =&gt; {
  12. if (options.length &gt; 0) {
  13. resolve(options[0].value);
  14. } else {
  15. resolve(null); // Set default value to null if options array is empty
  16. }
  17. });
  18. }).then(
  19. resolvedValue =&gt;
  20. resolvedValue as string | number | boolean | Date | undefined
  21. ), // Cast the resolved value to the appropriate type
  22. validators: () =&gt; [Validators.required],
  23. options: (data: PropData&lt;IdentityUserDto&gt;): Observable&lt;Option&lt;any&gt;[]&gt; =&gt; {
  24. const service = data.getInjected(TimeZoneService);
  25. return service
  26. .getList()
  27. .pipe(
  28. map(
  29. response =&gt;
  30. response.items?.map(
  31. item =&gt;
  32. ({ key: item.description, value: item.id } as Option&lt;any&gt;)
  33. ) || []
  34. )
  35. );
  36. }
  37. });

But this throw error in defaultValue prop

> Type 'Promise<string | number | boolean | Date | undefined>' is not
> assignable to type 'string | number | boolean | Date |
> undefined'.ts(2322) form-props.d.ts(36, 14): The expected type comes
> from property 'defaultValue' which is declared here on type '{
> validators?: PropCallback<IdentityUserDto, ValidatorFn[]> | undefined;
> asyncValidators?: PropCallback<IdentityUserDto, AsyncValidatorFn[]> |
> undefined; ... 15 more ...; name: string; }' (property) defaultValue?:
> string | number | boolean | Date | undefined

  1. const optionsPromise = this.options(data).toPromise();

it throws:

> this' implicitly has type 'any' because it does not have a type
> annotation.

答案1

得分: 1

默认值必须是一个值。这是一个承诺。默认值不支持动态值(暂时不支持)

你是否尝试将(UTC+00:00) 协调世界时 ETC/GMT作为默认值?

defaultValue: ''(UTC+00:00) 协调世界时 ETC/GMT'' 并将键和值设置为相同的值?也许在后端将键转换为值?

英文:

The default value must be a value. It is a promise. The default value is not supported the dynamic value (for a now)

Did you try to (UTC+00:00) Coordinated Universal Time ETC/GMT as defualt value.

defaultValue:'(UTC+00:00) Coordinated Universal Time ETC/GMT' and set key and value with same value ? maybe convert key to value in backend ?

huangapple
  • 本文由 发表于 2023年5月25日 09:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定