cdk虚拟滚动与粘性标题html表格角度

Kav*_*ree 7 html css angular6 angular-cdk-virtual-scroll

我从后端获取了超过 30000 条记录并在前端列出,因此使用 cdk-virtual-scroll 我可以实现这一目标。我创建了用 cdk-tag 括起来的普通表

          <cdk-virtual-scroll-viewport [itemSize] = "20">
            <div class="result_table">
             <table class="dataframe" border="1">
            <thead>
              <tr>
                <th >
                  Name
                </th>
                <th >
                  Description
                </th>
                <th >
                  Group
                </th>
                <th >
                  Data
                </th>
              </tr>
            </thead>
                <tbody>
                  <tr *cdkVirtualFor ="let data of address_obj_data_holder | filter:searchAddr">
                    <td >
                      {{data.name}}
                    </td>
                                             
                    <td >
                      {{data.description}}
                    </td>
                    <td >
                     {{data.group}}
                    </td> 
                    <td >
                     {{data.data}}
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </cdk-virtual-scroll-viewport>
Run Code Online (Sandbox Code Playgroud)

如果我这样做,当我向下滚动时,表格标题也会滚动,如果我喜欢,

          <cdk-virtual-scroll-viewport [itemSize] = "20">
            <div class="result_table">
             <table class="dataframe" border="1">
            <thead>
              <tr>
                <th style="width: 20% !important;">
                  Name
                </th>
                <th style="width: 40% !important;">
                  Description
                </th>
                <th style="width: 20% !important;">
                  Group
                </th>
                <th style="width: 20% !important;">
                  Data
                </th>
              </tr>
            </thead>
                <tbody>
                  <tr *cdkVirtualFor ="let data of address_obj_data_holder | filter:searchAddr">
                    <td style="width: 20% !important;">
                      {{data.name}}
                    </td>
                                             
                    <td style="width: 40% !important;">
                      {{data.description}}
                    </td>
                    <td style="width: 20% !important;">
                    {{data.group}}
                    </td> 
                    <td style="width: 20% !important;">
                      {{data.data}}
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </cdk-virtual-scroll-viewport>
Run Code Online (Sandbox Code Playgroud)

header 和 tbody 的宽度略有不同,两者的宽度不同,因为我们单独在下面的 body 中滚动。有人可以帮我解决这个对齐问题吗?

在此输入图像描述

小智 -2

如果您希望使表头粘在表格上并且您正在使用 cdk-virtual-scroll-viewport,请尝试以下操作:

table { overflow: auto; }
thead { position: sticky; top: 0; }
Run Code Online (Sandbox Code Playgroud)