You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.2 KiB

10 years ago
'use strict';
10 years ago
import React from 'react';
import jQuery from 'jquery';
10 years ago
import Table from 'rc-table';
let AntTable = React.createClass({
10 years ago
getInitialState() {
return {
selectedRowKeys: [],
loading: false,
data: []
10 years ago
};
},
10 years ago
getDefaultProps() {
return {
prefixCls: 'ant-table',
10 years ago
useFixedHeader: false,
10 years ago
rowSelection: null,
size: 'normal'
10 years ago
};
},
10 years ago
handleSelect(e) {
let checked = e.currentTarget.checked;
let currentRowIndex = e.currentTarget.parentElement.parentElement.rowIndex;
let selectedRow = this.props.data[currentRowIndex - 1];
if (checked) {
this.state.selectedRowKeys.push(currentRowIndex);
} else {
this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i){
return currentRowIndex !== i;
});
}
this.setState({
selectedRowKeys: this.state.selectedRowKeys
});
if (this.props.rowSelection.onSelect) {
this.props.rowSelection.onSelect(selectedRow, checked);
}
},
handleSelectAllRow(e) {
let checked = e.currentTarget.checked;
this.setState({
selectedRowKeys: checked ? this.props.data.map(function(item, i) {
return i + 1;
}) : []
});
if (this.props.rowSelection.onSelectAll) {
this.props.rowSelection.onSelectAll(checked);
}
},
renderSelectionCheckBox(value, record, index) {
let checked = this.state.selectedRowKeys.indexOf(index + 1) >= 0;
let checkbox = <input type="checkbox" checked={checked} onChange={this.handleSelect} />;
return checkbox;
},
loadData: function() {
this.props.resolve = this.props.resolve || function(data) {
return data || [];
};
if (this.props.dataSource) {
this.setState({
loading: true
});
jQuery.ajax({
url: this.props.dataSource,
success: (result) => {
result = this.props.resolve.call(this, result);
if (this.isMounted()) {
this.setState({
data: result
});
}
},
complete: () => {
this.setState({
loading: false
});
}
});
}
},
componentDidMount() {
this.loadData();
},
10 years ago
render() {
10 years ago
if (this.props.rowSelection) {
let checked = this.props.data.every(function(item, i) {
return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
}, this);
let checkboxAll = <input type="checkbox" checked={checked} onChange={this.handleSelectAllRow} />;
let selectionColumn = {
key: 'selection-column',
title: checkboxAll,
render: this.renderSelectionCheckBox
};
if (this.props.columns[0] &&
this.props.columns[0].key === 'selection-column') {
this.props.columns[0] = selectionColumn;
} else {
this.props.columns.unshift(selectionColumn);
}
}
10 years ago
var classString = '';
if (this.props.loading) {
classString += ' ant-table-loading';
}
if (this.props.size === 'small') {
classString += ' ant-table-small';
}
// 'message message-important message-read'
return <Table data={this.state.data}
className={classString}
{...this.props} />;
10 years ago
}
});
export default AntTable;