Skip to content

Commit

Permalink
Fixed #2636 - Add possibility to reset scroll in DataTable
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Feb 18, 2022
1 parent bcbc2c2 commit 37b2072
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions components/doc/datatable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3706,6 +3706,11 @@ export const DataTableStateDemo = () => {
<td>-</td>
<td>Resets column order when reorderableColumns is enabled.</td>
</tr>
<tr>
<td>resetScroll</td>
<td>-</td>
<td>Resets scroll position.</td>
</tr>
<tr>
<td>restoreTableState</td>
<td>state</td>
Expand Down
1 change: 1 addition & 0 deletions components/lib/datatable/DataTable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export interface DataTableProps {

export declare class DataTable extends React.Component<DataTableProps, any> {
public reset(): void;
public resetScroll(): void;
public exportCSV(options?: { selectionOnly: boolean }): void;
public filter<T>(value: T, field: string, mode: DataTableFilterMatchModeType, index?: number): void;
public resetColumnOrder(): void;
Expand Down
9 changes: 8 additions & 1 deletion components/lib/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,13 @@ export class DataTable extends Component {
this.setState(state);
}

resetScroll() {
if (this.wrapper) {
const scrollableContainer = !this.isVirtualScrollerDisabled() ? DomHandler.findSingle(this.wrapper, '.p-virtualscroller') : this.wrapper;
scrollableContainer.scrollTo(0, 0);
}
}

resetColumnOrder() {
const columns = this.getColumns(true);
let columnOrder = [];
Expand Down Expand Up @@ -1690,7 +1697,7 @@ export class DataTable extends Component {
const virtualScrollerOptions = this.props.virtualScrollerOptions || {};

return (
<div className="p-datatable-wrapper" style={{ maxHeight: isVirtualScrollerDisabled ? this.props.scrollHeight : null }}>
<div ref={el => this.wrapper = el} className="p-datatable-wrapper" style={{ maxHeight: isVirtualScrollerDisabled ? this.props.scrollHeight : null }}>
<VirtualScroller {...virtualScrollerOptions} items={processedData} columns={columns} scrollHeight={this.props.scrollHeight}
disabled={isVirtualScrollerDisabled} loaderDisabled showSpacer={false}
contentTemplate={(options) => {
Expand Down

0 comments on commit 37b2072

Please sign in to comment.