import React from 'react';
import {
Form,
Input,
Button,
DatePicker,
Select,
Table,
Icon,
Radio,
InputNumber,
Checkbox,
Modal,
message,
notification,
Affix
} from 'antd';
import globalConfig from 'config.js';
import Logger from '../../utils/Logger';
import ajax from '../../utils/ajax';
const FormItem = Form.Item;
const ButtonGroup = Button.Group;
const logger = Logger.getLogger('InnerTable');
/**
* 内部表格组件
*/
class InnerTable extends React.Component {
// 很多时候都要在antd的组件上再包一层
state = {
modalVisible: false, // modal是否可见
modalTitle: '新增', // modal标题
modalInsert: true, // 当前modal是用来insert还是update
selectedRowKeys: [], // 当前有哪些行被选中, 这里只保存key
selectedRows: [], // 当前有哪些行被选中, 保存完整数据
};
/**
* InnerTable组件的重render有两种可能:
* 1. 上层组件调用的render方法, 这个时候会触发componentWillReceiveProps方法
* 2. 自身状态变化引起的重新render
* 注意区分
*
* 对于第一种情况, 要将组件的状态还原到初始状态
*
* @param nextProps
*/
componentWillReceiveProps = (nextProps) => {
logger.debug('receive new props and try to render, nextProps=%o', nextProps);
// 其实传入的props和当前的props可能是一样的, 这个方法不会判断修改才触发
// 要自己判断props是否有变化
// 蛋疼的是, 要区分是上层组件引起的这个方法调用还是InnerTable组件本身引起的
// 本来自身的变化是不应该触发的, 但不知antd的form组件做了什么, 每次给表单设置值的时候就会触发, 我讨厌黑盒...
// 利用了一个特性: 如果是上层触发的, tableLoading必定是true
if (nextProps.tableLoading === true) {
// 所有状态都要手动还原到初始值
// 这个方法里setState不会触发render
this.setState({
modalVisible: false,
modalTitle: '新增',
modalInsert: true,
selectedRowKeys: [],
selectedRows: [],
});
}
}
/**
* 点击新增按钮, 弹出一个内嵌表单的modal
*
* @param e
*/
onClickInsert = (e) => {
e.preventDefault();
this.props.form.resetFields();
this.setState({modalVisible: true, modalTitle: '新增', modalInsert: true});
}
/**
* 点击更新按钮, 弹出一个内嵌表单的modal
* 注意区分单条更新和批量更新
*
* @param e
*/
onClickUpdate = (e) => {
e.preventDefault();
this.props.form.resetFields();
const multiSelected = this.state.selectedRowKeys.length > 1; // 是否选择了多项
// 如果只选择了一项, 就把原来的值填到表单里
// 否则就只把要更新的主键填到表单里
if (!multiSelected) {
logger.debug('update single record, and fill original values');
this.props.form.setFieldsValue(this.state.selectedRows.pop()); // FIXME: 理论上来说不应该这样去修改state中的值
} else {
const tmpObj = {};
tmpObj[this.primaryKey] = this.state.selectedRowKeys.join(', ');
logger.debug('update multiple records, keys = %s', tmpObj[this.primaryKey]);
this.props.form.setFieldsValue(tmpObj);
}
if (multiSelected) {
this.setState({modalVisible: true, modalTitle: '批量更新', modalInsert: false});
} else {
this.setState({modalVisible: true, modalTitle: '更新', modalInsert: false});
}
}
/**
* 点击删除按钮, 弹出一个确认对话框
* 注意区分单条删除和批量删除
*
* @param e
*/
onClickDelete = (e) => {
e.preventDefault();
Modal.confirm({
title: this.state.selectedRowKeys.length > 1 ? '确认批量删除' : '确认删除',
content: `当前被选中的行: ${this.state.selectedRowKeys.join(', ')}`,
// 这里注意要用箭头函数, 否则this不生效
onOk: () => {
this.handleDelete();
},
});
}
/**
* 隐藏modal
*/
hideModal = () => {
this.setState({modalVisible: false});
}
/**
* 点击modal中确认按钮的回调
*/
handleModalOk = () => {
// 将表单中的undefined去掉
// 表单传过来的主键是逗号分隔的字符串, 这里转换成数组
const newObj = {};
const primaryKeyArray = [];
const oldObj = this.props.form.getFieldsValue();
for (const key in oldObj) {
if (!oldObj[key])
continue;
if (key === this.primaryKey && typeof oldObj[key] === 'string') {
for (const str of oldObj[key].split(', ')) {
// 按schema中的约定, 主键只能是int/varchar
if (this.primaryKeyType === 'int') {
primaryKeyArray.push(parseInt(str));
} else {
primaryKeyArray.push(str);
}
}
} else {
newObj[key] = oldObj[key];
}
}
if (this.primaryKey && primaryKeyArray.length > 0) {
newObj[this.primaryKey] = primaryKeyArray;
}
this.hideModal();
logger.debug('click modal OK and the form obj = %o', newObj);
if (this.state.modalInsert) {
this.handleInsert(newObj);
} else {
this.handleUpdate(newObj);
}
}
/*下面开始才是真正的数据库操作*/
/**
* 真正去处理新增数据
*/
handleInsert = (obj) => {
const url = `${globalConfig.getAPIPath()}/${this.props.tableName}/insert`;
const hide = message.loading('正在新增...', 0);
logger.debug('handleInsert: url = %s, obj = %o', url, obj);
ajax.post(url).send(obj).end((err, res) => {
hide();
logger.debug('handleInsert: return error = %o, res = %o', err, res);
// err就是一个字符串
// res是一个Response对象, 其中的body字段才是服务端真正返回的数据
if (err || !res.body.success) {
notification.error({
message: '新增失败',
description: err ? '请求insert接口出错, 请联系管理员' : `请联系管理员, 错误信息: ${res.body.message}`,
duration: 0,
});
} else {
notification.success({
message: '新增成功',
description: this.primaryKey ? `新增数据行 主键=${res.body.data[this.primaryKey]}` : '',
duration: 0,
});
}
});
}
/**
* 真正去更新数据
*/
handleUpdate = (obj) => {
const keys = obj[this.primaryKey];
const url = `${globalConfig.getAPIPath()}/${this.props.tableName}/update?keys=${keys instanceof Array ? keys.join(',') : keys}`;
const hide = message.loading('正在更新...', 0);
obj[this.primaryKey] = undefined;
logger.debug('handleUpdate: url = %s, obj = %o', url, obj);
ajax.post(url).send(obj).end((err, res) => {
hide();
logger.debug('handleUpdate: return error = %o, res = %o', err, res);
if (err || !res.body.success) {
notification.error({
message: '更新失败',
description: err ? '请求update接口出错, 请联系管理员' : `请联系管理员, 错误信息: ${res.body.message}`,
duration: 0,
});
} else {
notification.success({
message: '更新成功',
description: `更新${res.body.data}条数据`,
duration: 0,
});
// 更新数据后, 注意刷新下整个页面
this.props.refresh();
}
});
}
/**
* 真正去删除数据
*/
handleDelete = () => {
const url = `${globalConfig.getAPIPath()}/${this.props.tableName}/delete?keys=${this.state.selectedRowKeys.join(',')}`;
const hide = message.loading('正在删除...', 0);
logger.debug('handleDelete: url = %s', url);
ajax.post(url).end((err, res) => {
hide();
logger.debug('handleDelete: return error = %o, res = %o', err, res);
if (err || !res.body.success) {
notification.error({
message: '删除失败',
description: err ? '请求delete接口出错, 请联系管理员' : `请联系管理员, 错误信息: ${res.body.message}`,
duration: 0,
});
} else {
notification.success({
message: '删除成功',
description: `删除${res.body.data}条数据`,
duration: 0,
});
// 更新数据后, 注意刷新下整个页面
this.props.refresh();
}
});
}
/**
* 辅助函数
*
* @param formItem
* @param field
* @returns {XML}
*/
colWrapper = (formItem, field) => {
//const {getFieldProps, getFieldError, isFieldValidating} = this.props.form;
return (