Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

129 строки
4.3 KiB

  1. // material-ui
  2. import * as React from 'react';
  3. import {
  4. DataGrid, GridOverlay,
  5. } from "@mui/x-data-grid";
  6. import {FormattedMessage, useIntl} from "react-intl";
  7. import {TablePagination, Typography} from '@mui/material';
  8. // ==============================|| EVENT TABLE ||============================== //
  9. export function FiDataGrid({ rows, columns, sx, autoHeight,
  10. hideFooterSelectedRowCount, rowModesModel, editMode,
  11. pageSizeOptions, filterItems, customPageSize, ...props }) {
  12. const intl = useIntl();
  13. const [_rows, set_rows] = React.useState([]);
  14. const [_columns, set_columns] = React.useState([]);
  15. const [_rowModesModel, set_rowModesModel] = React.useState({});
  16. const [_editMode, set_editMode] = React.useState("row");
  17. const [_pageSizeOptions, set_pageSizeOptions] = React.useState([10, 25, 50]);
  18. const [_filterItems, set_filterItems] = React.useState([]);
  19. const [page, setPage] = React.useState(0);
  20. const [pageSize, setPageSize] = React.useState(10);
  21. const [_autoHeight, set_autoHeight] = React.useState(true);
  22. const [myHideFooterSelectedRowCount, setMyHideFooterSelectedRowCount] = React.useState(true);
  23. const [_sx, set_sx] = React.useState({
  24. padding: "4 2 4 2",
  25. '& .MuiDataGrid-cell': {
  26. borderTop: 1,
  27. borderBottom: 1,
  28. borderColor: "#EEE",
  29. },
  30. '& .MuiDataGrid-footerContainer': {
  31. border: 1,
  32. borderColor: "#EEE"
  33. },
  34. });
  35. React.useEffect(() => {
  36. if (sx) {
  37. set_sx(sx);
  38. }
  39. if (hideFooterSelectedRowCount) {
  40. setMyHideFooterSelectedRowCount(hideFooterSelectedRowCount);
  41. }
  42. if (rowModesModel) {
  43. set_rowModesModel(rowModesModel)
  44. }
  45. if (rows) {
  46. set_rows(rows)
  47. }
  48. if (columns) {
  49. set_columns(columns)
  50. }
  51. if (pageSizeOptions) {
  52. set_pageSizeOptions(pageSizeOptions)
  53. }
  54. if(autoHeight !== undefined){
  55. set_autoHeight(autoHeight)
  56. }
  57. if(editMode){
  58. set_editMode(editMode);
  59. }
  60. if(filterItems){
  61. set_filterItems(filterItems);
  62. }
  63. if(customPageSize){
  64. setPageSize(customPageSize);
  65. }
  66. }, [sx, hideFooterSelectedRowCount, rowModesModel, rows, columns, pageSizeOptions, autoHeight, editMode, filterItems, customPageSize]);
  67. const handleChangePage = (event, newPage) => {
  68. setPage(newPage);
  69. };
  70. const handleChangePageSize = (event) => {
  71. setPageSize(parseInt(event.target.value, 10));
  72. setPage(0);
  73. };
  74. const startIndex = page * pageSize;
  75. const endIndex = (page + 1) * pageSize;
  76. const slicedRows = _rows.slice(startIndex, endIndex);
  77. function CustomNoRowsOverlay() {
  78. return (
  79. <GridOverlay>
  80. <Typography variant="body1">
  81. <FormattedMessage id="noRecordFound" />
  82. </Typography>
  83. </GridOverlay>
  84. );
  85. }
  86. return (
  87. <DataGrid
  88. {...props}
  89. rows={slicedRows}
  90. columns={_columns}
  91. disableColumnMenu
  92. rowModesModel={_rowModesModel}
  93. pageSizeOptions={_pageSizeOptions}
  94. editMode={_editMode}
  95. autoHeight={_autoHeight}
  96. hideFooterSelectedRowCount={myHideFooterSelectedRowCount}
  97. filterModel={{ items: _filterItems }}
  98. sx={_sx}
  99. components={{
  100. noRowsOverlay: CustomNoRowsOverlay,
  101. Pagination: () => (
  102. <TablePagination
  103. count={_rows.length}
  104. page={page}
  105. rowsPerPage={pageSize}
  106. rowsPerPageOptions={[5,10,20,100]}
  107. labelDisplayedRows={({ from, to, count }) =>
  108. `${from}-${to} ${intl.formatMessage({ id: "of" })} ${count}`
  109. }
  110. labelRowsPerPage={intl.formatMessage({ id: "rowsPerPage" }) + ":"}
  111. onPageChange={handleChangePage}
  112. onRowsPerPageChange={handleChangePageSize}
  113. />
  114. ),
  115. }}
  116. />
  117. );
  118. }