简体   繁体   English

如何以角度7动态形式填充从属下拉列表?

[英]How to populate dependent dropdowns in angular 7 dynamic forms?

I have a dynamic form that has more than one dropdown and one of dropdown dependent another. 我有一个动态表单,该表单具有多个下拉列表,并且其中一个依赖于另一个下拉列表。 The problem is that I couldn't populate the dependent dropdown. 问题是我无法填充从属下拉列表。

jobDepartmentDropdownOnChange(e: any) {
    this.personnelRecruitmentInfoService.getAllByJobDepartmentId(e.value).subscribe(
      res => {
        if (res.code === ResponseCode.success) {
          res.dataList.map(item => {
            this.personnels.push({
              value: item.personnel.id.toString(),
              label: item.personnel.dataValue
            });
          });

          if (this.formComponent !== undefined) {
            this.formComponent.form.controls['personnelId'].patchValue(this.personnels);
          }
        }
      }
    );
  }
const personnelDropdown = new InputDropdown({
  key: 'personnelId',
  label: '',
  options: this.personnels,
  value: '',
  disabled: true,
  required: false,
  multiple: true,
  order: 20
});
public personnels: any[] = [];
const jobDepartmentDropdown = new InputDropdown({
  key: 'jobDepartmentId',
  label: '',
  options: this.jobDepartments,
  value: '',
  onChange: this.jobDepartmentDropdownOnChange.bind(this),
  disabled: true,
  required: false,
  multiple: true,
  order: 19
});
export class InputDropdown extends InputBase<string> {
  controlType = 'dropdown';
  options: { value: string, label: string }[] = [];
  showSelect = false;
  multiple = false;

  constructor(options: {} = {}) {
    super(options);
    this.options = options['options'] || [];
    this.showSelect = options['showSelect'] || false;
    this.multiple = options['multiple'] || false;
  }
}

How can I populate personnelId dropdown according to the previous dropdown? 如何根据上一个下拉列表填充personnelId ID下拉列表?

You would be way better of using a subject for your personnels over triggering angulars change detection cycle way too often performance wise. 相对于触发角度变化检测周期的方式(通常是性能明智的方法),最好是为您的人员使用一个主题。 But if you simply wonder why your solution doesn't work regardless of performance, you're pushing elements in your personnels object not actually changing the reference held by this.personnels. 但是,如果您只是想知道为什么解决方案无论性能如何都不起作用,那么您在推动人员对象中的元素实际上并未更改this.personnels持有的引用。 This means the angular change detection won't be triggered. 这意味着不会触发角度变化检测。 If you do want to trigger the change detection, alter your "push" like this: 如果您确实要触发更改检测,请按以下方式更改“推送”:

jobDepartmentDropdownOnChange(e: any) {
    this.personnelRecruitmentInfoService.getAllByJobDepartmentId(e.value).subscribe(
      res => {
        if (res.code === ResponseCode.success) {
          res.dataList.map(item => {
            // Use the spread operator to create a new array hence 
            // updating the reference in the object angular detects changes upon.
            this.personnels = [...this.personnels, {
              value: item.personnel.id.toString(),
              label: item.personnel.dataValue
            }];
          });

          if (this.formComponent !== undefined) {
            this.formComponent.form.controls['personnelId'].patchValue(this.personnels);
          }
        }
      }
    );
  }

I found solution: 我找到了解决方案:

const jobDepartmentDropdown = new InputDropdown({
  key: 'jobDepartmentId',
  label: '',
  options: this.jobDepartments,
  value: '',
  onChange: this.jobDepartmentDropdownOnChange.bind(this),
  disabled: true,
  required: false,
  multiple: true,
  order: 19
});
((this.inputs as InputBase<any>[]).find(i => i.key === 'personnelId') as InputDropdown).options = options;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM