[英]Fix the last row of react material-table to display the sum of a particular column?
我们有一个要求显示 2 列的总和
import MaterialTable, {MTableToolbar, MTableAction} from "material-table";
.
.
.
<MaterialTable
actions={transformActions()}
data={data}
editable={props.allowRowEditing ? {
onRowAdd: newData => onRowAdd(newData),
onRowDelete: oldData => onRowDelete(oldData),
onRowUpdate: (newData, oldData) => onRowUpdate(newData, oldData)
} : {}}
icons={tableIcons}
.
.
./>
我有一个计算总数并显示总数的功能; 但是在对列进行排序时,显示总数的行也会移动。 有没有办法修复材料表本身的最后一行? 或者是否有任何风格黑客可以做到这一点?
根据以下问题报告,目前还没有任何方法可以使用材料表的属性来实现这一点
请注意,我不能随意使用另一个表库。
我试图定位最后一行并将其位置设置为绝对位置,如下所示
position: absolute;
height: 55px;
width: calc(100% - 1px);
bottom: 0;
虽然这将最后一行固定在其位置,但我找不到根据其他行对齐列的动态方法。
您可以使用TableFooter
元素将总数粘贴到表格底部。
覆盖 body 组件以添加页脚的示例:
import { TableCell, TableFooter, TableRow } from "@material-ui/core";
// ...
<MaterialTable
// ...
components={{
Body: (props) => (
<>
<MTableBody {...props}/>
<TableFooter>
<TableRow>
<TableCell colSpan={2}/>
<TableCell colSpan={2}>Total: 12345</TableCell>
</TableRow>
</TableFooter>
</>
)
}}
/>
这里的工作示例: https : //codesandbox.io/s/long-tree-fqkno
对如何呈现实际总值的里卡多答案的扩展
Body: (props) => {
let totalObj = {
actualSum: 0
}
props.renderData.forEach((rowData: any) => {
totalObj.actualSum += rowData.act_svc_lvl;
});
return (
<>
<MTableBody {...props}/>
<TableFooter>
<TableRow>
<TableCell colSpan={1}/>
<TableCell colSpan={1}/>
<TableCell colSpan={1}>{totalObj.actualSum}</TableCell>
</TableRow>
</TableFooter>
</>
)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.