Data Grid - 리스트 뷰 [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')
Data Grid - 리스트 뷰
작은 화면과 모바일 기기에서 더 컴팩트한 Data Grid를 위해 단일 컬럼 리스트 뷰로 데이터를 표시해 봐요.
데스크톱 기기를 위해 만들어진 전형적인 데이터 그리드는 모바일 기기에서 사용하기 어려울 수 있어요. Data Grid Pro는 작은 화면에서 더 나은 경험을 주기 위해 단일 컬럼 리스트 뷰 형식을 제공해요.
본문
리스트 뷰 구현하기
리스트 뷰를 활성화하려면 Data Grid에 listView prop을 전달하세요.
기본 그리드 뷰와 달리 리스트 뷰는 a renderCell() 함수가 포함된 listViewColumn prop을 전달해서 컬럼과 셀이 표시되는 방식을 명시적으로 정의해야 해요.
function ListViewCell(params: GridRenderCellParams) {
return <>{params.row.id}</>;
}
const listViewColDef: GridListViewColDef = {
field: 'listColumn',
renderCell: ListViewCell,
};
<DataGridPro listViewColumn={listViewColDef} listView={true} />;
import * as React from 'react';
import {
DataGridPro,
GridRenderCellParams,
GridListViewColDef,
GridColDef,
GridRowParams,
GridToolbarContainer,
GridActionsCell,
} from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';
import Stack from '@mui/material/Stack';
import Avatar from '@mui/material/Avatar';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import MessageIcon from '@mui/icons-material/Message';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import ToggleButton from '@mui/material/ToggleButton';
import GridViewIcon from '@mui/icons-material/ViewModule';
import ListViewIcon from '@mui/icons-material/ViewList';
declare module '@mui/x-data-grid' {
interface ToolbarPropsOverrides {
view: 'grid' | 'list';
onChangeView: (view: 'grid' | 'list') => void;
}
}
function MessageAction(params: Pick<GridRowParams, 'row'>) {
const handleMessage = (event: React.MouseEvent) => {
event.stopPropagation();
console.log(`send message to ${params.row.phone}`);
};
return (
<IconButton aria-label="Message" onClick={handleMessage}>
<MessageIcon />
</IconButton>
);
}
function ListViewCell(params: GridRenderCellParams) {
return (
<Stack
direction="row"
sx={{
alignItems: 'center',
height: '100%',
gap: 2,
}}
>
<Avatar sx={{ width: 32, height: 32, backgroundColor: params.row.avatar }} />
<Stack sx={{ flexGrow: 1 }}>
<Typography variant="body2" sx={{ fontWeight: 500 }}>
{params.row.name}
</Typography>
<Typography variant="body2" sx={{ color: 'text.secondary' }}>
{params.row.position}
</Typography>
</Stack>
<MessageAction {...params} />
</Stack>
);
}
const listViewColDef: GridListViewColDef = {
field: 'listColumn',
renderCell: ListViewCell,
};
const VISIBLE_FIELDS = ['avatar', 'name', 'position'];
type ToolbarProps = {
view: 'grid' | 'list';
onChangeView: (view: 'grid' | 'list') => void;
};
function Toolbar({ view, onChangeView }: ToolbarProps) {
return (
<GridToolbarContainer
sx={{ borderBottom: '1px solid', borderColor: 'divider', py: 1 }}
>
<ToggleButtonGroup
size="small"
sx={{ ml: 'auto' }}
value={view}
exclusive
onChange={(_, newView) => {
if (newView) {
onChangeView(newView);
}
}}
>
<ToggleButton
size="small"
color="primary"
sx={{ gap: 0.5 }}
value="grid"
selected={view === 'grid'}
>
<GridViewIcon fontSize="small" /> Grid
</ToggleButton>
<ToggleButton
size="small"
color="primary"
sx={{ gap: 0.5 }}
value="list"
selected={view === 'list'}
>
<ListViewIcon fontSize="small" /> List
</ToggleButton>
</ToggleButtonGroup>
</GridToolbarContainer>
);
}
export default function ListView() {
const [view, setView] = React.useState<'grid' | 'list'>('list');
const isListView = view === 'list';
const { data, loading } = useDemoData({
dataSet: 'Employee',
rowLength: 20,
visibleFields: VISIBLE_FIELDS,
});
const columns: GridColDef[] = React.useMemo(() => {
return [
...data.columns,
{
type: 'actions',
field: 'actions',
width: 75,
renderCell: (params: GridRenderCellParams) => (
<GridActionsCell {...params}>
<MessageAction row={params.row} />
</GridActionsCell>
),
},
];
}, [data.columns]);
const rowHeight = isListView ? 64 : 52;
return (
<div style={{ width: '100%', maxWidth: 360, height: 600 }}>
<DataGridPro
{...data}
loading={loading}
columns={columns}
rowHeight={rowHeight}
listView={isListView}
listViewColumn={listViewColDef}
slots={{
toolbar: Toolbar,
}}
showToolbar
slotProps={{
toolbar: {
view,
onChangeView: setView,
},
}}
sx={{ backgroundColor: 'background.paper' }}
/>
</div>
);
}
미디어 쿼리로 반응형 리스트 뷰
@mui/material의 useMediaQuery() 훅을 사용해서 지정된 브레이크포인트에서 리스트 뷰 기능을 활성화할 수 있어요.
아래 데모는 뷰포트 너비가 md 브레이크포인트보다 작아지면 자동으로 리스트 레이아웃으로 전환돼요.
import {
DataGridPro,
GridRenderCellParams,
GridListViewColDef,
} from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';
import Stack from '@mui/material/Stack';
import Avatar from '@mui/material/Avatar';
import Typography from '@mui/material/Typography';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useTheme } from '@mui/material/styles';
function ListViewCell(params: GridRenderCellParams) {
return (
<Stack
direction="row"
sx={{
alignItems: 'center',
height: '100%',
gap: 2,
}}
>
<Avatar sx={{ width: 32, height: 32, backgroundColor: params.row.avatar }} />
<Stack sx={{ flexGrow: 1 }}>
<Typography
variant="body2"
sx={{
fontWeight: 500,
}}
>
{params.row.name}
</Typography>
<Typography
variant="body2"
sx={{
color: 'text.secondary',
}}
>
{params.row.position}
</Typography>
</Stack>
</Stack>
);
}
const listViewColDef: GridListViewColDef = {
field: 'listColumn',
renderCell: ListViewCell,
};
const VISIBLE_FIELDS = ['avatar', 'name', 'position'];
export default function ListViewMediaQuery() {
const theme = useTheme();
const isListView = useMediaQuery(theme.breakpoints.down('md'));
const { data, loading } = useDemoData({
dataSet: 'Employee',
rowLength: 5,
visibleFields: VISIBLE_FIELDS,
});
const rowHeight = isListView ? 64 : 52;
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
width: '100%',
height: 400,
}}
>
<DataGridPro
{...data}
loading={loading}
rowHeight={rowHeight}
listView={isListView}
listViewColumn={listViewColDef}
/>
</div>
);
}
편집 가능한 행이 있는 리스트 뷰
편집 기능은 리스트 뷰에서는 지원되지 않지만, 아래와 같이 커스텀 셀 렌더러 안에서 편집 경험을 만들 수 있어요.
import * as React from 'react';
import {
DataGridPro,
GridRenderCellParams,
GridListViewColDef,
GridColDef,
GridRowParams,
GridRowsProp,
useGridApiContext,
} from '@mui/x-data-grid-pro';
import Stack from '@mui/material/Stack';
import Avatar from '@mui/material/Avatar';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
import DialogActions from '@mui/material/DialogActions';
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import {
randomId,
randomTraderName,
randomArrayItem,
} from '@mui/x-data-grid-generator';
const roles = ['Marketing', 'Finance', 'Development'];
const randomRole = () => {
return randomArrayItem(roles);
};
const rows: GridRowsProp = [
{
id: randomId(),
name: randomTraderName(),
position: randomRole(),
avatar: '#4caf50',
},
{
id: randomId(),
name: randomTraderName(),
position: randomRole(),
avatar: '#2196f3',
},
{
id: randomId(),
name: randomTraderName(),
position: randomRole(),
avatar: '#ff9800',
},
{
id: randomId(),
name: randomTraderName(),
position: randomRole(),
avatar: '#9c27b0',
},
{
id: randomId(),
name: randomTraderName(),
position: randomRole(),
avatar: '#f44336',
},
];
const columns: GridColDef[] = [
{ field: 'name', headerName: 'Name', width: 180 },
{
field: 'position',
headerName: 'Department',
width: 220,
type: 'singleSelect',
valueOptions: roles,
},
];
function EditAction(props: Pick<GridRowParams, 'row'>) {
const { row } = props;
const [editing, setEditing] = React.useState(false);
const [name, setName] = React.useState(row.name);
const [position, setPosition] = React.useState(row.position);
const apiRef = useGridApiContext();
const handleEdit = (event: React.MouseEvent) => {
event.stopPropagation();
setEditing(true);
};
const handleClose = () => {
setEditing(false);
};
const handleSave = (event: React.FormEvent) => {
event.preventDefault();
apiRef.current.updateRows([{ id: row.id, name, position }]);
handleClose();
};
React.useEffect(() => {
setName(row.name);
setPosition(row.position);
}, [row]);
return (
<React.Fragment>
<IconButton aria-label="Edit" onClick={handleEdit}>
<EditIcon />
</IconButton>
<Dialog
open={editing}
onClose={handleClose}
slotProps={{
paper: {
component: 'form',
onSubmit: handleSave,
},
}}
>
<DialogTitle>Edit Employee</DialogTitle>
<DialogContent sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
<DialogContentText>
Make changes to the employee's information.
</DialogContentText>
<TextField
autoFocus
required
margin="dense"
id="name"
name="name"
label="Name"
fullWidth
value={name}
onChange={(event) => setName(event.target.value)}
/>
<FormControl fullWidth required>
<InputLabel id="position-label">Position</InputLabel>
<Select
labelId="position-label"
id="position"
name="position"
label="Position"
value={position}
onChange={(event) => setPosition(event.target.value)}
>
{roles.map((role) => (
<MenuItem key={role} value={role}>
{role}
</MenuItem>
))}
</Select>
</FormControl>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel</Button>
<Button type="submit">Save changes</Button>
</DialogActions>
</Dialog>
</React.Fragment>
);
}
function DeleteAction(props: Pick<GridRowParams, 'row'>) {
const { row } = props;
const apiRef = useGridApiContext();
return (
<IconButton
aria-label="Delete"
onClick={() => apiRef.current.updateRows([{ id: row.id, _action: 'delete' }])}
>
<DeleteIcon />
</IconButton>
);
}
function ListViewCell(props: GridRenderCellParams) {
const { row } = props;
return (
<Stack direction="row" sx={{ alignItems: 'center', height: '100%', gap: 2 }}>
<Avatar sx={{ width: 32, height: 32, backgroundColor: row.avatar }} />
<Stack sx={{ flexGrow: 1 }}>
<Typography variant="body2" sx={{ fontWeight: 500 }}>
{row.name}
</Typography>
<Typography variant="body2" sx={{ color: 'text.secondary' }}>
{row.position}
</Typography>
</Stack>
<Stack direction="row" sx={{ gap: 0.5 }}>
<EditAction {...props} />
<DeleteAction {...props} />
</Stack>
</Stack>
);
}
const listViewColDef: GridListViewColDef = {
field: 'listColumn',
renderCell: (params) => <ListViewCell {...params} />,
};
export default function ListViewEdit() {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
width: '100%',
maxWidth: 360,
height: 400,
}}
>
<DataGridPro
rows={rows}
columns={columns}
rowHeight={64}
listView
listViewColumn={listViewColDef}
sx={{ backgroundColor: 'background.paper' }}
/>
</div>
);
}
작은 화면에 맞게 리스트 뷰 최적화하기
사용 사례가 일급(first-class) 모바일 UX를 요구한다면, 아래 데모처럼 커스텀 하위 컴포넌트를 사용해서 Data Grid의 리스트 레이아웃을 완전히 커스터마이징할 수 있어요.
import * as React from 'react';
import {
GridActionsCellItem,
GridColDef,
useGridApiRef,
GridRowParams,
DataGridPremium,
GridRowId,
gridClasses,
GridActionsCell,
GridRenderCellParams,
useGridApiContext,
} from '@mui/x-data-grid-premium';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
import Avatar from '@mui/material/Avatar';
import Stack from '@mui/material/Stack';
import OpenIcon from '@mui/icons-material/Visibility';
import useMediaQuery from '@mui/material/useMediaQuery';
import CSSBaseline from '@mui/material/CssBaseline';
import { randomId } from '@mui/x-data-grid-generator';
import { useTheme } from '@mui/material/styles';
import { FileIcon } from './components/FileIcon';
import { DetailsDrawer } from './components/DetailsDrawer';
import { ListCell } from './components/ListCell';
import { Toolbar } from './components/Toolbar';
import { INITIAL_ROWS } from './data';
import { FILE_TYPES } from './constants';
import { RowModel, FileType } from './types';
import { formatDate, formatSize, stringAvatar } from './utils';
import { ActionDrawer } from './components/ActionDrawer';
import { RenameDialog } from './components/RenameDialog';
declare module '@mui/x-data-grid' {
interface ToolbarPropsOverrides {
listView: boolean;
container: HTMLElement;
handleDelete: (ids: GridRowId[]) => void;
handleUpload: (event: React.ChangeEvent<HTMLInputElement>) => void;
}
}
interface Props {
// Injected by the documentation to work in an iframe.
window?: () => Window;
}
interface ActionHandlers {
setOverlayState: (state: {
overlay: 'actions' | 'details' | 'rename' | null;
params: Pick<GridRowParams<RowModel>, 'row'> | null;
}) => void;
handleDelete: (ids: GridRowId[]) => void;
}
const ActionHandlersContext = React.createContext<ActionHandlers | null>(null);
function ActionsCell(props: GridRenderCellParams) {
const context = React.useContext(ActionHandlersContext);
const apiRef = useGridApiContext();
if (!context) {
return null;
}
const { setOverlayState, handleDelete } = context;
return (
<GridActionsCell {...props}>
<GridActionsCellItem
label="Preview"
icon={<OpenIcon fontSize="small" />}
onClick={() => {
setOverlayState({ overlay: 'actions', params: props });
}}
showInMenu
/>
<GridActionsCellItem
label="Rename"
icon={<EditIcon fontSize="small" />}
onClick={() =>
apiRef.current?.startCellEditMode({
id: props.id,
field: 'name',
})
}
showInMenu
/>
<GridActionsCellItem
label="Delete"
icon={<DeleteIcon fontSize="small" />}
onClick={() => handleDelete([props.id])}
showInMenu
/>
</GridActionsCell>
);
}
const columns: GridColDef[] = [
{
field: 'name',
headerName: 'Name',
width: 350,
editable: true,
hideable: false,
renderCell: (params) => {
return (
<Stack
direction="row"
sx={{ gap: 1.5, alignItems: 'center', height: '100%' }}
>
<FileIcon type={params.row.type} />
{params.value}
</Stack>
);
},
},
{
field: 'createdBy',
headerName: 'Owner',
width: 200,
renderCell: (params) => {
const avatarProps = stringAvatar(params.value);
return (
<Stack direction="row" sx={{ gap: 1.5, alignItems: 'center' }}>
<Avatar
{...avatarProps}
sx={{ width: 24, height: 24, fontSize: 12, ...avatarProps.sx }}
/>
{params.value}
</Stack>
);
},
},
{
field: 'createdAt',
headerName: 'Added',
type: 'date',
width: 200,
valueFormatter: formatDate,
},
{
field: 'updatedAt',
headerName: 'Modified',
type: 'date',
width: 200,
valueFormatter: formatDate,
},
{
field: 'type',
headerName: 'Type',
width: 150,
},
{
field: 'size',
headerName: 'Size',
width: 120,
valueFormatter: formatSize,
},
{
type: 'actions',
field: 'actions',
resizable: false,
width: 50,
renderCell: (params) => <ActionsCell {...params} />,
},
];
export default function ListViewAdvanced({ window }: Props) {
// This is used only for the example - renders the drawer inside the container
const container = window !== undefined ? window().document.body : undefined;
const theme = useTheme();
const isBelowMd = useMediaQuery(theme.breakpoints.down('md'));
const isDocsDemo = window !== undefined;
const isListView = isDocsDemo ? true : isBelowMd;
const apiRef = useGridApiRef();
const [loading, setLoading] = React.useState(false);
const [overlayState, setOverlayState] = React.useState<{
overlay: 'actions' | 'details' | 'rename' | null;
params: Pick<GridRowParams<RowModel>, 'row'> | null;
}>({
overlay: null,
params: null,
});
const handleCloseOverlay = () => {
setOverlayState({ overlay: null, params: null });
};
const handleDelete = React.useCallback(
(ids: GridRowId[]) => {
apiRef.current?.updateRows(ids.map((id) => ({ id, _action: 'delete' })));
},
[apiRef],
);
const handleUpdate = React.useCallback(
(
id: GridRowId,
field: GridRowParams<RowModel>['columns'][number]['field'],
value: string,
) => {
const updatedAt = new Date().toISOString();
apiRef.current?.updateRows([{ id, [field]: value, updatedAt }]);
},
[apiRef],
);
const handleUpload = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
if (!event.target.files) {
return;
}
const file = event.target.files[0];
const createdAt = new Date().toISOString();
const fileType = file.type.split('/')[1];
// validate file type
if (!FILE_TYPES.includes(fileType as FileType)) {
alert('Invalid file type');
return;
}
const row: RowModel = {
id: randomId(),
name: file.name,
description: '',
type: fileType as FileType,
size: file.size,
createdBy: 'Kenan Yusuf',
createdAt,
updatedAt: createdAt,
state: 'pending',
};
event.target.value = '';
// Add temporary row
setLoading(true);
apiRef.current?.updateRows([row]);
// Simulate server response time
// eslint-disable-next-line no-restricted-properties -- not visible in UI
const timeout = Math.floor(Math.random() * 3000) + 2000;
setTimeout(() => {
const uploadedRow: RowModel = { ...row, state: 'uploaded' };
apiRef.current?.updateRows([uploadedRow]);
setOverlayState({ overlay: 'actions', params: { row } });
setLoading(false);
}, timeout);
},
[apiRef],
);
const listViewColDef: GridColDef = React.useMemo(
() => ({
field: 'listCell',
renderCell: (params) => (
<ListCell
{...params}
onOpenActions={() => {
setOverlayState({ overlay: 'actions', params });
}}
/>
),
}),
[],
);
const getEstimatedRowHeight = () => {
const density = apiRef.current?.state?.density;
if (isListView) {
switch (density) {
case 'compact':
return 47;
case 'standard':
return 67;
case 'comfortable':
return 97;
default:
return 67;
}
} else {
switch (density) {
case 'compact':
return 47;
case 'standard':
return 55;
case 'comfortable':
return 63;
default:
return 55;
}
}
};
const getRowHeight = React.useCallback(
() => (isListView ? 'auto' : undefined),
[isListView],
);
const actionHandlers = React.useMemo<ActionHandlers>(
() => ({
setOverlayState,
apiRef,
handleDelete,
}),
[apiRef, handleDelete],
);
return (
<React.Fragment>
<CSSBaseline />
<div
style={{
maxWidth: '100%',
height: 600,
}}
>
<ActionHandlersContext.Provider value={actionHandlers}>
<DataGridPremium
apiRef={apiRef}
rows={INITIAL_ROWS}
columns={columns}
loading={loading}
slots={{ toolbar: Toolbar }}
showToolbar
slotProps={{
toolbar: {
listView: isListView,
container,
handleDelete,
handleUpload,
},
loadingOverlay: {
variant: 'linear-progress',
},
}}
listView={isListView}
listViewColumn={listViewColDef}
pagination
pageSizeOptions={[10]}
initialState={{
density: 'comfortable',
pagination: {
paginationModel: {
pageSize: 10,
},
},
sorting: {
sortModel: [{ field: 'createdAt', sort: 'desc' }],
},
columns: {
columnVisibilityModel: {
type: false,
},
},
}}
sx={{
border: 0,
borderRadius: 0,
[`& .${gridClasses.cell}`]: { display: 'flex', alignItems: 'center' },
[`&.${gridClasses['root--densityCompact']} .${gridClasses.cell}`]: {
py: 1,
},
[`&.${gridClasses['root--densityStandard']} .${gridClasses.cell}`]: {
py: 1.5,
},
[`&.${gridClasses['root--densityComfortable']} .${gridClasses.cell}`]:
{
py: 2,
},
}}
getRowHeight={getRowHeight}
getEstimatedRowHeight={getEstimatedRowHeight}
onRowDoubleClick={(params) =>
setOverlayState({ overlay: 'actions', params })
}
hideFooterSelectedRowCount
/>
</ActionHandlersContext.Provider>
<DetailsDrawer
open={overlayState.overlay === 'details'}
params={overlayState.params}
listView={isListView}
container={container}
onDescriptionChange={(id, value) => handleUpdate(id, 'description', value)}
onClose={handleCloseOverlay}
/>
<ActionDrawer
open={overlayState.overlay === 'actions'}
params={overlayState.params}
container={container}
onPreview={() =>
setOverlayState({ overlay: 'details', params: overlayState.params })
}
onRename={() =>
setOverlayState({ overlay: 'rename', params: overlayState.params })
}
onDelete={(id) => {
handleDelete([id]);
handleCloseOverlay();
}}
onClose={handleCloseOverlay}
/>
<RenameDialog
open={overlayState.overlay === 'rename'}
params={overlayState.params}
container={container}
onSave={(id, value) => handleUpdate(id, 'name', value)}
onClose={handleCloseOverlay}
/>
</div>
</React.Fragment>
);
}
리스트 뷰 기능 호환성
리스트 뷰는 다음 기능들과 함께 사용할 수 있어요.
:::warning 여기에 나열되지 않은 기능은 예상대로 동작하지 않을 수 있어요(또는 전혀 동작하지 않을 수 있어요). 위에 나열된 기능을 쓰고 있는데 예상대로 동작하지 않는다면 버그 리포트를 열어 주세요. 나열되지 않은 다른 기능과 함께 리스트 뷰를 사용해야 한다면 기능 요청을 열어 주세요. :::
API
더 알아보기
더 자세한 내용은 MUI X Data Grid 문서를 참고하세요.