-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGridPro] Server-side tree data support (mui#12317)
- Loading branch information
1 parent
e35d3fc
commit 45cadf7
Showing
87 changed files
with
4,031 additions
and
1,176 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
docs/data/data-grid/server-side-data/ServerSideDataGrid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
function ServerSideDataGrid() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideDataGrid; |
57 changes: 57 additions & 0 deletions
57
docs/data/data-grid/server-side-data/ServerSideDataGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro, GridDataSource } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
function ServerSideDataGrid() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideDataGrid; |
60 changes: 60 additions & 0 deletions
60
docs/data/data-grid/server-side-data/ServerSideDataGridNoCache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const pageSizeOptions = [5, 10, 50]; | ||
|
||
const serverOptions = { useCursorPagination: false }; | ||
const dataSetOptions = {}; | ||
|
||
export default function ServerSideDataGridNoCache() { | ||
const { fetchRows, columns, initialState } = useMockServer( | ||
dataSetOptions, | ||
serverOptions, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
initialState={initialStateWithPagination} | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_dataSourceCache={null} | ||
pagination | ||
pageSizeOptions={pageSizeOptions} | ||
/> | ||
</div> | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
docs/data/data-grid/server-side-data/ServerSideDataGridNoCache.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro, GridDataSource } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const pageSizeOptions = [5, 10, 50]; | ||
|
||
const serverOptions = { useCursorPagination: false }; | ||
const dataSetOptions = {}; | ||
|
||
export default function ServerSideDataGridNoCache() { | ||
const { fetchRows, columns, initialState } = useMockServer( | ||
dataSetOptions, | ||
serverOptions, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
initialState={initialStateWithPagination} | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_dataSourceCache={null} | ||
pagination | ||
pageSizeOptions={pageSizeOptions} | ||
/> | ||
</div> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
docs/data/data-grid/server-side-data/ServerSideDataGridNoCache.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<DataGridPro | ||
initialState={initialStateWithPagination} | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_dataSourceCache={null} | ||
pagination | ||
pageSizeOptions={pageSizeOptions} | ||
/> |
60 changes: 60 additions & 0 deletions
60
docs/data/data-grid/server-side-data/ServerSideDataGridTTL.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro, GridDataSourceCacheDefault } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const lowTTLCache = new GridDataSourceCacheDefault({ ttl: 1000 * 10 }); // 10 seconds | ||
|
||
function ServerSideDataGridTTL() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_dataSourceCache={lowTTLCache} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideDataGridTTL; |
64 changes: 64 additions & 0 deletions
64
docs/data/data-grid/server-side-data/ServerSideDataGridTTL.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPro, | ||
GridDataSource, | ||
GridDataSourceCacheDefault, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const lowTTLCache = new GridDataSourceCacheDefault({ ttl: 1000 * 10 }); // 10 seconds | ||
|
||
function ServerSideDataGridTTL() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: encodeURIComponent( | ||
JSON.stringify(params.paginationModel), | ||
), | ||
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)), | ||
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_dataSourceCache={lowTTLCache} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideDataGridTTL; |
Oops, something went wrong.