Angular 5 - 多选而不按Ctrl键

标签 angular multi-select angular-reactive-forms

我正在处理一个表单,我正在使用 Angular Reactive Forms。 我需要在我的表单中进行多项选择,我确实通过按 ctrl 并选择多个选项来完成它。 我希望能够在不按 ctrl 键的情况下使用多选。 有可能吗?

表单组件:

this.productForm = new FormGroup({
  'items': new FormControl(this.product.items, Validators.required),
});

模板:

<div class="form-group">
  <select multiple="multiple" class="form-control" id="items" formControlName="items" required>
    <option *ngFor="let item of itemList" [value]="item.id">{{item.name}}</option>
  </select>
</div>

更新:

我尝试了 greg95000 的解决方案,但它只是一个部分有效的解决方案。

<div class="form-group">
  <select multiple="multiple" class="form-control" id="items" formControlName="items" required>
    <option (mousedown)="onMouseDown($event)" (mousemove)="$event.preventDefault()" *ngFor="let item of items" [value]="item.id">{{item.name}}</option>
  </select>
</div>


public onMouseDown(event: MouseEvent) {
  event.preventDefault();
  event.target['selected'] = !event.target['selected'];
}

这个解决方案打破了数据绑定(bind)

Multiselect view

最佳答案

我有一个有效的解决方案。因为我复制数组 Angular 知道有变化所以选择得到更新。 这是我的选择:

 <select class="form-control" multiple name="positionTypes" [(ngModel)]="freelancer.positionTypes" #select1>
  <option *ngFor="let positionType of positionTypes" [value]="positionType" (mousedown)="false" 
   (click)="positionTypMouseDown($event)">{{'PositionType.'+positionType | translate}}</option> 
 </select>

这是我组件中的函数:

positionTypMouseDown( event: any ) {
    event.stopPropagation();
    let scrollTop = 0;
    if ( event.target.parentNode ) {
        scrollTop = event.target.parentNode.scrollTop;
    }
    const stringValue = event.target.value.split( '\'' )[1];
    const index = this.freelancer.positionTypes.indexOf( stringValue, 0 );
    if ( index > -1 ) {
        this.freelancer.positionTypes.splice( index, 1 );
    } else {
        this.freelancer.positionTypes.push( stringValue );
    }
    // to make angular aware there is something new
    const tmp = this.freelancer.positionTypes;
    this.freelancer.positionTypes = [];
    for ( let i = 0; i < tmp.length; i++ ) {
        this.freelancer.positionTypes[i] = tmp[i];
    }
    setTimeout(( function() { event.target.parentNode.scrollTop = scrollTop; } ), 0 );
    setTimeout(( function() { event.target.parentNode.focus(); } ), 0 );
    return false;
}

我现在更新了我的答案,它在 chrome 中也能正常工作。重要的部分是焦点和滚动不关注选项而是关注选择(即 event.target.parentNode

关于Angular 5 - 多选而不按Ctrl键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318596/

相关文章:

angular - Ag 网格和 react 形式

angular - 禁用表单数组中的响应式(Reactive)表单选择表单控件

angular - 单元测试 angular 2 服务在注入(inject)后不运行回调

angular - Ng serve 或 ng new 不起作用

angular - 为什么在此 FormArray 中无法编辑 mat-form-field?

javascript - 多选在 CasperJS 中不起作用

angular - 直接链接到 Angular 4 中的子路由

javascript - 0x800a138f - JavaScript 运行时错误 : Unable to get property 'fn' of undefined or null reference

javascript - 计算多项选择中的所有数字返回总和 Multiselect + jQuery

angular - 以 Angular react 形式动态设置文本区域禁用