【问题标题】:Bootstrap 4 html tooltip in bootstrap table angular 8 for loop引导表中的引导程序 4 html 工具提示 Angular 8 for 循环
【发布时间】:2021-07-05 01:04:08
【问题描述】:

我正在尝试在表格中添加 html 工具提示。

包括以下CDN:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

表格代码:

<table class="table table-bordered">
      <thead *ngIf="serchResults">
      <tr>
        <th scope="col">Description</th>
        <th scope="col">Action</th>
      </tr>
      </thead>
      <tbody>
      <tr *ngFor="let item of serchResults">
        <td data-container="body" data-toggle="tooltip" data-html="true" title="<p>Mapped to: </p> <p>Abcd data </p> <p> Test data </p><p> Test data two </p><p> Test data 3</p>">
          {{item.title}}
        </td>
        <td><input type="checkbox" id="{{item.code}}" name="{{item.code}}" value="{{item.code}}"></td>
      </tr>
      </tbody>
    </table>

jquery:

<script>
      $(function() {
        $('[data-toggle="tooltip"]').tooltip({
          html: true,
        });
      });
  </script>

请看下面的截图。 HTML 工具提示不起作用。

工具提示 HTML 未在表格和 for 循环内正确显示。也将 CSS 应用于“inner-tooltip”在 for 循环内部和没有 for 循环时都不起作用。

【问题讨论】:

    标签: javascript jquery angular bootstrap-4 twitter-bootstrap-tooltip


    【解决方案1】:

    首先,避免使用jquery,你可以使用ngx-bootstrap。

    不过,要解决此问题,您可能需要在设置 searchResults 后运行以下脚本。原因是,ngFor 只有在它获得了它正在迭代的对象之后才会创建 DOM。一旦 serachResults 对象可用,您需要运行。

     $(function() {
            $('[data-toggle="tooltip"]').tooltip({
              html: true,
            });
     }); 
    

    【讨论】:

      【解决方案2】:

      找到了解决办法。很简单。

      <table class="table table-bordered" *ngIf="serchResults; else elseTable">
            <thead>
            <tr>
              <th scope="col">Description</th>
              <th scope="col">Action</th>
            </tr>
            </thead>
            <tbody>
            <tr *ngFor="let item of serchResults">
              <td>
                <span data-container="body" data-toggle="tooltip" data-html="true" [tooltip]="tooltipData" placement="bottom">{{item.title}}</span>
                <ng-template #tooltipData><p [innerHtml]="createTooltip(item)"></p></ng-template>
              </td>
              <td><input type="checkbox" id="item.code" name="item.code" value="item.code" (change)="fieldsChange($event, item)"></td>
            </tr>
            </tbody>
          </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 2018-06-27
        • 2013-05-07
        • 2013-01-04
        • 2018-02-27
        • 2021-04-23
        相关资源
        最近更新 更多