Skip to content

Commit

Permalink
[DataGridPro] Server-side tree data support (mui#12317)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored and DungTiger committed Jul 23, 2024
1 parent e35d3fc commit 45cadf7
Show file tree
Hide file tree
Showing 87 changed files with 4,031 additions and 1,176 deletions.
57 changes: 57 additions & 0 deletions docs/data/data-grid/server-side-data/ServerSideDataGrid.js
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 docs/data/data-grid/server-side-data/ServerSideDataGrid.tsx
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 docs/data/data-grid/server-side-data/ServerSideDataGridNoCache.js
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 docs/data/data-grid/server-side-data/ServerSideDataGridNoCache.tsx
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>
);
}
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 docs/data/data-grid/server-side-data/ServerSideDataGridTTL.js
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 docs/data/data-grid/server-side-data/ServerSideDataGridTTL.tsx
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;
Loading

0 comments on commit 45cadf7

Please sign in to comment.