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.
 
 

262 lines
9.3 KiB

  1. // material-ui
  2. import { useState, useEffect } from 'react';
  3. import { Box } from "@mui/material";
  4. import {
  5. DataGrid, GridOverlay,
  6. } from "@mui/x-data-grid";
  7. import * as HttpUtils from "utils/HttpUtils";
  8. import { FormattedMessage, useIntl } from "react-intl";
  9. import { TablePagination, Typography } from '@mui/material';
  10. import { getSearchCriteria, checkSearchCriteriaPath } from "auth/utils";
  11. // ==============================|| EVENT TABLE ||============================== //
  12. export function FiDataGrid({ rows, columns, sx, autoHeight = true,
  13. hideFooterSelectedRowCount, rowModesModel, editMode,
  14. pageSizeOptions, filterItems, customPageSize, doLoad, applyGridOnReady, applySearch,
  15. tab, height, maxHeight, pagination = true, ...props }) {
  16. const intl = useIntl();
  17. const [_rows, set_rows] = useState([]);
  18. const [_doLoad, set_doLoad] = useState({});
  19. const [_columns, set_columns] = useState([]);
  20. const [_rowModesModel, set_rowModesModel] = useState({});
  21. const [_editMode, set_editMode] = useState("row");
  22. const [_pageSizeOptions, set_pageSizeOptions] = useState([10]);
  23. const [_filterItems, set_filterItems] = useState([]);
  24. const [loading, setLoading] = useState(false);
  25. const [page, setPage] = useState(0);
  26. const [pageSize, setPageSize] = useState(10);
  27. // const [_autoHeight, set_autoHeight] = useState(true);
  28. const [myHideFooterSelectedRowCount, setMyHideFooterSelectedRowCount] = useState(true);
  29. const [_sx, set_sx] = useState({
  30. padding: "4 2 4 2",
  31. lineHeight: "normal",
  32. '& .MuiDataGrid-cell': {
  33. borderTop: 1,
  34. borderBottom: 1,
  35. borderColor: "#EEE",
  36. },
  37. '& .MuiDataGrid-footerContainer': {
  38. border: 1,
  39. borderColor: "#EEE"
  40. },
  41. "& .MuiDataGrid-columnHeaderTitle": {
  42. whiteSpace: "normal",
  43. lineHeight: "normal"
  44. },
  45. "& .MuiDataGrid-columnHeader": {
  46. // Forced to use important since overriding inline styles
  47. height: "unset !important"
  48. },
  49. });
  50. const effectiveAutoHeight = autoHeight && !height && !maxHeight;
  51. const containerSx = {
  52. width: '100%',
  53. ...(height ? { height } : {}),
  54. ...(maxHeight ? { maxHeight, height: '100%' } : {}),
  55. overflow: 'hidden',
  56. };
  57. const [rowCount, setRowCount] = useState(0);
  58. useEffect(() => {
  59. if (doLoad !== undefined && Object.keys(doLoad).length>0 ){
  60. if(applySearch!=undefined){
  61. if (Object.keys(getSearchCriteria(window.location.pathname)).length>0){
  62. const localStorageSearchCriteria = getSearchCriteria(window.location.pathname)
  63. // console.log(localStorageSearchCriteria)
  64. if(localStorageSearchCriteria.start!=undefined){
  65. // console.log(localStorageSearchCriteria)
  66. setPage(localStorageSearchCriteria.start/pageSize);
  67. }
  68. }
  69. }else{
  70. setPage(0);
  71. setPageSize(parseInt(event.target.value, 10));
  72. }
  73. set_doLoad(doLoad);
  74. setLoading(true)
  75. }
  76. }, [doLoad]);
  77. useEffect(() => {
  78. getDataList();
  79. }, [_doLoad, page]);
  80. useEffect(() => {
  81. if (sx) {
  82. set_sx(sx);
  83. }
  84. if (hideFooterSelectedRowCount) {
  85. setMyHideFooterSelectedRowCount(hideFooterSelectedRowCount);
  86. }
  87. if (rowModesModel) {
  88. set_rowModesModel(rowModesModel)
  89. }
  90. if (rows) {
  91. set_rows(rows)
  92. setRowCount(rows.length)
  93. }
  94. if (columns) {
  95. set_columns(columns)
  96. }
  97. if (pageSizeOptions) {
  98. set_pageSizeOptions(pageSizeOptions)
  99. }
  100. // if (autoHeight !== undefined) {
  101. // set_autoHeight(autoHeight)
  102. // }
  103. if (editMode) {
  104. set_editMode(editMode);
  105. }
  106. if (filterItems) {
  107. set_filterItems(filterItems);
  108. }
  109. if (customPageSize) {
  110. setPageSize(customPageSize);
  111. }
  112. // console.log(_doLoad)
  113. if (_doLoad !== undefined && Object.keys(_doLoad).length==0 ){
  114. setLoading(false)
  115. if (applyGridOnReady !== undefined){
  116. applyGridOnReady(false)
  117. }
  118. }
  119. }, [sx, hideFooterSelectedRowCount, rowModesModel, rows, columns, pageSizeOptions, autoHeight, editMode, filterItems, customPageSize]);
  120. const handleChangePage = (event, newPage) => {
  121. setPage(newPage);
  122. };
  123. const handleChangePageSize = (event) => {
  124. setPageSize(parseInt(event.target.value, 10));
  125. setPage(0);
  126. };
  127. function CustomNoRowsOverlay() {
  128. return (
  129. <GridOverlay
  130. sx={{
  131. width: "100%",
  132. justifyContent: "flex-start", // align overlay to left
  133. pl: 2, // padding-left to match grid cells
  134. }}
  135. >
  136. <Typography variant="body1" sx={{ textAlign: "left", width: "100%" }}>
  137. <FormattedMessage id="noRecordFound" />
  138. </Typography>
  139. </GridOverlay>
  140. );
  141. }
  142. function getDataList() {
  143. // console.log(Object.keys(_doLoad.params).length > 0)
  144. // console.log(Object.keys(_doLoad.params).length > 0)
  145. if (_doLoad?.url == null){
  146. setLoading(false)
  147. return;
  148. }
  149. if (_doLoad.params == undefined) return;
  150. if (_doLoad.params.searchCriteria !== undefined) return;
  151. if (_doLoad.params == null) _doLoad.params = {};
  152. _doLoad.params.start = page * pageSize;
  153. _doLoad.params.limit = pageSize;
  154. if(checkSearchCriteriaPath(window.location.pathname)){
  155. if(window.location.pathname === "/publicNotice"){
  156. if (tab != undefined && tab ==="application"){
  157. localStorage.setItem('searchCriteria', JSON.stringify({path:window.location.pathname,data:_doLoad.params}))
  158. }
  159. }else if (window.location.pathname != "/publicNotice"){
  160. localStorage.setItem('searchCriteria', JSON.stringify({path:window.location.pathname,data:_doLoad.params}))
  161. }
  162. }
  163. HttpUtils.get({
  164. url: _doLoad.url,
  165. params: _doLoad.params,
  166. onSuccess: function (responseData) {
  167. set_rows(responseData?.records);
  168. setRowCount(responseData?.count);
  169. if (_doLoad.callback != null) {
  170. _doLoad.callback(responseData);
  171. }
  172. setLoading(false)
  173. // console.log(applyGridOnReady)
  174. if (applyGridOnReady !== undefined){
  175. applyGridOnReady(false)
  176. }
  177. },
  178. onError: function (error){
  179. console.log(error)
  180. setLoading(false)
  181. if (applyGridOnReady !== undefined){
  182. applyGridOnReady(false)
  183. }
  184. }
  185. });
  186. }
  187. return (
  188. <Box sx={containerSx}>
  189. <DataGrid
  190. {...props}
  191. rows={_rows}
  192. rowCount={rowCount || 0}
  193. columns={_columns}
  194. disableColumnMenu
  195. shrinkWrap
  196. rowModesModel={_rowModesModel}
  197. pageSizeOptions={pagination ? _pageSizeOptions : []}
  198. editMode={_editMode}
  199. autoHeight={effectiveAutoHeight}
  200. hideFooterSelectedRowCount={myHideFooterSelectedRowCount}
  201. filterModel={{ items: _filterItems }}
  202. loading={loading}
  203. paginationMode={pagination ? "server" : undefined}
  204. sx={{
  205. ..._sx,
  206. '& .MuiDataGrid-virtualScroller': {
  207. overflowY: height || maxHeight ? 'auto' : 'visible',
  208. overflowX: height || maxHeight ? 'auto' : 'visible',
  209. },
  210. // 👇 completely hide the footer when pagination is off
  211. ...(pagination === false && {
  212. '& .MuiDataGrid-footerContainer': {
  213. display: 'none',
  214. },
  215. }),
  216. }}
  217. components={{
  218. NoRowsOverlay: CustomNoRowsOverlay,
  219. ...(pagination
  220. ? {
  221. Pagination: () => (
  222. <TablePagination
  223. count={rowCount || 0}
  224. page={page}
  225. rowsPerPage={pageSize}
  226. rowsPerPageOptions={_pageSizeOptions}
  227. labelDisplayedRows={() =>
  228. `${(_rows?.length ? page * pageSize + 1 : 0)}-${page * pageSize + (_rows?.length ?? 0)} ${intl.formatMessage({ id: "of" })} ${rowCount}`
  229. }
  230. labelRowsPerPage={intl.formatMessage({ id: "rowsPerPage" }) + ":"}
  231. onPageChange={handleChangePage}
  232. onRowsPerPageChange={handleChangePageSize}
  233. />
  234. ),
  235. }
  236. : {}),
  237. }}
  238. />
  239. </Box>
  240. );
  241. }