【问题标题】:How to create custom tooltip with formatter in Angular slickgrid without creating angular component?如何在 Angular slickgrid 中使用格式化程序创建自定义工具提示而不创建角度组件?
【发布时间】:2021-05-19 09:22:46
【问题描述】:

上下文

当悬停在行数据上时,我想显示带有自定义数据的工具提示,我不想使用 Angular 组件或引导工具提示,而是使用简单的 html 和 css,以便我可以将它与 Angular Slick 一起使用-网格格式化程序

我尝试过使用 html 和 css,结果如图所示,工具提示不完全可见,隐藏在表格中

谁能帮我解决这个问题?

环境 Angular - 8.2.14,Angular-Slickgrid - 2.22.4,TypeScript - 3.7

【问题讨论】:

    标签: angular typescript slickgrid angular-slickgrid


    【解决方案1】:

    由于 CSS 样式是如何在 SlickGrid 中完成的,您需要找到一个库,该库可以选择在“body”容器上呈现工具提示,而不仅仅是在单元格容器上(这是您尝试过的)。如果您使用 Bootstrap Tooltip,那将是他们在其网站上写的这一行

    指定container: 'body' 以避免在更复杂的组件(如我们的输入组、按钮组等)中出现渲染问题。

    请参阅引导工具提示 Options,特别是 container 选项

    编辑 22021-10-29

    使用下面显示的代码,我为 Angular-Slickgrid 和 SlickGrid 创建了一个新插件。您可以看到 Angular-Slickgrid 版本3.3.0 及更高版本的新插件的实时Example 32 - Custom Tooltip。它适用于很多用例,使用 [title] 属性或通过提供自定义工具提示格式化程序,它还具有用于处理 API 异步调用(Promise/Observable)的异步选项。

    编辑 1

    我又玩了这个,因为我还想添加基本的工具提示支持(可能没有任何工具提示库),下面是一个使用 SlickGrid onMouseEnteronMouseLeave 事件的非常基本且实用的工具提示。这个答案使用了一些来自较旧的 SO Answer

    <angular-slickgrid gridId="grid22"
                           [columnDefinitions]="columnDefinitions"
                           [gridOptions]="gridOptions"
                           [dataset]="dataset"
                           (onMouseEnter)="handleOnMouseEnter($event.detail.eventData)"
                           (onMouseLeave)="handleOnMouseLeave($event.detail.eventData)"
                           (onAngularGridCreated)="angularGridReady($event.detail)">
    </angular-slickgrid>
    
    import { getHtmlElementOffset } from 'angular-slickgrid';
    
    export class MyComponent {
      angularGridReady(angularGrid: AngularGridInstance) {
        this.angularGrid = angularGrid;
      }
    
      handleOnMouseEnter(e) {
        if (this.angularGrid.slickGrid && e) {
          const cell = this.angularGrid.slickGrid.getCellFromEvent(e);
          if (cell) {
            const item = this.angularGrid.dataView.getItem(cell.row);
            const columnDef = this.sgb.slickGrid.getColumns()[cell.cell];
            const cellPosition = getHtmlElementOffset(this.angularGrid.slickGrid.getCellNode(cell.row, cell.cell));
            const tooltipElm = document.createElement('div');
            tooltipElm.className = 'some-tooltip'; // you could also add cell/row into the class name
            tooltipElm.innerHTML = `<div><label>Id:</label> ${item.id}</div><div><label>Title:</label> ${item.title}</div>`;
            document.body.appendChild(tooltipElm);
            tooltipElm.style.top = `${cellPosition.top - tooltipElm.clientHeight - 3}px`;
            tooltipElm.style.left = `${cellPosition.left}px`;
          }
        }
      }
    
      handleOnMouseLeave() {
        // don't forget to destroy tooltip when leaving the cell
        // you could also use the event if you provided cell/row into the class name
        const prevTooltip = document.body.querySelector('.some-tooltip');
        prevTooltip?.remove();
      }
    }
    
    /* basic styling */
    .basic-tooltip {
      height: 55px;
      width: 125px;
      position: absolute;
      z-index: 10;
      background-color: white;
      border: 1px solid #e0e0e0;
      border-radius: 3px;
    }
    

    【讨论】:

    • 我们应该将它与自定义格式化程序选项一起使用吗?
    • 使用非常基本且有效的自定义工具提示更新了答案。工具提示创建可以替换为任何工具提示库,如 Bootstrap 工具提示、tipsy.js、...
    • 使用答案中提供的代码,我创建了一个新的自定义工具提示插件,检查更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多