-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[docs][DataGrid] Add pagination number formatting doc with a demo #19221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2f77871
82822f9
97fad14
da49d67
604eddc
129e79f
a1a21f3
ed3a262
0aaed86
4d799e3
c6afa37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||
import { frFR as locale } from '@mui/x-data-grid/locales'; | ||
|
||
const LOCALE = 'fr-FR'; // replace with your locale | ||
|
||
const formatNumber = (value) => { | ||
if (typeof Intl !== 'undefined' && Intl.NumberFormat) { | ||
try { | ||
const result = new Intl.NumberFormat(LOCALE).format(Number(value)); | ||
return result === 'NaN' ? String(value) : result; | ||
} catch { | ||
return String(value); | ||
} | ||
} | ||
return String(value); | ||
}; | ||
|
||
const paginationDisplayedRows = ({ from, to, count, estimated }) => { | ||
if (!estimated) { | ||
return `${formatNumber(from)}–${formatNumber(to)} sur ${ | ||
count !== -1 ? formatNumber(count) : `plus de ${formatNumber(to)}` | ||
}`; | ||
} | ||
const estimatedLabel = | ||
estimated && estimated > to | ||
? `environ ${formatNumber(estimated)}` | ||
: `plus de ${formatNumber(to)}`; | ||
return `${formatNumber(from)}–${formatNumber(to)} sur ${ | ||
count !== -1 ? formatNumber(count) : estimatedLabel | ||
}`; | ||
}; | ||
|
||
const localeText = { | ||
...locale.components.MuiDataGrid.defaultProps.localeText, | ||
paginationDisplayedRows, | ||
}; | ||
|
||
export default function PaginationNumberFormatting() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 4, | ||
maxColumns: 6, | ||
}); | ||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
{...data} | ||
pagination | ||
paginationMode="server" | ||
pageSizeOptions={[10000, 20000]} | ||
paginationModel={{ page: 1, pageSize: 10000 }} | ||
rowCount={1000000} | ||
localeText={localeText} | ||
/> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as React from 'react'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||
import { frFR as locale } from '@mui/x-data-grid/locales'; | ||
|
||
const LOCALE = 'fr-FR'; // replace with your locale | ||
|
||
const formatNumber = (value: number | string): string => { | ||
if (typeof Intl !== 'undefined' && Intl.NumberFormat) { | ||
try { | ||
const result = new Intl.NumberFormat(LOCALE).format(Number(value)); | ||
return result === 'NaN' ? String(value) : result; | ||
} catch { | ||
return String(value); | ||
} | ||
} | ||
return String(value); | ||
}; | ||
|
||
const paginationDisplayedRows = ({ | ||
from, | ||
to, | ||
count, | ||
estimated, | ||
}: { | ||
from: number; | ||
to: number; | ||
count: number; | ||
estimated?: number; | ||
}) => { | ||
if (!estimated) { | ||
return `${formatNumber(from)}–${formatNumber(to)} sur ${ | ||
count !== -1 ? formatNumber(count) : `plus de ${formatNumber(to)}` | ||
}`; | ||
} | ||
const estimatedLabel = | ||
estimated && estimated > to | ||
? `environ ${formatNumber(estimated)}` | ||
: `plus de ${formatNumber(to)}`; | ||
return `${formatNumber(from)}–${formatNumber(to)} sur ${ | ||
count !== -1 ? formatNumber(count) : estimatedLabel | ||
}`; | ||
}; | ||
|
||
const localeText = { | ||
...locale.components.MuiDataGrid.defaultProps.localeText, | ||
paginationDisplayedRows, | ||
}; | ||
|
||
export default function PaginationNumberFormatting() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 4, | ||
maxColumns: 6, | ||
}); | ||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
MBilalShafi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{...data} | ||
pagination | ||
paginationMode="server" | ||
pageSizeOptions={[10000, 20000]} | ||
paginationModel={{ page: 1, pageSize: 10000 }} | ||
rowCount={1000000} | ||
localeText={localeText} | ||
/> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<DataGridPro | ||
{...data} | ||
pagination | ||
paginationMode="server" | ||
pageSizeOptions={[10000, 20000]} | ||
paginationModel={{ page: 1, pageSize: 10000 }} | ||
rowCount={1000000} | ||
localeText={localeText} | ||
/> |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,25 +14,6 @@ In the following example, the label of the quick filter placeholder is customize | |||||
|
||||||
{{"demo": "CustomLocaleTextGrid.js", "bg": "inline"}} | ||||||
|
||||||
:::warning | ||||||
It's important to note that because the Data Grid uses components from the Material UI library, some translation keys need to be accessed using that component key. | ||||||
|
||||||
One example is the table pagination component used in the Data Grid footer when pagination is enabled. All the keys provided to the `MuiTablePagination` object are applied as props directly to the [Table Pagination](/material-ui/api/table-pagination/) component. | ||||||
|
||||||
```jsx | ||||||
<DataGrid | ||||||
{...data} | ||||||
localeText={{ | ||||||
MuiTablePagination: { | ||||||
labelDisplayedRows: ({ from, to, count }) => | ||||||
`${from} - ${to} of ${count === -1 ? `more than ${to}` : count}`, | ||||||
}, | ||||||
}} | ||||||
/> | ||||||
``` | ||||||
|
||||||
::: | ||||||
|
||||||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
## Locale text | ||||||
|
||||||
The default locale of MUI X is English (United States). | ||||||
|
@@ -114,6 +95,67 @@ The example below demonstrates how to use an RTL language (Arabic) with the Data | |||||
|
||||||
{{"demo": "DataGridRTL.js", "bg": "inline"}} | ||||||
|
||||||
## Pagination number formatting | ||||||
|
||||||
To format large numbers in the pagination component, customize the `paginationDisplayedRows` with the following code: | ||||||
|
||||||
```jsx | ||||||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||||||
|
import { DataGridPro } from '@mui/x-data-grid-pro'; | |
import { DataGrid } from '@mui/x-data-grid'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DataGrid won't work in this case because the pagination is not allowed to be more than 100.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add this info at the beginning of the section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DataGrid won't work in this case because the pagination is not allowed to be more than 100.
This would be news for me. People can paginate without limits in the MIT version. Let's remove the Pro component usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at this https://stackblitz.com/edit/3cvdpmsl?file=src%2FDemo.tsx

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michelengelen just want to confirm with you before merging this PR. Do you think it make sense for this section to target DataGrid Pro and above (given the error from the image above)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think it's better to switch to the community version. Let me update the demo.
Uh oh!
There was an error while loading. Please reload this page.