Feature request: Total (footer) row
Officially Supported: [ ] v0.14.x
Community Supported: [V] v15.0.x
**WHICH BROWSER ARE YOU USING?**
Officially Supported: [ ] IE 9 / IE 10 / IE 11 [ ] Edge [V] Chrome
Should work: [ ] Firefox [ ] Safari
--------
**I'm submitting a ...** (check one with "x")
[ ] bug report [V] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/adazzle/react-data-grid/blob/master/CONTRIBUTING.md
**Current behavior**
Doesn't seem to include a total row.
I used another Grid but:
1. It multiplies a lot of definitions including all the <Style>s.
2. It doesn't scroll with the main grid.
**Expected/desired behavior**
To allow it as a fixed line at the end of the grid.
I'd also be keen to see this; ideally as either a single row right at the bottom of the grid, or as a row fixed at the bottom of the scrollable area, which is visible wherever your scroll position is.
It's actually relatively easy to achieve this without the "doesn't scroll with the main grid" by providing a rowRenderer class (or function). I did it by defining the row type as NormalRowType | TotalsRowType, and pushing a totals row onto the end of the normal rows. Checking for a property on TotalsRowType allows your rowRenderer class to determine what to do. The [as far as I know undocumented] bit that helps is that the columns have already been sized, so you can easily align totals row cells (assuming you are not totalling everything) with the columns in the remainder of the grid.
It would still be nice to have a proper implementation with non-scrolling always-visible total row, but the second best is doable now fairly easily
@OracPrime Can you provide a snip please? I'd be interested to know how you managed this.
Sure - up against a work deadline at the moment, but give me a couple of days.
This should give you the basic idea, I hope:
import * as ReactDataGrid from "react-data-grid";
interface dataRow {
name: string;
padding: string;
amount: number;
}
interface totalRow {
isTotalRow: boolean;
totalAmount: number;
}
type tableRow = dataRow | totalRow;
interface IDataGridColumn<T> extends ReactDataGrid.Column {
key: keyof (T);
}
const rows = [
{ name: "hello", padding: "world", amount: 2 },
{ name: "wibble", padding: "wobble", amount: 3 },
{isTotalRow:true, totalAmount:5}
]
export class RowRenderer extends React.Component<{
idx: number,
row: any,
height: number,
colVisibleStart: number,
columns: any[]
}, {}>{
// private setScrollLeft(scrollBy) {
// // if you want freeze columns to work, you need to make sure you implement this as apass through
// this.refs.row.setScrollLeft(scrollBy);
// }
public render() {
if (this.props.row.isTotalRow) {
return (<div>
<div height={this.props.height}
style={{
position: "absolute",
width: this.props.columns[0].width,
height: this.props.height,
left: this.props.columns[0].left,
contain: "layout"
}}>
<div className="react-grid-Cell__value">Total</div>
</div>
<div height={this.props.height} className="total-row react-grid-Cell"
style={{
position: "absolute",
width: this.props.columns[2].width,
height: this.props.height,
left: this.props.columns[2].left,
contain: "layout"
}}>
<div className="react-grid-Cell__value">
{this.props.row.totalAmount}
</div>
</div>
</div>);
}
return <ReactDataGrid.Row ref="row" {...this.props} />;
}
};
export class Counter extends React.Component<void, void> {
private columns: IDataGridColumn<dataRow>[] = [
{ name: "Name", key: "name" },
{ name: "Padding", key: "padding" },
{ name: "Amount", key: "amount" }
]
private getRow(i: number) {
return rows[i];
}
public render() {
return <div>
<ReactDataGrid
rowGetter={this.getRow.bind(this)}
rowsCount={3}
columns={this.columns}
minHeight={300}
rowRenderer={RowRenderer}
>
</ReactDataGrid>
</div>;
}
}
One thing I forgot to mention (apart from tagging @davewthompson ) is that you need to make sure your render methods/components can cope with receiving an undefined value, as I think somewhere in the measure process can call the render methods for the normal row on the total row; but provided they check for undefined and don't do anything with it, it all works.
@davewthompson it's actually much cleaner than I suggested above:
` class BlankRenderer extends React.Component<void, void> { public render() { return ; } } export class RowRenderer extends React.Component<{ row: any, columns: any[], }, {}>{
public render(): JSX.Element {
let {columns, ...rest} = this.props;
columns = columns.slice(0); // take a copy (of the array, not the objects)
if (this.props.row.isTotalRow) {
columns = columns.map(c => {
if (Object.keys(this.props.row).indexOf(c.key) >= 0) {
return c;
}
c = { ...c }; // Shallow copy
c.formatter = BlankRenderer;
return c;
});
}
return <Row ref="row" columns={columns} {...rest} />;
}
}; `
If it's part of the main grid, it would sort with the rest of the data and it wouldn't always be available. I ended up using a second grid and scrolling from the wrapper with:
componentWillMount() {
if(this.props.totals) window.addEventListener('scroll', this.checkScroll, true)
}
componentWillUnmount() {
window.removeEventListener('scroll', this.checkScroll, true)
}
checkScroll(evt) {
let cs = evt.target.className;
if(cs == 'react-grid-Canvas') {
let scrollLeft = evt.target.scrollLeft;
try {
if(this.refs.totalsGrid) this.refs.totalsGrid.refs.base.refs.header.setScrollLeft(scrollLeft)
} catch (e) { }
}
}
Hi,
I am trying to do the same thing but I can't figure out. :( I want something sticky at the bottom of the table and not affected by sorting as @yairniz suggested but I don't understand what @yairniz meant by "using a second grid and scrolling from the wrapper"
It would be great if there is a concise example for this.
@nextofsearch I think @yairniz meant that horizontal scrolling would be messed up if the two grids weren't linked with a ref.
@yairniz
What kind of JSX do you have on your render? I'm trying to figure out how you use refs in your grid components
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please reopen this if you feel it has been incorrectly closed and we will do our best to look into it. Thank you for your contributions.
Hi,
I think this feature from react-table.js is exactly what we need.
Basically, it is allows user to add Footer prop which is a callback function to each column. What this will do is it will create a cell(value determined by callback) for that column at the end of each column.
That way, it is always at the bottom and it scrolls with the rest of the data.
Let me know if you need any more information.
I also need total row . Trying to solve it by adding another grid but have probelm with scrolling. Can not scroll horizontally totalGrid when mainGrid scrolls. Trying the solution from @yairniz but it does not work. @yairniz don't you have working solution for scrolling ?
@JanBN The solution consisted of:
- Creating another grid with the same columns definition.
- Listening on the scroll event of the main grid.
- Using setScrollLeft(maingrid.scrollLeft) on the total grid row.
@yairniz Thank you for the reply.
I have implemented it, but unfortunately the footer grid does not scroll.
this.footerGrid.base.header.setScrollLeft(value);
does nothing.
I have tried also
this.footerGrid.base.viewport.setScrollLeft(value);
but with no luck
Does this scrolling work for you ?
I don't have the code but I think I had to set it for the row specifically. Also try with/without 'px'. For debug, export your grid to the window scope and find the correct manner. (window.grid = this.footerGrid;)
I figured it out how to scroll the grid:
this.footerGrid.base.viewport.canvas.canvas.scrollLeft = param.scrollLeft;
Has there been any movement on getting this feature in? I need a row at the bottom that doesn't get sorted in with the rest of the data
@sontek https://github.com/adazzle/react-data-grid/blob/canary/packages/react-data-grid/src/DataGrid.tsx#L151
It is implemented in the canary branch.