@mui/x-data-grid 에 사용자정의 컬럼 타입을 추가해서 사용하려고 합니다.
2621
投稿した質問数 74
@mui/x-data-grid 패키지를 사용해서 type= "time" 을 추가 해 보려고 합니다.
검색을 해보니 공식문서에서는 커스터마이징 해서 사용할수있다고 하는 글을 보았습니다.
제가 필요한 time picker 부분을 추가하는 부분은 찾지 못했습니다. ㅠ.ㅠ
https://mui.com/x/react-data-grid/column-definition/#custom-column-types
@mui/x-data-grid 에서 기본 제공하는 타입들은 아래와 같습니다.
'string'(default)'number''date''dateTime''boolean''singleSelect''actions'
위에 보시다 시피 'time' 이 없습니다... ㅠ.ㅠ
구글링 해보니 아래 부분에
https://github.com/mui/mui-x/issues/5421 글에서
7월12일에 time 열이 추가 되었다고 하는것 같은데..
관련된 파일이 아래 gridDateColDef.ts, gridDateOperators.ts 두가지 파일로 보이며
커스터마이징이 필요할것을 보여집니다.
https://github.com/mui/mui-x/blob/master/packages/grid/x-data-grid/src/colDef/gridDateColDef.ts
import { gridDateComparator } from '../hooks/features/sorting/gridSortingUtils';
import { getGridDateOperators } from './gridDateOperators';
import { GRID_STRING_COL_DEF } from './gridStringColDef';
import { GridColTypeDef } from '../models/colDef/gridColDef';
import { renderEditDateCell } from '../components/cell/GridEditDateCell';
import { GridValueFormatterParams } from '../models/params/gridCellParams';
export function gridDateFormatter({ value }: GridValueFormatterParams<Date | string>) {
if (value instanceof Date) {
return value.toLocaleDateString();
}
return value ?? '';
}
export function gridDateTimeFormatter({ value }: GridValueFormatterParams<Date | string>) {
if (value instanceof Date) {
return value.toLocaleString();
}
return value ?? '';
}
export const GRID_DATE_COL_DEF: GridColTypeDef<Date | string, string> = {
...GRID_STRING_COL_DEF,
type: 'date',
sortComparator: gridDateComparator,
valueFormatter: gridDateFormatter,
filterOperators: getGridDateOperators(),
renderEditCell: renderEditDateCell,
getApplyQuickFilterFn: undefined,
};
export const GRID_DATETIME_COL_DEF: GridColTypeDef<Date | string, string> = {
...GRID_STRING_COL_DEF,
type: 'dateTime', <======= 이 타이블 수정
sortComparator: gridDateComparator,
valueFormatter: gridDateTimeFormatter,
filterOperators: getGridDateOperators(true),
renderEditCell: renderEditDateCell,
getApplyQuickFilterFn: undefined,
};
colDef 폴더에 gridDateColDef.ts 파일에서는 type 이 time 인 컬럼 속성은 보이지 않습니다.
mui x data grid 예시 파일을 열어서 아래
https://codesandbox.io/s/d13q7i?file=/demo.tsx
import * as React from "react";
import Box from "@mui/material/Box";
import { DataGrid, GridColTypeDef } from "@mui/x-data-grid";
import {
randomStatusOptions,
randomPrice,
randomCreatedDate
} from "@mui/x-data-grid-generator";
const rows = [
{
id: 1,
status: randomStatusOptions(),
subTotal: randomPrice(),
total: randomPrice(),
date: randomCreatedDate(),
time: "05:45"
},
{
id: 2,
status: randomStatusOptions(),
subTotal: randomPrice(),
total: randomPrice(),
date: randomCreatedDate(),
time: "05:45"
},
{
id: 3,
status: randomStatusOptions(),
subTotal: randomPrice(),
total: randomPrice(),
date: randomCreatedDate(),
time: "05:45"
}
];
const currencyFormatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
const usdPrice: GridColTypeDef = {
type: "number",
width: 200,
valueFormatter: ({ value }) => currencyFormatter.format(value),
cellClassName: "font-tabular-nums"
};
export default function CustomColumnTypesGrid() {
return (
<Box
sx={{
height: 300,
width: "100%",
"& .font-tabular-nums": {
fontVariantNumeric: "tabular-nums"
}
}}
>
<DataGrid
columns={[
{ field: "status", width: 130 },
{ field: "subTotal", ...usdPrice, editable: true },
{ field: "total", ...usdPrice },
{ field: "time", editable: true },
{ field: "date", type: "dateTime", width: 200, editable: true }
]}
rows={rows}
/>
</Box>
);
}
<DataGrid
columns={[
{ field: "status", width: 130 },
{ field: "subTotal", ...usdPrice, editable: true },
{ field: "total", ...usdPrice },
{ field: "time", editable: true },
{ field: "date", type: "dateTime", width: 200, editable: true }
]}
rows={rows}
/>



위에 일자와 시간을 선택할수 있는 셀렉터를
아래 time picker 만 나오도록 custumizing 할수 있는 팁을 주실수 있을까요?

回答 1
0
어쩌다보니 구글링해서 여기를 오게되어서 댓글 남깁니다.
js에서는 time이라는 오브젝트는 없다보니 Date로 하는 것이 일반적인 표준인 것 같습니다.
https://codesandbox.io/s/upbeat-liskov-kq6bnq?file=/demo.tsx
문제를 해결해본 코드샌드박스입니다.
공식 문서를 참조하시면 좋을 것 같습니다.
https://mui.com/x/react-data-grid/editing/
'S3' 형식에 'S3Client' 형식의 destroy, middlewareStack, send 속성이 없습니다.ts(2739)
0
509
1
throw new Error(&#x60;${this.name}.hasMany called with something that&#x27;s not a subclass of Sequelize.Model&#x60;); 에러 질문
0
343
1
안녕하세요.. connect ECONNREFUSED 127.0.0.1:3306 관련해서 질문드립니다
0
958
1
@types를 dependencies에 넣는 이유?
0
401
1
JS에서 babel 사용시 `import * as` 구문을 안써도되는데, 바벨이 esModuleInterop: true 로 처리해주는 것인가요?
0
318
1
Sequelize Association 오류
0
514
2
Sequelize constructor.primaryKeyAttributes 오류
0
973
1
Sequelize에 요청을 보내면 오류가 뜹니다
0
797
1
강좌에서 사용된 3개의 코드 의미가 궁금합니다 [ 코드 : 1. [ key:string]:string] 2. delete user.password , 3.passport.deserializeUser<number> ]
0
339
1
미들웨어 에러
1
313
1
Could not find a declaration file for mould '../utils/jwt-utils'
1
181
1
마지막에 type과 interface 추가하는 부분
0
218
1
다른 컴퓨터에서 제가 만든 DB 테이블의 데이터를 그대로 사용하려면?
0
282
1
express 모듈 에러 관련
0
241
1
import * as A from 'B'
0
216
1
MySql ssl 보안 옵션 질문 입니다.
0
353
2
passport/index.ts 에러 입니다
1
453
1
routes/post.ts 페이지에서 에러나요..
0
315
1
passport/index.ts 에러납니다...
0
818
1
ts import 에러
0
443
1
타입스크립트로 변환후 nextjs 빌드 후 배포
0
263
1
sequelize include 질문드립니다.!
0
192
1
roperty 'id' does not exist on type 'User'.
0
900
1
passport user.id 질문드립니다.
0
204
1

