如何在@ ng-select / ng-select选项上显示工具提示

问题描述 投票:1回答:4

我在我的应用程序中使用@ ng-select / ng-select @ 2.3.6,我在数组中有一个很长的文本。

因此,完整的文本在下拉列表中不可见,所以我想在每个选项上显示标题/工具提示

我试过了,

let listArray = [{name: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s'}];

<ng-select placeholder="Select" (change)="onChange($event)">
      <ng-option *ngFor="let list of listArray" 
       title="{{list.name}}"> {{list.name}} </ng-option>
 </ng-select>

但没有运气

angular angular6
4个回答
5
投票

您可以使用以下代码实现工具提示解决方案

<ng-select [items]="listArray" bindLabel="name" bindValue="name">
    <ng-template ng-option-tmp let-item="item">
    <div title="{{item.name}}">{{item.name}}</div>
    </ng-template>
</ng-select>

谢谢


1
投票

您可以使用@ ng-bootstrap / bootstrap库与ng-select并行显示工具提示:

模板:

<ng-select [ngbTooltip]="tipContent" container="body" placement="bottom" placeholder="Select" (change)="onChange($event)">
      <ng-option *ngFor="let list of listArray" title="{{list.name}}"> {{list.name}}  
     </ng-option>
</ng-select>

<ng-template #tipContent>{{listArray[0].name}}</ng-template>

TS:

listArray = [
    {name: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"}
];

Demo

我只使用bootstrap添加了一个没有外部库的演示。将鼠标悬停在选项上以查看工具提示(在显示之前需要几秒钟):New Demo


0
投票

你可以在template中加入<ng-option>,并添加指令ng-option-tmp

<ng-select [items]="listArrayManyElements" placeholder="Select" [(ngModel)]="Selected" 
           bindLabel="name" bindValue="name">
    <ng-template ng-option-tmp let-item="item">
        <div [title]="item.name">{{item.name}}</div>
    </ng-template>
</ng-select>

我已经更新了你的stackblitz


0
投票

你可以像这样创建自定义指令:

import { ContentChild, Directive, ElementRef, OnInit } from '@angular/core';
import { NgSelectComponent } from "@ng-select/ng-select";

@Directive({
selector: '[admSelectTitle]'
})

export class SelectTitleDirective implements OnInit {

@ContentChild(NgSelectComponent) select: NgSelectComponent;

@ContentChild(NgSelectComponent, { read: ElementRef }) selectNative: ElementRef;

/**
 * @inheritDoc
 */
ngOnInit() {
    if (!this.select) {
        return;
    }
    const nativeElement = this.selectNative.nativeElement;
    nativeElement.addEventListener("mouseover", function() {
        const listOfPickedElements = nativeElement.querySelectorAll('.ng-value-label.ng-star-inserted');
        listOfPickedElements.forEach((el) => {
            el.setAttribute('title', el.innerHTML);
        })

        const listOfAvailableOptions = nativeElement.querySelectorAll('.ng-dropdown-panel-items.scroll-host .ng-option.ng-option-marked');
        listOfAvailableOptions.forEach((v) => {
            v.setAttribute('title', v.innerText)
        })

    })
}
}

之后你可以简单地使用它:

<ng-select admSelectTitle [items]="yourItems" bindLabel="name" bindValue="id" class="anyClass" formControlName="anyName" [multiple]="true">
                    <ng-template ng-option-tmp let-item="item">
                        {{ item.name }}
                    </ng-template>
                </ng-select>

现在有关于ng-select和ng-options元素的工具提示。抱歉英文不好=)

© www.soinside.com 2019 - 2024. All rights reserved.