簡體   English   中英

angular ng-multiselect-dropdown 獲取 id

[英]angular ng-multiselect-dropdown get id

我正在使用 ng-multiselect 從一個工作正常的 api 中選擇項目,但是當我點擊提交時,我得到一個看起來像這樣的數組 0: {item_id: 111, item_text: "some text"} 1: {item_id: 222 , item_text: "一些 text2"}

我怎樣才能在像這樣 item_id 的字符串中獲取 item_id 的值:111,222

export class ExampleListComponent implements OnInit, ControlValueAccessor {
  @Input()
  get value(): any { return this._value; }
  set value(value: any) { this.writeValue(value); }
  private _value: any = null;

  public loading = false;
  public dataSource = new MatTableDataSource<ExampleListItem>();
  dropdownSettings: IDropdownSettings;
  dropdownList = [];
  selectedItems = [];
  @Output() valueChange = new EventEmitter<any>();

  onChange: Function = () => {};
  onTouched: Function = () => {};


  constructor(
    private exListService: exListService,
  ) { }



  ngOnInit() {
    this.getData();

    this.dropdownSettings = {
      singleSelection: false,
      idField: 'item_id',
      textField: 'item_text',
      selectAllText: 'Select All',
      unSelectAllText: 'UnSelect All',
      itemsShowLimit: 3,
      allowSearchFilter: true
    };
  }

registerOnChange(onChange: Function) {
    this.onChange = onChange;
}

registerOnTouched(onTouched: Function) {
    this.onTouched = onTouched();
  }

  writeValue(value: any) {
    if (value !== this._value) {
      this._value = value;

      this.onChange(value);
      this.onTouched();
      this.valueChange.emit(value);
    }
  }

更改這些行:

   this.onChange(value);
   this.valueChange.emit(value);

到:

   const valueToOutput = (value ?? []).map(valueObject => valueObject.item_id).join(',')

   this.onChange(valueToOutput);
   this.valueChange.emit(valueToOutput);

請注意,如果value未定義、null 或空數組,這將為您提供一個空字符串""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM