You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

177 lines
4.7 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 {Typography} from '@mui/material';
  8. // ==============================|| EVENT TABLE ||============================== //
  9. export function FiDataGrid({ rows, columns, sx, autoHeight,
  10. hideFooterSelectedRowCount, rowModesModel, editMode,
  11. pageSizeOptions, filterItems,
  12. ...props }) {
  13. const intl = useIntl();
  14. const [_rows, set_rows] = React.useState([]);
  15. const [_columns, set_columns] = React.useState([]);
  16. const [_rowModesModel, set_rowModesModel] = React.useState({});
  17. const [_editMode, set_editMode] = React.useState("row");
  18. const [_pageSizeOptions, set_pageSizeOptions] = React.useState([10, 25, 50]);
  19. const [_filterItems, set_filterItems] = React.useState([]);
  20. const [_autoHeight, set_autoHeight] = React.useState(true);
  21. const [myHideFooterSelectedRowCount, setMyHideFooterSelectedRowCount] = React.useState(true);
  22. const [_sx, set_sx] = React.useState({
  23. padding: "4 2 4 2",
  24. // boxShadow: 1,
  25. // border: 1,
  26. // borderColor: '#DDD',
  27. '& .MuiDataGrid-cell': {
  28. borderTop: 1,
  29. borderBottom: 1,
  30. borderColor: "#EEE",
  31. },
  32. '& .MuiDataGrid-footerContainer': {
  33. border: 1,
  34. borderColor: "#EEE"
  35. },
  36. });
  37. React.useEffect(() => {
  38. if (sx) {
  39. set_sx(sx);
  40. }
  41. if (hideFooterSelectedRowCount) {
  42. setMyHideFooterSelectedRowCount(hideFooterSelectedRowCount);
  43. }
  44. if (rowModesModel) {
  45. set_rowModesModel(rowModesModel)
  46. }
  47. if (rows) {
  48. set_rows(rows)
  49. }
  50. if (columns) {
  51. set_columns(columns)
  52. }
  53. if (pageSizeOptions) {
  54. set_pageSizeOptions(pageSizeOptions)
  55. }
  56. if(autoHeight != undefined){
  57. set_autoHeight(autoHeight)
  58. }
  59. if(editMode){
  60. set_editMode(editMode);
  61. }
  62. }, []);
  63. React.useEffect(() => {
  64. if (sx) {
  65. set_sx(sx);
  66. }
  67. }, [sx]);
  68. React.useEffect(() => {
  69. if (hideFooterSelectedRowCount) {
  70. setMyHideFooterSelectedRowCount(hideFooterSelectedRowCount);
  71. }
  72. }, [hideFooterSelectedRowCount]);
  73. React.useEffect(() => {
  74. if (rowModesModel) {
  75. set_rowModesModel(rowModesModel)
  76. }
  77. }, [rowModesModel]);
  78. React.useEffect(() => {
  79. if (rows) {
  80. set_rows(rows)
  81. }
  82. }, [rows]);
  83. React.useEffect(() => {
  84. if (columns) {
  85. set_columns(columns)
  86. }
  87. }, [columns]);
  88. React.useEffect(() => {
  89. if (pageSizeOptions) {
  90. set_pageSizeOptions(pageSizeOptions)
  91. }
  92. }, [pageSizeOptions]);
  93. React.useEffect(() => {
  94. if(autoHeight != undefined){
  95. set_autoHeight(autoHeight)
  96. }
  97. }, [autoHeight]);
  98. React.useEffect(() => {
  99. if(editMode){
  100. set_editMode(editMode);
  101. }
  102. }, [editMode]);
  103. React.useEffect(() => {
  104. if(filterItems){
  105. set_filterItems(filterItems);
  106. }
  107. }, [filterItems]);
  108. function CustomNoRowsOverlay() {
  109. return (
  110. <GridOverlay>
  111. <Typography variant="body1">
  112. <FormattedMessage id="rowsPerPage" />
  113. </Typography>
  114. </GridOverlay>
  115. );
  116. }
  117. const CustomPagination = (props) => {
  118. const { pagination } = props;
  119. const { page, pageSize, rowCount } = pagination;
  120. const startIndex = page * pageSize + 1;
  121. const endIndex = Math.min((page + 1) * pageSize, rowCount);
  122. return (
  123. <div>
  124. <div>{`${startIndex}-${endIndex} YES ${rowCount}`}</div>
  125. {/* Render other pagination controls */}
  126. </div>
  127. );
  128. };
  129. return (
  130. <DataGrid
  131. {...props}
  132. rows={_rows}
  133. columns={_columns}
  134. disableColumnMenu
  135. rowModesModel={_rowModesModel}
  136. pageSizeOptions={_pageSizeOptions}
  137. editMode={_editMode}
  138. autoHeight={_autoHeight}
  139. hideFooterSelectedRowCount={myHideFooterSelectedRowCount}
  140. filterModel={{items:_filterItems}}
  141. sx={_sx}
  142. slots={{
  143. noRowsOverlay: () => (
  144. CustomNoRowsOverlay()
  145. )
  146. }}
  147. components={{
  148. Pagination: CustomPagination,
  149. }}
  150. componentsProps={{
  151. pagination: {
  152. labelRowsPerPage: intl.formatMessage({id: 'rowsPerPage'}),
  153. }
  154. }}
  155. />
  156. );
  157. }