【问题标题】:How to show tooltip on @ng-select/ng-select options如何在@ng-select/ng-select 选项上显示工具提示
【发布时间】:2019-01-11 23:35:48
【问题描述】:

我在我的应用程序中使用@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>

但没有运气

【问题讨论】:

  • 嗯,很可能有一个最大长度。将用于测试目的的文本缩短为 1 个单词,并看看这是否至少有效。
  • 我也有缩短文本,但问题仍然存在。

标签: angular typescript angular6


【解决方案1】:

这个问题很老,但使用 span 可能是另一种解决方案:

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

【讨论】:

    【解决方案2】:

    在我的情况下,我使用 ngx-bootstrap 和 ng select。这是示例enter image description here

    <div>
      <ng-template #itemTol>
        {{getTooltips(items, value)}}
      </ng-template>
        <ng-select [items]="items" #ngitem[clearable]="false" [tooltip]="itemTol" [isDisabled]="ngitem.isOpen" bindLabel="text" bindValue="value" placeholder="Please select" name="sales_agent" formControlName="name">
        <ng-template ng-option-tmp let-item="item">
          <div [tooltip]="item.text" placement="bottom">{{item.text}}</div>
        </ng-template>
      </ng-select>
    </div>

    【讨论】:

    • 很高兴能解释一下您的代码为何能解决问题。
    【解决方案3】:

    就我而言,问题的解决方案是将“FormsModule”添加到导入“NgSelectModule”的模块中

    你的模块应该是这样的:

    import { NgSelectModule } from '@ng-select/ng-select';
    import { FormsModule } from '@angular/forms';
    @NgModule({
      imports: [
    FormsModule,
    NgSelectModule,
    ]
    

    【讨论】:

      【解决方案4】:
      <ng-select placeholder="Select" (change)="onChange($event)">
            <ng-option *ngFor="let list of listArray">
               <div title="{{list.name}}"> 
                  {{list.name}}
               </div>
            </ng-option>
       </ng-select>
      

      【讨论】:

        【解决方案5】:

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

        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 元素的工具提示。 抱歉英语不好=)

        【讨论】:

          【解决方案6】:

          您可以使用 @ng-bootstrap/ng-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

          我添加了一个仅使用引导程序而没有外部库的演示。将鼠标悬停在选项上以查看工具提示(需要几秒钟才能显示): New Demo

          【讨论】:

          • 我在数组中没有单个值,最多有 15 个对象,在这种情况下它将如何工作。
          • 我添加了一个仅使用引导程序的演示,没有外部库来满足您的要求。不知道是否适合您,但如果您想尝试,请查看。当您有多个项目要显示工具提示时,使用 ng-select 有点棘手。
          • 感谢您的回答,但引导下拉菜单看起来很糟糕。我不想用那个。
          • 为我工作,只是添加了placement="right" container="body" 以使其充分发挥作用
          【解决方案7】:

          您可以在&lt;ng-option&gt; 中添加template,并添加指令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

          【讨论】:

          • "
            {{list.name}}
            " 它认为是一个选项值并且 div 也打印在列表中。
          • @AniketAvhad 我已经更新了我的答案,您必须将值绑定到ng-option
          • 如果我使用你的解决方案然后出现这样的问题,请查看stackblitz.com/edit/…"
          • @AniketAvhad 我浏览了一下源代码,你应该使用ng-option-tmp 指令
          • @Poul 感谢您的回答...在此示例中... stackblitz.com/edit/angular-wziwui?file=src/app/... 我选择第一个选项.. “Loreum ... .." 选择后...我们可以看到所选项目...当我将鼠标悬停在所选值上...我想看到标题(因为名称太大而用户看不到完整值)与我打开下拉选项并将鼠标悬停在...我可以看到标题。
          【解决方案8】:

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

          <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>
          

          谢谢

          【讨论】:

          • 感谢您的回答。如果我们需要显示所选项目的标题怎么办?
          • @atitpatel 您能否添加有关您确切需要的示例或代码?
          • 在这个例子中... stackblitz.com/edit/angular-wziwui?file=src/app/... 我选择第一个选项.. "Loreum ....." 选择后...我们可以看到所选项目...当我将鼠标悬停在所选值上时...我希望看到标题(因为名称太大,用户无法看到完整值)与我打开下拉选项并将鼠标悬停在上方时相同。 .. 我可以看到标题。
          • @atitpatel 将 "[title]="Selected" 添加到 ng-select 并在悬停时显示选定的值。
            {{item.name}}
            我已经更新了你的“stackblitz.com/edit/angular-wziwui”。
          • 如果我没有 ng-model 怎么办?我们可以使用任何其他方法吗?
          猜你喜欢
          • 2022-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-15
          • 2019-02-16
          • 1970-01-01
          • 2019-03-06
          相关资源
          最近更新 更多