editable 구현 관련 문의 드립니다.
606
작성한 질문수 74
antd 의 테이블 구현 관련 궁금한 부분을 문의 드리고 싶습니다.
antd 의 EditableTable, ResizableTitle, infinite scroll(react)를 적용해 보려하고 있습니다.
1. 테이블 헤드 가로 사이즈 변경이 먹지 않습니다. (예제 코드를 약간 변경해서 적용했습니다.)
2. 편집시 <td> padding과 Edit button 클릭시 td 안에 <input> padding 을 유지시 셀의 세로 높이가 바뀌는 문제가 있어서
Edit button 클릭시에도 동일하게 padding를 유지하도록 <td> className을 주어 padding를 주려고 합니다.
어느 부분이 잘못되어 있는지 코드 체크 부탁드립니다.
// import { VariableSizeGrid as Grid } from 'react-window';
// import ResizeObserver from 'rc-resize-observer';
// import classNames from 'classnames';
import React, { useState, useEffect, useRef } from 'react';
import { Resizable } from 'react-resizable';
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
import styled, { createGlobalStyle } from 'styled-components';
const Global = createGlobalStyle`
.editable-row,
.ant-form-item-explain {
position: absolute;
top: 100%;
font-size: 12px;
}
table thead tr th { height: 10px;}
table tbody tr th { height: 10px;}
.editable row { height: 10px;}
#components-table-demo-resizable-column .react-resizable {
position: relative;
background-clip: padding-box;
}
#components-table-demo-resizable-column .react-resizable-handle {
position: absolute;
right: -5px;
bottom: 0;
z-index: 1;
width: 10px;
height: 100%;
cursor: col-resize;
}
.antd-table > tr > td {
padding: 0;
}
.antd-form-control-input-content > input {
padding: 11px 11px;
}
`;
const originData = [];
for (let i = 0; i < 100; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: i,
address: `London Park no. ${i}`,
// description: name + ' ' + age +' '+ address,
});
}
const ResizableTitle = props => {
const { onResize, width, ...restProps } = props;
if (!width) {
return <th {...restProps} />;
}
return (
<Resizable
width={width}
height={0}
handle={
<span
className="react-resizable-handle"
onClick={e => {
e.stopPropagation();
}}
/>
}
onResize={onResize}
draggableOpts={{ enableUserSelectHack: false }}
>
<th {...restProps} />
</Resizable>
);
};
const EditableCell = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
const inputNode = inputType === 'number' ? <InputNumber /> : <Input />;
return (
========================================
{editing ?
(
<td {...restProps} className="padding0">
<Form.Item
name={dataIndex}
style={{
margin: 0,
}}
rules={[
{
required: true,
message: `Please Input ${title}!`,
},
]}
>
{inputNode}
</Form.Item>
</td>
) : (
<td {...restProps}>
children
</td>
)
}
);
};
============================================
const EditableTable = () => {
const [form] = Form.useForm();
const [data, setData] = useState(originData);
const [editingKey, setEditingKey] = useState('');
const isEditing = (record) => record.key === editingKey;
const edit = (record) => {
form.setFieldsValue({
name: '',
age: '',
address: '',
...record,
});
setEditingKey(record.key);
};
const cancel = () => {
setEditingKey('');
};
const save = async (key) => {
try {
const row = await form.validateFields();
const newData = [...data];
const index = newData.findIndex((item) => key === item.key);
if (index > -1) {
const item = newData[index];
newData.splice(index, 1, { ...item, ...row });
setData(newData);
setEditingKey('');
} else {
newData.push(row);
setData(newData);
setEditingKey('');
}
} catch (errInfo) {
console.log('Validate Failed:', errInfo);
}
};
const columns = [
{
title: 'name',
dataIndex: 'name',
width: 100,
editable: true,
sorter: (a, b) => a.name.length - b.name.length,
},
{
title: 'age',
dataIndex: 'age',
width: 80,
editable: true,
sorter: (a, b) => a.age - b.age,
},
{
title: 'address',
dataIndex: 'address',
width: 200,
editable: true,
sorter: (a, b) => a.address.length - b.address.length,
},
{
title: 'operation',
dataIndex: 'operation',
render: (_, record) => {
const editable = isEditing(record);
return editable ? (
<span>
<a
href="javascript:;"
onClick={() => save(record.key)}
style={{
marginRight: 8,
}}
>
Save
</a>
{/* <Popconfirm title="Sure to cancel?" onConfirm={cancel}>
<a>Cancel</a>
</Popconfirm> */}
<a href="javascript:;" onClick={cancel} style={{ marginRight: 8 }}>
Cancel
</a>
</span>
) : (
<a disabled={editingKey !== ''} onClick={() => edit(record)}>
Edit
</a>
);
},
},
];
const handleResize = index => (e, { size }) => {
this.setState(({ columns }) => {
const nextColumns = [...columns];
nextColumns[index] = {
...nextColumns[index],
width: size.width,
};
return { columns: nextColumns };
});
};
const mergedColumns = columns.map((col, index) => {
if (!col.editable) {
return col;
}
return {
...col,
onHeaderCell: column => ({
width: column.width,
onResize: handleResize(index),
}),
onCell: (record) => ({
record,
inputType: col.dataIndex === 'age' ? 'number' : 'text',
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
const onChange = (pagination, filters, sorter, extra) => {
console.log('params', pagination, filters, sorter, extra);
}
return (
<Form form={form} component={false}>
<Table
components={{
header: {
cell: ResizableTitle,
},
body: {
cell: EditableCell,
},
}}
expandable={{
expandedRowRender: record => <p style={{ margin: 0 }}>{record.description}</p>,
rowExpandable: record => record.name !== 'Not Expandable',
}}
bordered
dataSource={data}
columns={mergedColumns}
rowClassName="editable-row"
onChange={onChange}
scroll={{ y: 400 }}
pagination={false}
/>
</Form>
);
};
export default EditableTable;
답변 0
넥스트 버젼 질문
0
77
2
로그인시 401 Unauthorized 오류가 뜹니다
0
89
1
무한 스크롤 중 스크롤 튐 현상
0
175
1
특정 페이지 접근을 막고 싶을 때
0
103
2
createGlobalStyle의 위치와 영향범위
0
96
2
인라인 스타일 리렌더링 관련
0
91
2
vsc 에서 npm init 설치시 오류
0
146
2
nextjs 15버전 사용 가능할까요?
0
158
1
화면 새로고침 문의
0
121
1
RTK에서 draft, state 차이가 있나요?
0
153
2
Next 14 사용해도 될까요?
0
452
1
next, node 버전 / 폴더 구조 질문 드립니다.
0
349
1
url 오류 질문있습니다
0
211
1
ssh xxxxx로 우분투에 들어가려니까 port 22: Connection timed out
0
372
1
sudo certbot --nginx 에러
0
1274
2
Minified React error 콘솔에러 (hydrate)
0
469
1
카카오 공유했을 때 이전에 작성했던 글이 나오는 버그
0
247
1
프론트서버 배포 후 EADDRINUSE에러 발생
0
326
1
npm run build 에러
0
518
1
front 서버 npm run build 중에 발생한 에러들
0
381
1
서버 실행하고 브라우저로 들어갔을때 404에러
0
338
2
css 서버사이드 랜더링이 적용되지 않아서 문의 드립니다.
0
287
1
팔로워 3명씩 불러오고 데이터 합쳐주는걸로 바꾸고 서버요청을 무한으로하고있습니다.
0
237
2
해시태그 검색에서 throttle에 관해 질문있습니다.
0
201
1





