react-data-grid icon indicating copy to clipboard operation
react-data-grid copied to clipboard

Feature request: Total (footer) row

Open yairniz opened this issue 9 years ago • 20 comments

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.

yairniz avatar Mar 16 '17 13:03 yairniz

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.

OracPrime avatar Mar 23 '17 10:03 OracPrime

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 avatar Mar 28 '17 22:03 OracPrime

@OracPrime Can you provide a snip please? I'd be interested to know how you managed this.

davewthompson avatar Mar 29 '17 11:03 davewthompson

Sure - up against a work deadline at the moment, but give me a couple of days.

OracPrime avatar Mar 29 '17 11:03 OracPrime

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>;
    }

}

OracPrime avatar Mar 30 '17 22:03 OracPrime

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.

OracPrime avatar Mar 31 '17 09:03 OracPrime

@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} />;
}

}; `

OracPrime avatar Apr 04 '17 15:04 OracPrime

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) { }
    }
} 

yairniz avatar Apr 05 '17 11:04 yairniz

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 avatar Apr 08 '17 10:04 nextofsearch

@nextofsearch I think @yairniz meant that horizontal scrolling would be messed up if the two grids weren't linked with a ref.

dijonkitchen avatar Sep 21 '17 18:09 dijonkitchen

@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

terolepisto avatar Dec 21 '17 07:12 terolepisto

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.

stale[bot] avatar Jul 12 '18 13:07 stale[bot]

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.

hanming2033 avatar Jul 26 '18 11:07 hanming2033

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 avatar Jan 22 '19 21:01 JanBN

@JanBN The solution consisted of:

  1. Creating another grid with the same columns definition.
  2. Listening on the scroll event of the main grid.
  3. Using setScrollLeft(maingrid.scrollLeft) on the total grid row.

yairniz avatar Jan 23 '19 11:01 yairniz

@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 ?

JanBN avatar Jan 23 '19 11:01 JanBN

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;)

yairniz avatar Jan 23 '19 11:01 yairniz

I figured it out how to scroll the grid: this.footerGrid.base.viewport.canvas.canvas.scrollLeft = param.scrollLeft;

JanBN avatar Jan 25 '19 16:01 JanBN

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 avatar Nov 16 '19 21:11 sontek

@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.

qili26 avatar Nov 17 '19 00:11 qili26