您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

226 行
7.9 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, tab, ...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. console.log(localStorageSearchCriteria)
  55. if(localStorageSearchCriteria.start!=undefined){
  56. console.log(localStorageSearchCriteria)
  57. setPage(localStorageSearchCriteria.start/pageSize);
  58. }
  59. }
  60. }else{
  61. setPage(0);
  62. setPageSize(parseInt(event.target.value, 10));
  63. }
  64. set_doLoad(doLoad);
  65. setLoading(true)
  66. }
  67. }, [doLoad]);
  68. useEffect(() => {
  69. getDataList();
  70. }, [_doLoad, page]);
  71. useEffect(() => {
  72. if (sx) {
  73. set_sx(sx);
  74. }
  75. if (hideFooterSelectedRowCount) {
  76. setMyHideFooterSelectedRowCount(hideFooterSelectedRowCount);
  77. }
  78. if (rowModesModel) {
  79. set_rowModesModel(rowModesModel)
  80. }
  81. if (rows) {
  82. set_rows(rows)
  83. setRowCount(rows.length)
  84. }
  85. if (columns) {
  86. set_columns(columns)
  87. }
  88. if (pageSizeOptions) {
  89. set_pageSizeOptions(pageSizeOptions)
  90. }
  91. if (autoHeight !== undefined) {
  92. set_autoHeight(autoHeight)
  93. }
  94. if (editMode) {
  95. set_editMode(editMode);
  96. }
  97. if (filterItems) {
  98. set_filterItems(filterItems);
  99. }
  100. if (customPageSize) {
  101. setPageSize(customPageSize);
  102. }
  103. // console.log(_doLoad)
  104. if (_doLoad !== undefined && Object.keys(_doLoad).length==0 ){
  105. setLoading(false)
  106. if (applyGridOnReady !== undefined){
  107. applyGridOnReady(false)
  108. }
  109. }
  110. }, [sx, hideFooterSelectedRowCount, rowModesModel, rows, columns, pageSizeOptions, autoHeight, editMode, filterItems, customPageSize]);
  111. const handleChangePage = (event, newPage) => {
  112. setPage(newPage);
  113. };
  114. const handleChangePageSize = (event) => {
  115. setPageSize(parseInt(event.target.value, 10));
  116. setPage(0);
  117. };
  118. function CustomNoRowsOverlay() {
  119. return (
  120. <GridOverlay>
  121. <Typography variant="body1">
  122. <FormattedMessage id="noRecordFound" />
  123. </Typography>
  124. </GridOverlay>
  125. );
  126. }
  127. function getDataList() {
  128. // console.log(Object.keys(_doLoad.params).length > 0)
  129. // console.log(Object.keys(_doLoad.params).length > 0)
  130. if (_doLoad?.url == null){
  131. setLoading(false)
  132. return;
  133. }
  134. if (_doLoad.params == undefined) return;
  135. if (_doLoad.params.searchCriteria !== undefined) return;
  136. if (_doLoad.params == null) _doLoad.params = {};
  137. _doLoad.params.start = page * pageSize;
  138. _doLoad.params.limit = pageSize;
  139. if(checkSearchCriteriaPath(window.location.pathname)){
  140. if(window.location.pathname === "/publicNotice"){
  141. if (tab != undefined && tab ==="application"){
  142. localStorage.setItem('searchCriteria', JSON.stringify({path:window.location.pathname,data:_doLoad.params}))
  143. }
  144. }else if (window.location.pathname != "/publicNotice"){
  145. localStorage.setItem('searchCriteria', JSON.stringify({path:window.location.pathname,data:_doLoad.params}))
  146. }
  147. }
  148. HttpUtils.get({
  149. url: _doLoad.url,
  150. params: _doLoad.params,
  151. onSuccess: function (responseData) {
  152. set_rows(responseData?.records);
  153. setRowCount(responseData?.count);
  154. if (_doLoad.callback != null) {
  155. _doLoad.callback(responseData);
  156. }
  157. setLoading(false)
  158. // console.log(applyGridOnReady)
  159. if (applyGridOnReady !== undefined){
  160. applyGridOnReady(false)
  161. }
  162. },
  163. onError: function (error){
  164. console.log(error)
  165. setLoading(false)
  166. if (applyGridOnReady !== undefined){
  167. applyGridOnReady(false)
  168. }
  169. }
  170. });
  171. }
  172. return (
  173. <DataGrid
  174. {...props}
  175. rows={_rows}
  176. rowCount={rowCount ? rowCount : 0}
  177. columns={_columns}
  178. paginationMode="server"
  179. disableColumnMenu
  180. shrinkWrap={true}
  181. rowModesModel={_rowModesModel}
  182. pageSizeOptions={_pageSizeOptions}
  183. editMode={_editMode}
  184. autoHeight={_autoHeight}
  185. hideFooterSelectedRowCount={myHideFooterSelectedRowCount}
  186. filterModel={{ items: _filterItems }}
  187. sx={_sx}
  188. loading={loading}
  189. components={{
  190. noRowsOverlay: CustomNoRowsOverlay,
  191. Pagination: () => (
  192. <TablePagination
  193. count={rowCount ? rowCount : 0}
  194. page={page}
  195. rowsPerPage={pageSize}
  196. rowsPerPageOptions={_pageSizeOptions}
  197. labelDisplayedRows={() =>
  198. `${(_rows?.length ? page * pageSize + 1 : 0)}-${page * pageSize + (_rows?.length ?? 0)} ${intl.formatMessage({ id: "of" })} ${rowCount}`
  199. }
  200. labelRowsPerPage={intl.formatMessage({ id: "rowsPerPage" }) + ":"}
  201. onPageChange={handleChangePage}
  202. onRowsPerPageChange={handleChangePageSize}
  203. />
  204. ),
  205. }}
  206. />
  207. );
  208. }