npm install ka-table
import React, { useState } from "react";
import { ITableProps, kaReducer, Table } from "ka-table";
import { DataType } from "ka-table/enums";
import { DispatchFunc } from "ka-table/types";
const TableDemo: React.FC = () => {
const [tableProps, changeTableProps] = useState(tablePropsInit);
const dispatch: DispatchFunc = (action) => {
changeTableProps((prevState: ITableProps) => kaReducer(prevState, action));
};
return (
<Table
{...tableProps}
dispatch={dispatch}
/>
);
};
export default TableDemo;
-> 초기값을 정해주기 위한 함수를 만든다.
-> 이곳에는 당연히 행, 열에 무엇이 들어갈지를 정의한다.
const tablePropsInit: ITableProps = {
columns: [{}],
data: dataArray,
rowKeyField: 'id'
}
columns: [
{
key: "column1-1",
width: 150,
field: "column1",
title: "희망부서 1",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column1-2",
width: 150,
field: "column1",
title: "희망부서 2",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column2",
width: 150,
field: "column2",
title: "희망부서 3",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column9",
width: 107,
title: "조치",
dataType: DataType.String,
style: { textAlign: "center" },
}
]
data: dataArray,
const dataArray = Array(10)
.fill(undefined)
.map((_, index) => ({
column1: "Aromatic1Unit",
column2: "Aromatic2Unit",
id: index,
}));
rowKeyField: "id",
const tablePropsInit: ITableProps = {
columns: [
{
key: "column1-1",
width: 150,
field: "column1",
title: "희망부서 1",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column1-2",
width: 150,
field: "column1",
title: "희망부서 2",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column2",
width: 150,
field: "column2",
title: "희망부서 3",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column9",
width: 107,
title: "조치",
dataType: DataType.String,
style: { textAlign: "center" },
},
],
data: dataArray,
rowKeyField: "id",
};
-> 버튼 말고도 그냥 내용을 입력하기 위해서 사용해도 됨!
-> table에 속성 값으로 넣으면 끝
<Table
{...tableProps}
childComponents={{
cellText: {
content: (props) => {
switch (props.column.key) {
case "column9":
return <button>입력</button>;
}
},
},
}}
dispatch={dispatch}
/>
import "./Demo.scss";
import React, { useState } from "react";
import { ITableProps, kaReducer, Table } from "ka-table";
import { DataType } from "ka-table/enums";
import { DispatchFunc } from "ka-table/types";
const dataArray = Array(10)
.fill(undefined)
.map((_, index) => ({
column1: "Aromatic1Unit",
column2: "Aromatic2Unit",
id: index,
}));
const tablePropsInit: ITableProps = {
columns: [
{
key: "column1-1",
width: 150,
field: "column1",
title: "희망부서 1",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column1-2",
width: 150,
field: "column1",
title: "희망부서 2",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column2",
width: 150,
field: "column2",
title: "희망부서 3",
dataType: DataType.String,
style: { textAlign: "center" },
},
{
key: "column9",
width: 107,
title: "조치",
dataType: DataType.String,
style: { textAlign: "center" },
},
],
data: dataArray,
rowKeyField: "id",
};
const TableDemo: React.FC = () => {
const [tableProps, changeTableProps] = useState(tablePropsInit);
const dispatch: DispatchFunc = (action) => {
changeTableProps((prevState: ITableProps) => kaReducer(prevState, action));
};
return (
<Table
{...tableProps}
childComponents={{
cellText: {
content: (props) => {
switch (props.column.key) {
case "column9":
return <button>입력</button>;
}
},
},
}}
dispatch={dispatch}
/>
);
};
export default TableDemo;