|
|
@@ -0,0 +1,135 @@
|
|
|
+import React from 'react';
|
|
|
+import ReactDOM from 'react-dom';
|
|
|
+import Table from '../src/Table/index.js';
|
|
|
+import Animate from 'rc-animate';
|
|
|
+import {isEqual} from '../utils/BaseUtils.js';
|
|
|
+import '../assets/Table/index.less';
|
|
|
+import '../assets/Table/animation.less';
|
|
|
+import {sort} from '../utils/BaseUtils.js';
|
|
|
+
|
|
|
+class TableModel extends React.Component {
|
|
|
+ constructor(props) {
|
|
|
+ super(props);
|
|
|
+ this.newProps = props;
|
|
|
+ this.columns = this.setColumnsRender(props.columns);
|
|
|
+ this.state = {
|
|
|
+ data: []
|
|
|
+ };
|
|
|
+ this.sorts = []; // 排序规则
|
|
|
+ }
|
|
|
+ // 初始化检索columns,获得排序规则
|
|
|
+ initSort() {
|
|
|
+ // 先重置
|
|
|
+ this.sorts = [];
|
|
|
+
|
|
|
+ let cols = this.columns;
|
|
|
+ let i = 0;
|
|
|
+ for (i; i < cols.length; i++) {
|
|
|
+ let col = cols[i];
|
|
|
+ if (Math.abs(col['sort']) > 0) {
|
|
|
+ this.sorts.push({
|
|
|
+ sortKey: col['dataIndex'],
|
|
|
+ sortLevel: Math.abs(col['sort']), // 仅用于规则排序
|
|
|
+ direction: col['sort'] > 0 ? 1 : -1
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 把排序规则排序
|
|
|
+ sort(this.sorts, [{ key: 'sortLevel', direction: 1 }]);
|
|
|
+ }
|
|
|
+ // 根据sort值分层排序data
|
|
|
+ sortData(data) {
|
|
|
+ let sortArray = this.sorts.map(function (s, i) {
|
|
|
+ return {
|
|
|
+ key: s.sortKey,
|
|
|
+ direction: s.direction
|
|
|
+ };
|
|
|
+ });
|
|
|
+ if (sortArray) { }
|
|
|
+ let sortData = sort(data || [], sortArray);
|
|
|
+ return sortData;
|
|
|
+ }
|
|
|
+ // 根据renderName设置每列的render
|
|
|
+ setColumnsRender(columns) {
|
|
|
+ let cols = columns;
|
|
|
+ // 如果需要增加序号列
|
|
|
+ if (this.newProps.index) {
|
|
|
+ cols.unshift({
|
|
|
+ key: 'index',
|
|
|
+ title: '序号',
|
|
|
+ render: function(v,r,i) {
|
|
|
+ return {
|
|
|
+ children: i+1,
|
|
|
+ props: {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ // 根据render方法名获得对应方法实体
|
|
|
+ for (let i = 0; i < cols.length; i++) {
|
|
|
+ let col = cols[i];
|
|
|
+ let renderFunction = col.render ? col.render : this.newProps.render ? this.newProps.render : function (_v, _r, _i) { return { children: _v, props: {} } };
|
|
|
+ col.render = renderFunction;
|
|
|
+ }
|
|
|
+ return cols;
|
|
|
+ }
|
|
|
+ componentWillMount() {
|
|
|
+ }
|
|
|
+ componentDidMount() {
|
|
|
+ this.initSort();
|
|
|
+ this.setState({
|
|
|
+ data: this.sortData(this.newProps.data)
|
|
|
+ });
|
|
|
+ }
|
|
|
+ componentWillUnmount() {
|
|
|
+ }
|
|
|
+ componentWillReceiveProps(nextProps) {
|
|
|
+ if (isEqual(nextProps.data, this.newProps.data)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.newProps = nextProps;
|
|
|
+ this.columns = this.setColumnsRender(this.newProps.columns);
|
|
|
+ this.initSort();
|
|
|
+ this.setState({
|
|
|
+ data: this.sortData(this.newProps.data)
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ shouldComponentUpdate(nextProps, nextState) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ getBodyWrapper(body) {
|
|
|
+ return (
|
|
|
+ <Animate transitionName="fade" component="tbody" className={body.props.className}
|
|
|
+ transitionAppear={true}
|
|
|
+ transitionLeave={true}
|
|
|
+ >
|
|
|
+ {body.props.children}
|
|
|
+ </Animate>
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ getTitle() {
|
|
|
+ const { title } = this.newProps;
|
|
|
+ return title;
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ const { fontSize } = this.newProps;
|
|
|
+ return (
|
|
|
+ <div ref='body' style={{ width: '100%', height: '100%', overflowX: 'hidden', overflowY: 'scroll', padding: '0px' }}>
|
|
|
+ <Table
|
|
|
+ title={this.getTitle()}
|
|
|
+ emptyText={this.newProps.emptyText || 'No Data'}
|
|
|
+ columns={this.columns || []}
|
|
|
+ data={this.state.data || []}
|
|
|
+ getBodyWrapper={this.getBodyWrapper}
|
|
|
+ headerRowsStyle={this.newProps.headerRowsStyle}
|
|
|
+ rowsStyle={this.newProps.rowsStyle}
|
|
|
+ scroll={{y: 400 }}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+export default TableModel;
|