B45*_*45i 5 javascript typescript angular-ngselect angular
如何在我的 ng-select 下拉列表中添加一个额外的项目,例如Create New in the following image:
这是我目前的代码:
<ng-select
[multiple]="true"
[hideSelected]="true"
[items]="robots"
formControlName="RobotGUID"
bindLabel="Name"
bindValue="GUID"
>
<ng-template ng-label-tmp let-item="item" let-clear="clear">
<ng-container *ngIf="item.GUID">
<span class="ng-value-icon left" (click)="onRobotEditClick($event, item.GUID)" aria-hidden="true">
<i class="fas fa-edit btn-focus"></i>
</span>
<span class="ng-value-label">{{item.Name}}</span>
<span class="ng-value-icon right" (click)="clear(item)" aria-hidden="true">×</span>
</ng-container>
</ng-template>
</ng-select>
Run Code Online (Sandbox Code Playgroud)
我尝试使用,<ng-option>但该项目没有出现在下拉列表中。如何从模板中添加额外的项目?
您可以使用ng-footer-tmp在选择框中添加其他项目。
像这样尝试:
.html
<ng-select [items]="cities"
bindLabel="name"
placeholder="Select city"
[(ngModel)]="selectedCity">
<ng-template ng-footer-tmp>
<p class="create-new" (click)="CreateNew()">Create New </p>
</ng-template>
</ng-select>
Run Code Online (Sandbox Code Playgroud)
.style.css
.create-new {
cursor: pointer;
padding-top:5px;
padding-bottom:10px
}
.ng-dropdown-footer{
border-top:unset !important;
padding: 0px 10px !important;
}
.ng-dropdown-footer:hover {
background-color: #f5faff;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用[addTag]和addTagText。
[addTag] :允许创建自定义选项。addTagText :使用标记时设置自定义文本。app.component.html :
<ng-select [items]="cities"
bindLabel="name"
placeholder="Select city"
[(ngModel)]="selectedCity"
addTagText="Create New"
[addTag]="CreateNew">
</ng-select>
Run Code Online (Sandbox Code Playgroud)
app.component.ts :
export class AppComponent {
cities = [
{id: 1, name: 'City1'},
{id: 2, name: 'City2'},
{id: 3, name: 'City3'},
{id: 4, name: 'City4'},
{id: 5, name: 'City5'}
];
CreateNew(city){
alert("Create New Clicked : "+city)
}
}
Run Code Online (Sandbox Code Playgroud)
图片 :
2.
| 归档时间: |
|
| 查看次数: |
5565 次 |
| 最近记录: |