- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
目标
我想创建一个带有滚动主体和粘性标题的表格。表格标题单元格的宽度需要与其对应的正文列宽相匹配。
它在 React 中,Table 组件包含两个独立的组件(一个用于标题,另一个用于正文中的行)。组件的内容将来自外部来源,因此我无法对任何内容的宽度做出任何假设。表格的主体将有很多行,因此表格标题需要保持粘性并保持在顶部。
问题
虽然表体中列的宽度是自动设置的,但表头中的宽度不会自动设置。表头宽度需要根据表格内容自动设置。
你可以在这里看到问题和我的测试代码: https://codesandbox.io/s/y0jx5rp081
注意事项
最佳答案
您可以通过在呈现后查询它们来获取表格单元格的宽度。要在渲染后访问 React 组件,我们需要使用 ref。
constructor(props) {
...
this.bodyRef = React.createRef()
}
将其作为 prop 传递给表体。
<tbody
...
ref={this.bodyRef}
>
然后在componentDidMount中渲染后就可以访问了。如果您删除、添加或编辑行,您可能也想在 componentDidUpdate 中执行此操作(但要注意不要形成无限循环)。
componentDidMount() {
const row = this.bodyRef.current.children[0].children; // Get the children of one <tr>
// the path from this.bodyRef.current to the children you need may vary.
const headerWidths = [];
for (let i = 0; i < row.length; i += 1) {
headerWidths.push(row[i].getBoundingClientRect().width); // Get the rendered width of the element.
}
this.setState({
headerWidths
});
}
然后只需使用您所在州的宽度值即可。
constructor(props) {
...
this.state = {
...
headerWidths: []
}
}
...
render() {
return (
...
<th style={{ width: this.state.headerWidths[index] }}>
...
</th>
)
}
这是您方便的示例的工作版本。
关于javascript - react .js : Set sticky table-header cell width based on corresponding table-row cell width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51597713/
git count-objects -v 发出的这个警告是什么意思? Google 只给了我 git 本身的来源。 warning: no corresponding .pack: .git/obje
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
假设您将一个项目分解为一个 super 项目和一个子模块。它们都有稳定的(主)分支和开发分支。有没有办法设置您的子模块,以便 super 项目从其 dev 分支跟踪子模块的 dev 分支,并从其稳定分
我有一个列表,其中包含两种不同的商品,product-x 和 product-y。 当用户删除 product-x 时,我需要它的等效 product-y 也删除,反之亦然。 我已经尝试了几种不同的方
下面的示例显示了一个 box div 和一个 :after 内容,它应该是一个单独的 block 。 div.box { background-color: #FAA; width:
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我使用的是 ABAP 7.4 SP13。 我想在下面的代码中使用“选项 2”,但是如何进行这种深度映射?这里的目标是有一个干净、简单、易于阅读的映射,避免循环。 TYPES: BEGIN OF
我试图充分了解 docker 的功能,以便对我安全地使用它产生合理的信心。对此的一个建议是始终使用 USER Dockerfile 中的语句。在试图理解这一点的影响时,我遇到了一些麻烦。 具体问题:
我在 Flutter Web 应用程序中使用命名路由进行导航。导航到所需的路由时,URL 会更新,但我无法通过 URL 栏直接导航到该路由。每次我尝试在 URL 中添加路径时,它都会将我带到“.../
我重命名了一个文件夹并更新了命名空间声明,但 ReSharper 6 声称命名空间应该反射(reflect)重命名之前的情况。文件位置数据存储在哪里? 最佳答案 检查并确保您的程序集名称与新的命名空间
我正在 Shopify 上创建带有折扣的草稿订单,其返回错误类似于“必须与根据值计算出的值相对应”。 我计算折扣如下: $amount 是订单总金额 (78.99),$rate (30) 是折扣百分比
我正在使用pyspark.ml.classification中的RandomForestClassifier 我在二进制类数据集上运行模型并显示概率。 我的列概率如下: +-----+--------
我正在尝试绘制以不同角度记录的传感器数据。 import pandas as pd import matplotlib.pyplot as plt #create dataframe, each
我的 ASP.NET 页面没有显示控件。它只是显示母版页错误 The page has one or more controls that do not correspond with 代替 关
问题: 我有一大堆日期,我需要按周排列它们。 问题:如何按日期所在的一年中的第几周对日期进行分组? 示例数据集 Date date = new SimpleDateFormat.parse("04/0
我想获取此命令的选项: my_cmd show --value true -D 为此,我需要通过选项两次(由于架构)。在第二遍期间,无法正确检索与其选项对应的参数。在此示例中,在第二次传递期间检索到的
本地代码 native .c #include #include #include jstring Java_com_lab5_oli_myapplication_MainActivity_he
我正在使用 Firebase Python AdminSDK 生成自定义 token ,Javascript 客户端使用该 token 登录 Firebase。当 JS 客户端尝试使用自定义 toke
在上面的 excel 表中,我需要一个公式来提取存在“1”的特定列名,然后在相应的单元格中输入该名称。示例 - 在上面的图像中,对应于 col“Tags”的每一行都包含相应单元格包含“1”的列名。 最
我是一名优秀的程序员,十分优秀!