<Column />
태그 속성으로 데이터 포맷하기type : String
import React from 'react';
import { DataGrid, Column } from 'devextreme-react/data-grid';
const employees = [{
ID: '1',
hireDate: 1491821760000
}, // ...
];
class App extends React.Component {
render() {
return (
<DataGrid ...
dataSource={employees}>
<Column dataField="ID" dataType="number" />
<Column dataField="hireDate" dataType="date" />
</DataGrid>
);
}
}
export default App;
dataType=""
이러한 속성을 통해 데이터의 type을 지정할 수 있다.'string' | 'number' | 'date' | 'boolean' | 'object' | 'datetime'
type : format
// Uses a predefined format
format: {
type: String, // one of the predefined formats
precision: Number, // the precision of values
currency: String // a specific 3-letter code for the "currency" format
}
Format
타입에서 알아보자.formatter: function (date) {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${day}.${month}.${year}`;
},
parser: function (e) {
const parts = e.split(".");
const day = Number(parts[0]);
const month = Number(parts[1] - 1);
const year = Number(parts[2]);
return new Date(year, month, day);
}
parser
와 formatter
를 통해 더욱더 디테일하게 파싱할 수 있다.type
을 사용하면 된다.const smallNumber = 1.2345;
type: "fixedPoint" // 1
type: "decimal" // 1.2345
type: "percent" // 123%
type: "currency" // $1
const largeNumber = 1000000000;
type: "exponential" // 1.0E+9
type: "thousands" // 1,000,000K
type: "millions" // 1,000M
type: "billions" // 1B
type: "trillions" // 0T
type: "largeNumber" // 1B (uses "thousands", "millions", "billions", or "trillions", depending on the value)
const date = new Date(2021, 6, 15, 20, 45, 34);
type: "longDate" // Thursday, July 15, 2021
type: "longTime" // 8:45:34 PM
type: "longDateLongTime" // Thursday, July 15, 2021, 8:45:34 PM
type: "monthAndDay" // July 15
type: "monthAndYear" // July 2021
type: "quarterAndYear" // Q3 2021
type: "shortDate" // 7/15/2021
type: "shortTime" // 8:45 PM
type: "shortDateShortTime" // 7/15/2021, 8:45 PM
type: "millisecond" // 000
type: "second" // 34
type: "minute" // 45
type: "hour" // 20
type: "day" // 15
type: "dayOfWeek" // Thursday
type: "month" // July
type: "quarter" // Q3
type: "year" // 2021
<column />
에 대한 format 속성에 대해서 알아보자.type : String
Format character | Description |
---|---|
0 | A digit. Displays '0' if the formatted number does not have a digit in that position. |
# | Any number of leading digits, a single digit, or nothing. If this character goes first in the format string, it can match multiple leading digits (before the decimal point). Subsequent characters match a single digit. If the formatted number does not have a digit in the corresponding position, it displays nothing. For example, if you apply format "#0.#" to "123.45", the result is "123.4". |
. | A decimal separator. Actual character depends on locale. |
, | A group separator. Actual character depends on locale. |
% | The percent sign. Multiplies the input value by 100. |
; | Separates positive and negative format patterns. For example, the "#0.##;(#0.##)" format displays a positive number according to the pattern before the semicolon (";"), and a negative number according to the pattern after the semicolon (";"). If you do not use this character and the additional pattern, negative numbers display a minus ("-") prefix. |
Escape characters | You can display the special characters above as literals if you enclose them in single quotation marks. For example, '%'. |
Other characters | You can add any literal characters to the beginning or end of the format string. |
const number = 1234.567;
// Leave the first digit before the decimal point and round up the decimal
format: "0.0" // 4.6
const smallNumber = 0.1234;
// Display nothing in place of a digit
format: "#.#" // .1
const largeNumber = 123456.789;
// Add a group separator
format: ",##0.###" // 123,456.789
const smallNumber = 0.01234;
// Represent as a percentage and limit to two decimal digits
format: "#0.##%" // 1.23%
// Add a percent sign and limit to two decimal digits
format: "#0.##'%'" // 0.01%
Format character | Description |
---|---|
y | A calendar year. |
Q | A quarter number or name. Available combinations with example: "Q" - "2", "QQ" - "02", "QQQ" - "Q2" and "QQQQ" - "2nd quarter". |
M | A month number or name. Available combinations with example: "M" - "9", "MM" - "09", "MMM" - "Sep", "MMMM" - "September", "MMMMM" - "S". |
d | A month day. |
E | A week day name. Available combinations with example: "E", "EE" or "EEE" - "Tue", "EEEE" - "Tuesday", "EEEEE" - "T". |
a | A day period (am or pm). |
h | An hour. From 1 to 12. |
H | An hour. From 0 to 23. |
m | A minute. |
s | A second. |
S | A fractional second. |
'' (two single quotes) | Literal text. Text enclosed in two single quotes is shown as-is. |
const date = new Date(2021, 6, 15, 20, 45, 34);
format: "MM/dd/yyyy" // 07/15/2021
format: "MM/dd/yy" // 07/15/21
format: "dd.MM.yyyy" // 15.07.2021
format: "MMMM dd, yyyy" // July 15, 2021
format: "EEEE, MMMM dd" // Thursday, July 15
format: "HH:mm:ss" // 20:45:34
format: "hh:mm a" // 08:45 PM
format: "MMMM dd, yyyy HH:mm:ss" // July 15, 2021 20:45:34
<Column
caption="블라블라"
alignment="center"
dataType="date"
format="yyyy-MM-dd"
/>
<Column caption="블라블라" alignment="right" dataType="number">
<Format type="fixedPoint" />
</Column>
<Format />
컴포넌트를 사용해서 이런 형식으로 데이터 포맷을 할 수도 있다.date
타입으로 포맷할 시에는 가장 간단하다.