Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

156 lignes
4.4 KiB

  1. // material-ui
  2. import * as React from 'react';
  3. import {
  4. DataGrid,
  5. GridActionsCellItem,
  6. } from "@mui/x-data-grid";
  7. import * as Icon from '../utils/IconUtils';
  8. import * as HttpUtils from "../utils/HttpUtils"
  9. import * as UrlUtils from "../utils/ApiPathConst"
  10. import * as DateUtils from "../utils/DateUtils"
  11. // ==============================|| EVENT TABLE ||============================== //
  12. export default function FileList({refType, refId, allowDelete, sx}) {
  13. const [rows, setRows] = React.useState([]);
  14. const [rowModesModel] = React.useState({});
  15. React.useEffect(() => {
  16. loadData();
  17. }, []);
  18. const onDeleteClick = (fileId, skey, filename) => () => {
  19. HttpUtils.get(
  20. {
  21. url: UrlUtils.GET_FILE_DELETE,
  22. params:{
  23. fileId: fileId,
  24. skey:skey,
  25. filename: filename
  26. },
  27. onSuccess: function(){
  28. loadData();
  29. }
  30. }
  31. );
  32. };
  33. const onDownloadClick = (fileId, skey, filename) => () => {
  34. HttpUtils.fileDownload({
  35. fileId:fileId,
  36. skey:skey,
  37. filename:filename,
  38. });
  39. };
  40. const loadData = ()=>{
  41. HttpUtils.post(
  42. {
  43. url: UrlUtils.POST_FILE_LIST,
  44. params:{
  45. refType: refType,
  46. refId: refId,
  47. },
  48. onSuccess: function(responseData){
  49. setRows(responseData.records);
  50. }
  51. }
  52. );
  53. };
  54. const convertToStr=(bytes,decimals)=>{
  55. if(bytes == 0) return '0 Bytes';
  56. var dm = decimals || 2,
  57. sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  58. let i = 0;
  59. i = Math.floor(Math.log(bytes) / Math.log(1024));
  60. return parseFloat((bytes / Math.pow(1024, i)).toFixed(dm)) + ' ' + sizes[i];
  61. }
  62. const columns = [
  63. {
  64. id: 'created',
  65. field: 'created',
  66. headerName: 'created',
  67. flex: 1,
  68. valueGetter:(params)=>{
  69. return DateUtils.datetimeStr(params.value);
  70. }
  71. },
  72. {
  73. field: 'actions',
  74. type: 'actions',
  75. headerName: '',
  76. width: 50,
  77. cellClassName: 'actions',
  78. getActions: (params) => {
  79. return [
  80. <GridActionsCellItem
  81. key="OutSave"
  82. icon={<Icon.Download/>}
  83. label="Download"
  84. className="textPrimary"
  85. onClick={onDownloadClick(params.id, params.row.skey, params.row.filename)}
  86. color="primary"
  87. />]
  88. },
  89. },{
  90. id: 'filename',
  91. field: 'filename',
  92. headerName: 'filename',
  93. flex: 3,
  94. },
  95. {
  96. id: 'filesize',
  97. field: 'filesize',
  98. headerName: 'filesize',
  99. flex: 1,
  100. valueGetter:(params)=>{
  101. return convertToStr(params.value);
  102. }
  103. },
  104. ];
  105. if(allowDelete){
  106. columns.push({
  107. field: 'actions',
  108. type: 'actions',
  109. headerName: 'Delete',
  110. width: 100,
  111. cellClassName: 'actions',
  112. getActions: (params) => {
  113. return [
  114. <GridActionsCellItem
  115. key="OutSave"
  116. icon={<Icon.Delete/>}
  117. label="Delete"
  118. className="textPrimary"
  119. onClick={onDeleteClick(params.id, params.row.skey, params.row.filename)}
  120. color="primary"
  121. />]
  122. },
  123. });
  124. }
  125. return (
  126. <div style={{height: 400, width: '100%'}}>
  127. <DataGrid
  128. sx={sx}
  129. rows={rows}
  130. columns={columns}
  131. editMode="row"
  132. rowModesModel={rowModesModel}
  133. initialState={{
  134. pagination: {
  135. paginationModel: {page: 0, pageSize: 5},
  136. },
  137. }}
  138. pageSizeOptions={[5, 10]}
  139. autoHeight = {true}
  140. />
  141. </div>
  142. );
  143. }