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.
 
 

217 lines
7.3 KiB

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