diff --git a/src/components/paginator/Paginator.js b/src/components/paginator/Paginator.js index d73d0eac0b..29be0ce6ac 100644 --- a/src/components/paginator/Paginator.js +++ b/src/components/paginator/Paginator.js @@ -142,16 +142,21 @@ export class Paginator extends Component { } onRowsChange(event) { - this.changePage(0, event.value); + const rows = event.value; + + this.isRowChanged = rows !== this.props.rows; + this.changePage(0, rows); } componentDidUpdate(prevProps, prevState) { - if (this.props.rows !== prevProps.rows) { + if (this.props.rows !== prevProps.rows && !this.isRowChanged) { this.changePage(0, this.props.rows); } else if (this.getPage() > 0 && prevProps.totalRecords !== this.props.totalRecords && this.props.first >= this.props.totalRecords) { this.changePage((this.getPageCount() - 1) * this.props.rows, this.props.rows); } + + this.isRowChanged = false; } renderElement(key, template) { diff --git a/src/showcase/datatable/DataTablePaginatorDemo.js b/src/showcase/datatable/DataTablePaginatorDemo.js index 58a3608a38..855cf3e1ca 100644 --- a/src/showcase/datatable/DataTablePaginatorDemo.js +++ b/src/showcase/datatable/DataTablePaginatorDemo.js @@ -1,774 +1,166 @@ -import React, { Component } from 'react'; +import React, { useEffect, useState } from 'react'; import { DataTable } from '../../components/datatable/DataTable'; import { Column } from '../../components/column/Column'; import { CustomerService } from '../service/CustomerService'; -import { TabView } from '../../components/tabview/TabView'; -import { Button } from '../../components/button/Button'; import { Ripple } from '../../components/ripple/Ripple'; import { Dropdown } from '../../components/dropdown/Dropdown'; import { InputText } from '../../components/inputtext/InputText'; -import { useLiveEditorTabs } from '../liveeditor/LiveEditor'; -import { AppInlineHeader } from '../../AppInlineHeader'; import { classNames } from '../../components/utils/ClassNames'; -import AppDemoActions from '../../AppDemoActions'; -export class DataTablePaginatorDemo extends Component { - - constructor(props) { - super(props); - - this.state = { - customers1: [], - customers2: [], - customers3: [], - first1: 0, - rows1: 10, - first2: 0, - rows2: 10, - currentPage: 1, - pageInputTooltip: 'Press \'Enter\' key to go to this page.' - }; - - this.customerService = new CustomerService(); - - this.onCustomPage1 = this.onCustomPage1.bind(this); - this.onCustomPage2 = this.onCustomPage2.bind(this); - this.onPageInputKeyDown = this.onPageInputKeyDown.bind(this); - this.onPageInputChange = this.onPageInputChange.bind(this); - } - - onCustomPage1(event) { - this.setState({ - first1: event.first, - rows1: event.rows, - currentPage: event.page + 1 - }); - } - - onCustomPage2(event) { - this.setState({ - first2: event.first, - rows2: event.rows - }); - } - - onPageInputKeyDown(event, options) { - if (event.key === 'Enter') { - const page = parseInt(this.state.currentPage); - if (page < 0 || page > options.totalPages) { - this.setState({ pageInputTooltip: `Value must be between 1 and ${options.totalPages}.`}) - } - else { - const first = this.state.currentPage ? options.rows * (page - 1) : 0; - - this.setState({ first1: first, pageInputTooltip: 'Press \'Enter\' key to go to this page.' }); - } - } - } - - onPageInputChange(event) { - this.setState({ currentPage: event.target.value }); - } - - componentDidMount() { - this.customerService.getCustomersLarge().then(data => this.setState({ customers1: data })); - this.customerService.getCustomersLarge().then(data => this.setState({ customers2: data })); - this.customerService.getCustomersLarge().then(data => this.setState({ customers3: data })); - } - - render() { - const paginatorLeft = - ) - }, - 'NextPageLink': (options) => { - return ( - - ) - }, - 'PageLinks': (options) => { - if ((options.view.startPage === options.page && options.view.startPage !== 0) || (options.view.endPage === options.page && options.page + 1 !== options.totalPages)) { - const className = classNames(options.className, { 'p-disabled': true }); - - return ...; - } - - return ( - - ) - }, - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 }, - { label: 'All', value: options.totalRecords } - ]; - - return ; - }, - 'CurrentPageReport': (options) => { - return ( - - Go to this.onPageInputKeyDown(e, options)} onChange={this.onPageInputChange}/> - - ) - } - }; - const template2 = { - layout: 'RowsPerPageDropdown CurrentPageReport PrevPageLink NextPageLink', - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 } - ]; - - return ( - <> - Items per page: - - - ); - }, - 'CurrentPageReport': (options) => { - return ( - - {options.first} - {options.last} of {options.totalRecords} - - ) - } - }; - - return ( -
-
- -

DataTable Paginator

-

Pagination is enabled by setting paginator property to true, rows attribute defines the number of rows per page and pageLinks specify the the number of page links to display.

-
- -
- -
-
-
Basic
- - - - - - - -
Custom Paginator Template
- - - - - - - - - - - - - -
-
- - -
- ); - } -} - -export class DataTablePaginatorDemoDoc extends Component { - - constructor(props) { - super(props); - - this.sources = { - 'class': { - tabName: 'Class Source', - content: ` -import React, { Component } from 'react'; -import { DataTable } from 'primereact/datatable'; -import { Column } from 'primereact/column'; -import { Button } from 'primereact/button'; -import { Ripple } from 'primereact/ripple'; -import { Dropdown } from 'primereact/dropdown'; -import { InputText } from 'primereact/inputtext'; -import { CustomerService } from '../service/CustomerService'; -import { Button } from 'primereact/button'; -import { classNames } from 'primereact/utils'; - -export class DataTablePaginatorDemo extends Component { - - constructor(props) { - super(props); - - this.state = { - customers1: [], - customers2: [], - customers3: [], - first1: 0, - rows1: 10, - first2: 0, - rows2: 10, - currentPage: 1, - pageInputTooltip: 'Press \\'Enter\\' key to go to this page.' - }; - - this.customerService = new CustomerService(); - - this.onCustomPage1 = this.onCustomPage1.bind(this); - this.onCustomPage2 = this.onCustomPage2.bind(this); - this.onPageInputKeyDown = this.onPageInputKeyDown.bind(this); - this.onPageInputChange = this.onPageInputChange.bind(this); - } - - onCustomPage1(event) { - this.setState({ - first1: event.first, - rows1: event.rows, - currentPage: event.page + 1 - }); - } - - onCustomPage2(event) { - this.setState({ - first2: event.first, - rows2: event.rows - }); - } - - onPageInputKeyDown(event, options) { - if (event.key === 'Enter') { - const page = parseInt(this.state.currentPage); - if (page < 0 || page > options.totalPages) { - this.setState({ pageInputTooltip: \`Value must be between 1 and \${options.totalPages}.\`}) - } - else { - const first = this.state.currentPage ? options.rows * (page - 1) : 0; - - this.setState({ first1: first, pageInputTooltip: 'Press \\'Enter\\' key to go to this page.' }); - } - } - } - - onPageInputChange(event) { - this.setState({ currentPage: event.target.value }); - } - - componentDidMount() { - this.customerService.getCustomersLarge().then(data => this.setState({ customers1: data })); - this.customerService.getCustomersLarge().then(data => this.setState({ customers2: data })); - this.customerService.getCustomersLarge().then(data => this.setState({ customers3: data })); - } - - render() { - const paginatorLeft = - ) - }, - 'NextPageLink': (options) => { - return ( - - ) - }, - 'PageLinks': (options) => { - if ((options.view.startPage === options.page && options.view.startPage !== 0) || (options.view.endPage === options.page && options.page + 1 !== options.totalPages)) { - const className = classNames(options.className, { 'p-disabled': true }); - - return ...; - } - - return ( - - ) - }, - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 }, - { label: 'All', value: options.totalRecords } - ]; - - return ; - }, - 'CurrentPageReport': (options) => { - return ( - - Go to this.onPageInputKeyDown(e, options)} onChange={this.onPageInputChange}/> - - ) - } - }; - const template2 = { - layout: 'RowsPerPageDropdown CurrentPageReport PrevPageLink NextPageLink', - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 } - ]; - - return ( - <> - Items per page: - - - ); - }, - 'CurrentPageReport': (options) => { - return ( - - {options.first} - {options.last} of {options.totalRecords} - - ) - } - }; - - return ( -
-
-
Basic
- - - - - - - -
Custom Paginator Template
- - - - - - - - - - - - - -
-
- ); - } -} - ` - }, - 'hooks': { - tabName: 'Hooks Source', - content: ` -import React, { useState, useEffect } from 'react'; -import { DataTable } from 'primereact/datatable'; -import { Column } from 'primereact/column'; -import { Button } from 'primereact/button'; -import { Ripple } from 'primereact/ripple'; -import { Dropdown } from 'primereact/dropdown'; -import { InputText } from 'primereact/inputtext'; -import { CustomerService } from '../service/CustomerService'; -import { Button } from 'primereact/button'; -import { classNames } from 'primereact/utils'; - -const DataTablePaginatorDemo = () => { - const [customers1, setCustomers1] = useState([]); +export const DataTablePaginatorDemo = () => { const [customers2, setCustomers2] = useState([]); - const [customers3, setCustomers3] = useState([]); const [first1, setFirst1] = useState(0); const [rows1, setRows1] = useState(10); - const [first2, setFirst2] = useState(0); - const [rows2, setRows2] = useState(10); const [currentPage, setCurrentPage] = useState(1); - const [pageInputTooltip, setPageInputTooltip] = useState('Press \\'Enter\\' key to go to this page.'); - - const customerService = new CustomerService(); - - const onCustomPage1 = (event) => { - setFirst1(event.first); - setRows1(event.rows); - setCurrentPage(event.page + 1); - } - - const onCustomPage2 = (event) => { - setFirst2(event.first); - setRows2(event.rows); - } - - const onPageInputKeyDown = (event, options) => { - if (event.key === 'Enter') { - const page = parseInt(currentPage); - if (page < 0 || page > options.totalPages) { - setPageInputTooltip(\`Value must be between 1 and \${options.totalPages}.\`); - } - else { - const first = currentPage ? options.rows * (page - 1) : 0; - - setFirst1(first); - setPageInputTooltip('Press \\'Enter\\' key to go to this page.'); - } - } - } - - const onPageInputChange = (event) => { - setCurrentPage(event.target.value); - } - - useEffect(() => { - customerService.getCustomersLarge().then(data => setCustomers1(data)); - customerService.getCustomersLarge().then(data => setCustomers2(data)); - customerService.getCustomersLarge().then(data => setCustomers3(data)); - }, []); // eslint-disable-line react-hooks/exhaustive-deps - - const paginatorLeft = - ) - }, - 'NextPageLink': (options) => { - return ( - - ) - }, - 'PageLinks': (options) => { - if ((options.view.startPage === options.page && options.view.startPage !== 0) || (options.view.endPage === options.page && options.page + 1 !== options.totalPages)) { - const className = classNames(options.className, { 'p-disabled': true }); - - return ...; - } - - return ( - - ) - }, - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 }, - { label: 'All', value: options.totalRecords } - ]; - - return ; - }, - 'CurrentPageReport': (options) => { - return ( - - Go to onPageInputKeyDown(e, options)} onChange={onPageInputChange}/> - - ) - } - }; - const template2 = { - layout: 'RowsPerPageDropdown CurrentPageReport PrevPageLink NextPageLink', - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 } - ]; - - return ( - <> - Items per page: - - - ); - }, - 'CurrentPageReport': (options) => { - return ( - - {options.first} - {options.last} of {options.totalRecords} - - ) - } - }; - - return ( -
-
-
Basic
- - - - - - - -
Custom Paginator Template
- - - - - - - - - - - - - -
-
+ const [pageInputTooltip, setPageInputTooltip] = useState( + "Press 'Enter' key to go to this page." ); -} - ` - }, - 'ts': { - tabName: 'TS Source', - content: ` -import React, { useState, useEffect } from 'react'; -import { DataTable } from 'primereact/datatable'; -import { Column } from 'primereact/column'; -import { Button } from 'primereact/button'; -import { Ripple } from 'primereact/ripple'; -import { Dropdown } from 'primereact/dropdown'; -import { InputText } from 'primereact/inputtext'; -import { CustomerService } from '../service/CustomerService'; -import { Button } from 'primereact/button'; -import { classNames } from 'primereact/utils'; - -const DataTablePaginatorDemo = () => { - const [customers1, setCustomers1] = useState([]); - const [customers2, setCustomers2] = useState([]); - const [customers3, setCustomers3] = useState([]); - const [first1, setFirst1] = useState(0); - const [rows1, setRows1] = useState(10); - const [first2, setFirst2] = useState(0); - const [rows2, setRows2] = useState(10); - const [currentPage, setCurrentPage] = useState(1); - const [pageInputTooltip, setPageInputTooltip] = useState('Press \\'Enter\\' key to go to this page.'); const customerService = new CustomerService(); const onCustomPage1 = (event) => { - setFirst1(event.first); - setRows1(event.rows); - setCurrentPage(event.page + 1); - } + console.log("onCustomPage1"); + setFirst1(event.first); + setRows1(event.rows); + setCurrentPage(event.page + 1); + }; - const onCustomPage2 = (event) => { - setFirst2(event.first); - setRows2(event.rows); - } const onPageInputKeyDown = (event, options) => { - if (event.key === 'Enter') { - const page = parseInt(currentPage); - if (page < 0 || page > options.totalPages) { - setPageInputTooltip(\`Value must be between 1 and \${options.totalPages}.\`); - } - else { - const first = currentPage ? options.rows * (page - 1) : 0; - - setFirst1(first); - setPageInputTooltip('Press \\'Enter\\' key to go to this page.'); - } + if (event.key === "Enter") { + const page = parseInt(currentPage, 10); + if (page < 0 || page > options.totalPages) { + setPageInputTooltip( + `Value must be between 1 and ${options.totalPages}.` + ); + } else { + const first = currentPage ? options.rows * (page - 1) : 0; + + setFirst1(first); + setPageInputTooltip("Press 'Enter' key to go to this page."); } - } + } + }; const onPageInputChange = (event) => { - setCurrentPage(event.target.value); - } + setCurrentPage(event.target.value); + }; useEffect(() => { - customerService.getCustomersLarge().then(data => setCustomers1(data)); - customerService.getCustomersLarge().then(data => setCustomers2(data)); - customerService.getCustomersLarge().then(data => setCustomers3(data)); + customerService.getCustomersLarge().then((data) => setCustomers2(data)); }, []); // eslint-disable-line react-hooks/exhaustive-deps - const paginatorLeft = - ) - }, - 'NextPageLink': (options) => { - return ( - - ) - }, - 'PageLinks': (options) => { - if ((options.view.startPage === options.page && options.view.startPage !== 0) || (options.view.endPage === options.page && options.page + 1 !== options.totalPages)) { - const className = classNames(options.className, { 'p-disabled': true }); - - return ...; - } - - return ( - - ) - }, - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 }, - { label: 'All', value: options.totalRecords } - ]; - - return ; - }, - 'CurrentPageReport': (options) => { - return ( - - Go to onPageInputKeyDown(e, options)} onChange={onPageInputChange}/> - - ) + layout: + "PrevPageLink PageLinks NextPageLink RowsPerPageDropdown CurrentPageReport", + PrevPageLink: (options) => { + return ( + + ); + }, + NextPageLink: (options) => { + return ( + + ); + }, + PageLinks: (options) => { + if ( + (options.view.startPage === options.page && + options.view.startPage !== 0) || + (options.view.endPage === options.page && + options.page + 1 !== options.totalPages) + ) { + const className = classNames(options.className, { "p-disabled": true }); + + return ( + + ... + + ); } - }; - const template2 = { - layout: 'RowsPerPageDropdown CurrentPageReport PrevPageLink NextPageLink', - 'RowsPerPageDropdown': (options) => { - const dropdownOptions = [ - { label: 10, value: 10 }, - { label: 20, value: 20 }, - { label: 50, value: 50 } - ]; - return ( - <> - Items per page: - - - ); - }, - 'CurrentPageReport': (options) => { - return ( - - {options.first} - {options.last} of {options.totalRecords} - - ) - } + return ( + + ); + }, + RowsPerPageDropdown: (options) => { + const dropdownOptions = [ + { label: 10, value: 10 }, + { label: 20, value: 20 }, + { label: 50, value: 50 }, + { label: "All", value: options.totalRecords } + ]; + + return ( + + ); + }, + CurrentPageReport: (options) => { + return ( + + Go to{" "} + onPageInputKeyDown(e, options)} + onChange={onPageInputChange} + /> + + ); + } }; return ( -
-
-
Basic
- - - - - - - -
Custom Paginator Template
- - - - - - - - - - - - - -
+
+
+
Custom Paginator Template
+ + + + + +
+
); -} - ` - } - } - } - - shouldComponentUpdate() { - return false; - } - - render() { - return ( -
- - { - useLiveEditorTabs({ name: 'DataTablePaginatorDemo', sources: this.sources, service: 'CustomerService', data: 'customers-large' }) - } - -
- ) - } -} + };