7

I am using ng-select in my Angular application and have quite an unusual use case. I need to always display the placeholder, even on option selected. With current code the placeholder is replaced with the value of selected option:

<ng-select
  [(ngModel)]="selectedApplication"
  class="application-switcher"
  [attr.data-sel-id]="selId"
  [clearable]="false"
  appendTo="body"
  [searchable]="false"
  placeholder="{{ 'APP_TITLE' | translate }}"
  [virtualScroll]="virtualScroll"
  [markFirst]="false">
    <ng-option *ngFor="let application of applicationList" [value]="application">
      <div>
        {{ getApplicationName(application) }}
      </div>
    </ng-option>
</ng-select>
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
unclePutin
  • 71
  • 1
  • 1
  • 2
  • Where should the placeholder move to when an option is selected? – Ramesh Reddy Feb 24 '20 at 13:50
  • It should not, I need placeholder to be displayed event when an option is selected – unclePutin Feb 24 '20 at 13:53
  • 1
    Where do you suppose the placeholder should be when an option is selected? Above the selected option, below the selected option or somewhere else? – ruth Feb 24 '20 at 13:55
  • 1
    Maybe I phrased the question incorrectly, the placeholder should not appear anywhere else, the value of selected option should not override the placeholder value displayed. – unclePutin Feb 24 '20 at 14:00

4 Answers4

5

Note:

We can combine the chips view with show more option as

enter image description here

Code Snippet:

<ng-select
          [items]="depositGroupList"
          bindLabel="groupName"
          bindValue="groupId"
          formControlName="groupId"
          appearance="outline"
          class="custom"
          [multiple]="true"
          [searchable]="true"
          [closeOnSelect]="false"
          [clearable]="true"
        >
          <ng-template
            ng-multi-label-tmp
            let-items="items"
            let-index="index"
          >
            <div class="ng-value" *ngFor="let item of items | slice: 0:2">
              {{ item.groupName }}
            </div>
            <div class="ng-value" *ngIf="items.length > 2">
              <span class="ng-value-label"
                >{{ items.length - 2}} more...</span
              >
            </div>
          </ng-template>
        </ng-select>
mabdullahse
  • 3,474
  • 25
  • 23
0

If You Wana Have a fix placeholder inside the selected items area, you can do it this way:

Custom template for all selected items using ng-multi-label-tmp

<ng-select
    [items]="githubUsers$ | async"
    [multiple]="true"
    bindLabel="login"
    bindValue="login"
    placeholder="Select items"
    [(ngModel)]="selectedUsers">
    <ng-template ng-multi-label-tmp let-items="items" >
        <div class="ng-value" >
            <span class="ng-value-label">Items Selected: </span>
        </div>
        <div class="ng-value" *ngFor="let item of items ">
            <span class="ng-value-label"> {{item.login}}</span>
            <span class="ng-value-icon right" (click)="clear(item)" aria-hidden="true">×</span>
        </div>

    </ng-template>
</ng-select>

Change the html file in Stackblitz of this sample Custom template for all selected items using ng-multi-label-tmp to see the result:

enter image description here

Masoud Bimmar
  • 6,941
  • 4
  • 30
  • 33
0

You can do this

      <ng-select [items]="objects"
            bindLabel="name"
            bindValue="name"
            groupBy="type"
            [clearable]="false"
            placeholder="Select your choice...">


           <ng-template ng-multi-label-tmp let-items="items" >
            <div class="ng-value" >
                <span class="ng-value-label">Select your choice... </span>
            </div>
           </ng-template>


           <ng-template ng-option-tmp let-item="item" let-index="index">
                <span class="ng-value-label">{{item.name}}</span>
           </ng-template>
       

    </ng-select>
0

As a quick workaround you can try to add a custom template and leave it empty.

<ng-template ng-multi-label-tmp></ng-template>
Dharman
  • 30,962
  • 25
  • 85
  • 135