spring-cloud/admin-web/src/pages/Product/ProductAttrList.js

159 lines
4.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import React, { PureComponent, Fragment, Component } from 'react';
import { Form, Card, Table, Button, Divider, Modal, Input } from 'antd';
import moment from 'moment';
import { connect } from 'dva';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import PaginationHelper from '../../../helpers/PaginationHelper';
import styles from './ProductAttrList.less';
const FormItem = Form.Item;
const CreateForm = Form.create()(props => {
const { modalVisible, form, handleAdd, handleModalVisible, modalType, initValues } = props;
const okHandle = () => {
form.validateFields((err, fieldsValue) => {
if (err) return;
let pid = fieldsValue.pid;
if (fieldsValue.pid) {
pid = pid.split('-')[1];
fieldsValue.pid = pid;
}
form.resetFields();
handleAdd({
fields: fieldsValue,
modalType,
initValues,
});
});
};
const selectStyle = {
width: 200,
};
function onTypeChange(event) {
initValues.type = parseInt(event.target.value);
}
const title = modalType === 'add' ? '' : '';
const okText = modalType === 'add' ? '' : '';
return (
<Modal
destroyOnClose
title={title}
visible={modalVisible}
onOk={okHandle}
okText={okText}
onCancel={() => handleModalVisible()}
>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="规格名称">
{form.getFieldDecorator('name', {
initialValue: initValues ? initValues.name : null,
rules: [{ required: true, message: '', min: 2 }],
})(<Input placeholder="规格名称" />)}
</FormItem>
</Modal>
);
});
@connect(({ productAttrList, loading }) => ({
productAttrList,
attrData: productAttrList.attrData,
loading: loading.models.productAttrList,
}))
@Form.create()
export default class ProductAttrList extends PureComponent {
state = {
modalVisible: false,
modalType: 'add', //add or update
initValues: {},
};
componentDidMount() {
const { dispatch } = this.props;
dispatch({
type: 'productAttrList/page',
payload: {
...PaginationHelper.defaultPayload,
},
});
}
handleModalVisible = (flag, modalType, initValues) => {
this.setState({
modalVisible: !!flag,
initValues: initValues || {},
modalType: modalType || 'add',
});
};
render() {
const { attrData, productAttrList, loading, pagination } = this.props;
const columns = [
{
title: '',
dataIndex: 'name',
},
{
title: '',
dataIndex: 'status',
render: val => <span>{val === 1 ? '' : ''}</span>,
},
{
title: '',
dataIndex: 'createTime',
sorter: true,
render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
},
{
title: '',
render: (text, record) => (
<Fragment>
<a onClick={() => this.handleModalVisible(true, 'update', record)}></a>
<Divider type="vertical" />
<a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
</a>
</Fragment>
),
},
];
const { modalVisible, modalType, initValues } = this.state;
const parentMethods = {
handleAdd: this.handleAdd,
handleModalVisible: this.handleModalVisible,
modalType,
initValues,
};
return (
<PageHeaderWrapper>
<Card>
<div className={styles.tableList}>
<div className={styles.tableListOperator}>
<Button
icon="plus"
type="primary"
onClick={() => this.handleModalVisible(true, 'add', {})}
>
</Button>
</div>
</div>
<Table
defaultExpandAllRows={true}
columns={columns}
dataSource={attrData.attrs ? attrData.attrs : []}
rowKey="id"
loading={loading}
pagination={pagination}
/>
</Card>
{modalVisible ? <CreateForm {...parentMethods} modalVisible={modalVisible} /> : null}
</PageHeaderWrapper>
);
}
}