Typhead空值应重置另一个选择对象的选项

时间:2018-08-17 09:19:02

标签: angular ng-bootstrap typehead

我在页面上有一个标头,用于加载客户帐户的用户。 在我点击了打印头结果中的一项之后,用户会加载到下一个选择框,但在我提交之前,我先从打印头中删除了该选项,将其清除并执行onBlur,另一个选择框仍然显示错误的结果。

检测到打字头值为空后,在哪里可以删除该框的选项?

这里是HTML,下面是我认为相关的代码。

P.S。您可以想象,我不是Angular的专家

<div class="form-group">
    <label class="control-label">{{ "ASSUME_IDENTITY.LABELS.CUSTOMER" | translate }}</label>
    <div>
        <ng-template #rt let-r="result" let-t="term">
            {{ r.fullPath }}
        </ng-template>

        <input id="typeahead-format" name="nodeFullPath" type="text" mdbActive [ngClass]="{'invalid': validNode, 'valid': validNode}"
         class="form-control" #el (focus)="onFocus($event)" (selectItem)="selectItem($event)" (blur)="onBlurMethod(selectedNode)"
         [ngbTypeahead]="search" [resultTemplate]="rt" [inputFormatter]="formatter" />

    </div>
</div>
<div class="form-group">
    <label class="control-label">{{ "ASSUME_IDENTITY.LABELS.USER" | translate }}</label>
    <div>
        <select class="form-control" [(ngModel)]="selectedUser" name="selectedUser" [disabled]="!(usersLoaded | async)">
            <option *ngFor="let user of (usersForNode | async)" [ngValue]="user"> {{user.label | translate}}</option>
        </select>
    </div>
</div>
<div>
    <button type="button" [disabled]="!(usersLoaded | async)" class=" btn btn-primary pull-right" (click)="assumeIdbuttonPressed()">
        {{ "ASSUME_IDENTITY.BTN_ASSUME_IDENTITY" | translate }}</button>
</div>

.....................

setSelectedUserToFirstItem(user: HttpOption) {
    this.selectedUser = user;
}

onFocus(e: Event): void {
    e.stopPropagation();
    setTimeout(() => {
    const inputEvent: Event = new Event('input');
    e.target.dispatchEvent(inputEvent);
}, 0);
}

search = (text: Observable<string>): Observable<CustomerNodePath[]> =>
    text
        .debounceTime(100)
        .distinctUntilChanged()
        .map((term: any) => this.fullPathList.filter(v => v.fullPath.toLowerCase().indexOf(term.toLowerCase()) > -1))

formatter = (x: { fullPath: string }): string => x.fullPath;

selectItem = (event: any) => {
    this.selectedNode = event.item;
    this.store.dispatch(new GetUsersForNode(this.selectedNode.id));
}

onBlurMethod = (node: CustomerNodePath): void => {
    if (!this.selectedNode) this.dispatchNodeValidationError();
}

private dispatchNodeValidationError = (): void => {
    this.store.dispatch(new NodeValidationError());
}

1 个答案:

答案 0 :(得分:0)

这是我们拥有的解决方案:

将其放入组件中。

 onBlurMethod = ( event: any, node: CustomerNodePath): void => {
        if (event.target.value === "") this.store.dispatch(new ClearUsers())
        if (!this.selectedNode) this.dispatchNodeValidationError()
    }

您可以看到,在reducer中注册了一个名为ClearUsers的新操作,我们正在调用它。

这是Reducer中的一个新案例,因此也是一个新的Action:

case AssumeIdActionTypes.GetUsersForNodeSuccess:
        const usersForNode = mapToHttpOption(action.users);
        return {...state,
            usersForNode: usersForNode,
            usersLoaded: hasUser(usersForNode),
            error: ''
        };

case AssumeIdActionTypes.ClearUsers:
    return {... state,
        usersForNode: []
    };