【问题标题】:how to place text input beside of chips in Angular如何在Angular中的芯片旁边放置文本输入
【发布时间】:2020-06-03 21:00:07
【问题描述】:

我正在尝试制作可以显示标签、输入和下拉的 mat 表单字段,如下面的设计,但我不确定如何实现,所以任何人都可以帮助我。我已经尝试了几个小时的不同方法,但仍然找不到使它起作用的方法。如果有人能给我建议或帮助,将不胜感激。

我只是想将文本输入始终放在筹码旁边(如果用户放了很多筹码,可能会像我现在所拥有的那样扩展),并且在侧面也有下拉选项(图标)设计如下。

<mat-chip-list *ngIf="editMode">
    <mat-form-field class="form" appearance="fill">

        <!--show Tags-->
        <ng-container *ngFor="let chip of chips" class="add-tag-chip" (click)="displayTagInput()">
                <mat-chip [selectable]="selectable" [removable]="removable" (removed)="removeTags(chip)">
                    {{chip.tag}}
                    <mat-icon matChipRemove *ngIf="chip.pending !== true && removable" matTooltip="Remove a tag">cancel</mat-icon>
                    <mat-spinner *ngIf="chip.pending === true" [diameter]="16" mode="indeterminate"></mat-spinner>
                </mat-chip>
        </ng-container>

        <!--Text Input (supposed to be on the side of Tags)-->
            <input matInput [(ngModel)]="tagIn" [matAutocomplete]="auto" placeholder="New Tag"  (keyup.enter)="addTag()" />

        <!--For Drop Down List-->
            <mat-autocomplete #auto="matAutocomplete">
                <mat-option *ngFor="let tag of filteredTags | async" [value]="tag">
                    {{tag}}
                </mat-option>
            </mat-autocomplete>

    </mat-form-field>
</mat-chip-list>

这是我正在尝试做的设计

这就是我现在拥有的

【问题讨论】:

    标签: html angular input mat-form-field


    【解决方案1】:

    您为什么不按照文档中的说明使用它?我还发现 this stackblitz 这正是您想要的。这是html代码:

    <mat-form-field class="demo-chip-list">
      <mat-chip-list #chipList>
        <mat-chip
          *ngFor="let fruit of fruits"
          [selectable]="selectable"
          [removable]="removable"
          (removed)="remove(fruit)">
          {{fruit}}
          <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
        </mat-chip>
        <input
          placeholder="New fruit..."
          #fruitInput
          [formControl]="fruitCtrl"
          [matAutocomplete]="auto"
          [matChipInputFor]="chipList"
          [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
          [matChipInputAddOnBlur]="addOnBlur"
          (matChipInputTokenEnd)="add($event)"
        />
      </mat-chip-list>
      <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
        <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
          {{ fruit }}
        </mat-option>
      </mat-autocomplete>
    </mat-form-field>
    

    请注意,没有ng-container(它们在您的代码中似乎不是强制性的)并且mat-form-field 标签包含了整个内容。在您的代码中,您将其作为 mat-chip-list 的子代。


    [编辑]:我明白了。这是代码:

    .css:

    /* ::ng-deep needed to only target your component (it's deprecated but have not replacement for the moment) */
    .your-component ::ng-deep .mat-form-field-infix {
      display: flex !important
    }
    
    /* Change the placeholder to stick to the same position as if your input is focused, not really good */
    .your-component ::ng-deep.mat-form-field-label {
        transform: translateY(-1.28125em) scale(.75) perspective(100px) translateZ(.001px) !important;
    }
    

    .html:

    <mat-chip-list>
        <mat-form-field>
        <!-- ng-container is now a span -->
            <span *ngFor="let fruit of fruits" (click)="displayTagInput()">
                <mat-chip [selectable]="selectable" [removable]="removable" (removed)="removeTags(chip)">
                    {{fruit}}
                    <mat-icon matChipRemove matTooltip="Remove a tag">cancel
                    </mat-icon>
                </mat-chip>
            </span>
    
            <input matInput [(ngModel)]="tagIn" [matAutocomplete]="auto2" placeholder="New Tag..."  (keyup.enter)="addTag()" />
    
            <mat-autocomplete #auto2="matAutocomplete" (optionSelected)="selected($event)">
                <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
                    {{ fruit }}
                </mat-option>
            </mat-autocomplete>
    
        </mat-form-field>
    </mat-chip-list>
    

    这里是the demo ons tackblitz。请注意,它不能完美地工作。我不得不用 css 强制占位符的收缩效果(我认为这是因为 mat-form-fieldmat-chip-list 内部)。

    另外,由于我删除了一些以使其更清晰,因此您必须使用自己的代码、芯片删除方法等对其进行测试。

    希望对你有帮助:)

    【讨论】:

    • 我想我也看到了那个例子,但仍然无法修复它。我是 mat-chip-list 的孩子,因为我有编辑模式和非编辑模式(不包括在内)。我还发现右侧的下拉图标,这就是我在这里发布的原因。如果你能帮忙,那就太棒了。谢谢
    • 好吧,请添加您目前拥有的复制品,否则我们将无法提供所有需要的元素。
    • 感谢您的回复,我真的很想这样做,但我也在使用后端数据,所以我不确定是否可以在 stackblitz 上添加它。有没有机会您可以查看代码并发现缺陷。谢谢
    • @aaselab 我编辑了我的答案,给你我能做的。我认为这“还不错”,但占位符并不完美。您尝试为标签或其他内容添加一些边距。如果您可以删除有关.mat-form-field-label的css
    • 非常感谢您帮助我。您是否知道我应该如何实现输入框右侧的向下箭头图标,以便用户可以单击并查看现有标签。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多