intimate_view/src/Table.js

248 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-08-18 11:22:29 +00:00
import React, { useState } from 'react';
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
import reqwest from 'reqwest';
import './Table.less';
const originData = [];
// for (let i = 0; i < 200; i++) {
// originData.push({
// key: i.toString(),
// name: `Edrward ${i}`,
// age: 32,
// address: `London Park no. ${i}`,
// });
// }
const EditableCell = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
const inputNode = inputType === 'number' ? <InputNumber /> : <Input />;
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
style={{
margin: 0,
}}
rules={[
{
required: true,
message: `Please Input ${title}!`,
},
]}
>
{inputNode}
</Form.Item>
) : (
children
)}
</td>
);
};
const DataTable = () => {
const [form] = Form.useForm();
const [data, setData] = useState(originData);
const [editingKey, setEditingKey] = useState('');
fetch = (params = {}) => {
this.setState({ loading: true });
reqwest({
url: 'http://localhost:5500/twitcasting/data?page=0',
method: 'get',
type: 'json',
data: (params),
}).then(data => {
console.log(data);
this.setState({
loading: false,
data: data.results,
pagination: {
...params.pagination,
total: data.totalCount,
// 200 is mock data, you should read it from server
// total: data.totalCount,
},
});
});
};
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: 'uid',
dataIndex: 'uid',
editable: false,
},
{
title: '平台',
dataIndex: 'platform',
editable: false,
},
{
title: '平台id',
dataIndex: 'user_id',
editable: false,
},
{
title: '平台名称',
dataIndex: 'user_name',
editable: false,
},
{
title: '直播地址',
dataIndex: 'live_url',
editable: false,
},
{
title: '标签',
dataIndex: 'tags',
editable: false,
},
{
title: '粉丝数(关注)',
dataIndex: 'followers',
editable: true,
},
{
title: '礼物数(币|钱)',
dataIndex: 'gratuity',
editable: true,
},
{
title: '直播标题',
dataIndex: 'live_title',
editable: true,
},
{
title: '近直播开始时间',
dataIndex: 'live_start_time',
editable: true,
},
{
title: '近直播结束时间',
dataIndex: 'live_end_time',
editable: true,
},
{
title: '数据更新时间',
dataIndex: 'update_time',
editable: true,
},
{
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>
</span>
) : (
<a disabled={editingKey !== ''} onClick={() => edit(record)}>
Edit
</a>
);
},
},
];
const mergedColumns = columns.map(col => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
inputType: col.dataIndex === 'age' ? 'number' : 'text',
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
return (
<Form form={form} component={false}>
<Table size="small"
pagination={{
pageSize: 20,
position: ["topLeft"],
onChange: cancel,
}}
components={{
body: {
cell: EditableCell,
},
}}
bordered
dataSource={data}
columns={mergedColumns}
rowClassName="editable-row"
/>
</Form>
);
};
export default DataTable;
//