Improve table ajax dataSource

1. Add ajax data params support
2. improve dataSource.clone for new config
This commit is contained in:
afc163 2015-08-11 16:57:17 +08:00
parent 6bba4f755c
commit f5afbf16aa
2 changed files with 26 additions and 8 deletions

View File

@ -60,7 +60,7 @@ var dataSource = new Table.DataSource({
}
});
var Test=React.createClass({
var Test = React.createClass({
getInitialState() {
return {
dataSource: dataSource
@ -68,14 +68,28 @@ var Test=React.createClass({
},
refresh() {
this.setState({
dataSource: this.state.dataSource.clone()
dataSource: dataSource.clone()
});
},
changeAndRefresh() {
// 可以修改原来的 dataSource 再发请求
this.setState({
dataSource: dataSource.clone({
data: {
city: 'hz'
}
})
});
},
render() {
return <div>
<Table columns={columns} dataSource={this.state.dataSource} />
<button className="ant-btn ant-btn-primary" onClick={this.refresh}>
外部重新加载数据
重新加载数据
</button>
&nbsp;
<button className="ant-btn" onClick={this.changeAndRefresh}>
加载 city=hz 的数据
</button>
</div>;
}

View File

@ -23,6 +23,7 @@ class DataSource {
this.getParams = config.getParams || noop;
this.getPagination = config.getPagination || noop;
this.headers = config.headers || {};
this.data = config.data || {};
}
constructor(config) {
@ -31,10 +32,12 @@ class DataSource {
}
}
clone() {
var d = new DataSource();
d.init(this.config);
return d;
clone(config) {
if (config) {
return new DataSource(objectAssign(config, this.config));
} else {
return this;
}
}
}
@ -358,9 +361,10 @@ var AntTable = React.createClass({
}
// remote 使 this.dataSource
let dataSource = this.getRemoteDataSource();
let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
return jQuery.ajax({
url: dataSource.url,
data: dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {},
data: objectAssign(buildInParams, dataSource.data),
headers: dataSource.headers,
dataType: 'json',
success: (result) => {