ant-design/components/table/index.jsx

109 lines
3.0 KiB
React
Raw Normal View History

2015-07-10 17:47:53 +08:00
'use strict';
2015-07-09 11:41:36 +08:00
import React from 'react';
2015-07-12 17:10:06 +08:00
import jQuery from 'jquery';
2015-07-09 11:41:36 +08:00
import Table from 'rc-table';
let AntTable = React.createClass({
2015-07-10 17:47:53 +08:00
getInitialState() {
return {
2015-07-12 17:10:06 +08:00
selectedRowKeys: [],
loading: false,
data: []
2015-07-10 17:47:53 +08:00
};
},
2015-07-09 11:41:36 +08:00
getDefaultProps() {
return {
2015-07-09 14:51:48 +08:00
prefixCls: 'ant-table',
2015-07-10 17:47:53 +08:00
useFixedHeader: false,
2015-07-10 17:50:48 +08:00
rowSelection: null,
size: 'normal'
2015-07-09 11:41:36 +08:00
};
},
2015-07-10 17:47:53 +08:00
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;
},
2015-07-12 17:10:06 +08:00
loadData: function() {
2015-07-12 17:24:03 +08:00
this.props.resolve = this.props.resolve || function(data) {
return data || [];
};
2015-07-12 17:10:06 +08:00
if (this.props.dataSource) {
this.setState({
loading: true
});
jQuery.ajax({
url: this.props.dataSource,
success: (result) => {
2015-07-12 17:24:03 +08:00
result = this.props.resolve.call(this, result);
2015-07-12 17:10:06 +08:00
if (this.isMounted()) {
this.setState({
data: result
});
}
},
complete: () => {
this.setState({
loading: false
});
}
});
}
},
componentDidMount() {
this.loadData();
},
2015-07-09 11:41:36 +08:00
render() {
2015-07-10 17:47:53 +08:00
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);
}
}
2015-07-12 17:10:06 +08:00
return <Table data={this.state.data} className={this.state.loading ? 'ant-table-loading' : ''} {...this.props} />;
2015-07-09 11:41:36 +08:00
}
});
export default AntTable;