ããã«ã¡ã¯ãAG-Boostäºæ¥æ¬é¨ã®å¤§å¡ã§ãã
ä»åã¯ãMUIï¼Material-UIï¼ã®DataGridã«ã¤ãã¦ç´¹ä»ãã¾ãã
ãã¼ã ã®ããã³ãéçºã§ã¯ã以åã¾ã§Core UIãSemantic UIã使ã£ã¦ãã¾ããããæè¿ã¯MUIã使ãæ¹åã«ã·ãããã¦ãã¾ãã MUIã®ç解ãæ·±ããããã«ãDataGridã触ã£ã¦ã¿ãæãç°¡åã«è¦æ ãã®ãããã¼ã¿ãã¼ãã«ãä½æã§ããã®ã§ãç´¹ä»ãããã¨æãã¾ãã
MUIã¨ã¯
MUIï¼Material-UIï¼ã¯ãReactåãã®ã³ã³ãã¼ãã³ãã©ã¤ãã©ãªã§ãGoogleã®Material Designã¬ã¤ãã©ã¤ã³ã«åºã¥ãã¦ãã¶ã¤ã³ããã¦ãã¾ãã MUIã使ç¨ãããã¨ã§ãã¬ã¹ãã³ã·ãã§è¦æ ãã®è¯ãUIãç°¡åã«ä½æã§ãã¾ãã
ã«ã¹ã¿ãã¤ãºã容æã§ãããUIã³ã³ãã¼ãã³ããåå©ç¨ããããããããã®ãã¼ã«ãããã¨ãã¦åºã使ç¨ããã¦ãã¾ãã
DataGridã¨ã¯
ä»åç´¹ä»ããDataGridã¯ãMUIã®ãã¼ã¿ãã¼ãã«ã³ã³ãã¼ãã³ãã§ãã
表形å¼ã®ãã¼ã¿ã表示ãããã¨ãå¯è½ã§ãç·¨éã»ã½ã¼ãã»ãã£ã«ã¿ãªã³ã°ãªã©æ§ã
ãªæ©è½ãåãã¦ãã¾ãã
ç¡æçã®DataGridããããã«å¤ãã®æ©è½ãåãããææçã®DataGridProãDataGridPremiumãããã¾ãã
Install
DataGridã¨ããã®ä¾åé¢ä¿ã«ãã@mui/material
ãinstallãã¾ãã
yarn add @mui/material @emotion/react @emotion/styled @mui/x-data-grid
使ãæ¹
åºæ¬çãªDataGridã®ä½¿ãæ¹ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ã¨ããããæä½éã®columnsã¨è¡¨ç¤ºãã¼ã¿ãpropsã§æ¸¡ãã¦ããã¾ãã
const Users = (props) => { const columns: GridColDef[] = [ { field: 'id', headerName: 'ID' }, { field: 'name', headerName: 'ã¦ã¼ã¶ã¼å' }, { field: 'domain', headerName: 'ãã¡ã¤ã³' }, { field: 'plan', headerName: 'ãã©ã³' }, ]; const rows = [ { id: 1, name: 'A', domain: 'domainA.com', plan: { id: 1, name: 'bronze' } }, { id: 2, name: 'B', domain: 'domainB.com', plan: { id: 2, name: 'silver' } }, { id: 3, name: 'C', domain: 'domainC.com', plan: { id: 3, name: 'gold' } }, ]; return ( <DataGrid rows={rows} columns={columns} /> ); };
âã®ãããªãã¼ãã«ãä½æãããã¨ããã¾ãã
planã¯objectå½¢å¼ã®å¤ãªã®ã§ããã®æç¹ã§ã¯ä¸æã表示ã§ãã¾ããã
次ã«ããããã¼ã¡ãã¥ã¼ãåé¤ãã¿ã³ã追å ãã¦ã¿ã¾ãã
ã¾ããvalueGetter
ã使ã£ã¦planãæ£ãã表示ãããããã«ãã¾ãã
cellã®æ¨ªå¹
ãwidth
ãflex
ã§èª¿æ´å¯è½ã§ãã
const Users = (props) => { const columns: GridColDef[] = [ { field: 'id', headerName: 'ID', flex: 1 }, { field: 'name', headerName: 'ã¦ã¼ã¶ã¼å', flex: 2 }, { field: 'domain', headerName: 'ãã¡ã¤ã³', flex: 2 }, { field: 'plan', headerName: 'ãã©ã³', flex: 2, valueGetter: (params) => { return params.row.plan.name; }, }, { field: 'delete', headerName: 'åé¤', flex: 1, renderCell: () => ( <Button variant="contained" onClick={(e) => { console.log('åé¤click'); e.stopPropagation(); e.preventDefault(); }} > åé¤ </Button> ), }, ]; const rows = [ { id: 1, name: 'A', domain: 'domainA.com', plan: { id: 1, name: 'bronze' } }, { id: 2, name: 'B', domain: 'domainB.com', plan: { id: 2, name: 'silver' } }, { id: 3, name: 'C', domain: 'domainC.com', plan: { id: 3, name: 'gold' } }, ]; return ( <DataGrid rows={rows} columns={columns} slots={{ toolbar: GridToolbar }} onCellClick={(event) => { console.log(`id: ${event.row.id}ãClick`); }} /> ); };
âå³ç«¯ã«åé¤ãã¿ã³ã追å ããã«ã©ã ã®è¡¨ç¤ºå¶å¾¡ã»ãã¦ã³ãã¼ãã»ãã£ã«ã¿ã¼ãªã©ãä¸è¦§ãã¼ãã«ã«æ¬²ããæ©è½ãç°¡åã«å®è£ ãããã¨ãã§ãã¾ããã
columsã®ä½ããæ´ãã<DataGrid />
ã«optionã追å ããã ãã§ããããã®æç¹ã§ååãªã¯ãªãªãã£ã¼ã®ãã¼ãã«ãä½ãã¦ãã¾ãã¾ãã..
<DataGrid>
ã«ã¯ä»ã«ãæ§ã
ãªpropsãããã®ã§ã詳細ã¯å
¬å¼ããã¥ã¡ã³ããã覧ãã ããã
columnã«ãè¨å®å¯è½ãªé
ç®ã沢山ããã¾ãã
DataGrid API - MUI X
GridColDef API - MUI X
æå¾ã«å°ããã¶ã¤ã³ã調æ´ãã¾ãã classãä¸æãè¨å®ããããsxããããã£ã使ããã¨ã§ãã¶ã¤ã³ã®ã«ã¹ã¿ãã¤ãºãã§ãã¾ãã
<DataGrid rows={rows} slots={{ toolbar: GridToolbar }} columns={columns} onCellClick={(event) => { console.log('rowClick'); }} sx={{ // èæ¯è²ã交äºã«å¤ãã '& .MuiDataGrid-row:nth-of-type(odd)': { backgroundColor: '#f4f4f4', }, // hoveræã®èæ¯è²ãå¤ãã '& .MuiDataGrid-row:hover': { backgroundColor: '#d3d3d3', }, // toolBarã®ä¸ã«border追å '& .MuiDataGrid-toolbarContainer': { borderBottom: '1px solid #ccc', }, // åé¤ãã¿ã³ã®opacityãå¤æ´ '& .MuiDataGrid-cell--delete': { opacity: '0.8', }, }} // åçã«classãä»ä¸ãã getCellClassName={(params) => { const { field } = params.colDef; return field === 'delete' ? 'MuiDataGrid-cell--delete' : ''; }} />
è¦ãç®ã¯ãã®æ§ã«ãªãã¾ããã
ã¾ã¨ã
MUIã®DataGridã使ããã¨ã§ãæ軽ãã¤é«æ©è½ãªãã¼ã¿ãã¼ãã«ãå®è£
ã§ãããã¨ãã§ãã¾ããã
ã«ã¹ã¿ãã¤ãºãç°¡åãªã®ã§ãè¦ä»¶ã«åãããæè»ãªä½ããã§ãã¾ãã
ä»å¾ãã¼ãã«ãä½ãéã¯ãDataGridã使ãã°ã³ã¹ãããå®è£
ã§ãããªã¨æãã¾ããã
DataGrid以å¤ã«ã便å©ãªMUIã®ã³ã³ãã¼ãã³ãã¯æ²¢å±±ããã®ã§ãæ¯é試ãã¦ã¿ã¦ãã ããã
ããã