From f5afbf16aac8df2b60cdbffdd0d2dd311d78b313 Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 11 Aug 2015 16:57:17 +0800 Subject: [PATCH] Improve table ajax dataSource 1. Add ajax data params support 2. improve dataSource.clone for new config --- components/table/demo/ajax.md | 20 +++++++++++++++++--- components/table/index.jsx | 14 +++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/components/table/demo/ajax.md b/components/table/demo/ajax.md index 51ab08ca71..a32adb0d2b 100644 --- a/components/table/demo/ajax.md +++ b/components/table/demo/ajax.md @@ -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
+   + ; } diff --git a/components/table/index.jsx b/components/table/index.jsx index e65b8362bb..11e5a6feba 100644 --- a/components/table/index.jsx +++ b/components/table/index.jsx @@ -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) => {