在react js中进行ajax调用后,该列将消失
The column disappears after the ajax call in react js
我调用了一个节点js-server-postgres-db,这给了我一定的结果。我需要在两张表中显示这个结果。每个表有2列。在ajax调用之后,第二个表显示两列,但第一个表只显示第一列。这是我的反应码
Jsx代码
var apiTable = React.createClass({
getInitialState: function() {
return {
data: []
};
},
componentDidMount: function() {
$.ajax({
url: 'report/api?id=' + this.props.id,
dataType: 'json',
method: 'GET' ,
success: function(res) {
this.setState({data: res.data});
}.bind(this),
error: function(xhr, status, err) {
React.unmountComponentAtNode(document.getElementById('alert'));
React.renderComponent(<span className="alert alert-danger">Error</span>, document.getElementById('alert'));
console.error('Search error', err.toString());
},
complete: function(data) {
React.unmountComponentAtNode(document.getElementById('alert'));
}
});
},
render: function() {
var apis = this.state.data.api || [];
var ctsTests = this.state.data.cts || [];
var apiRows = apis.map(function(apiName) {
return (<tr><td className="col-md-2">{apiName.api}</td><td className="col-md-2">{apiName.pathWeight}</td></tr>);
});
var ctsRows = ctsTests.map(function(test) {
return (<tr><td>{test.cts}</td><td>{test.pathWeight}</td></tr>);
});
return (
<div>
<div className="table-container">
<table className="table table-bordered table-hover">
<thead>
<tr>
<th className="col-md-2">CTS test cases</th>
<th className="col-md-2">Path Weight</th>
</tr>
{
ctsTests.length === 0 ?
<tr><td className="info-row">No CTS test cases found</td><td>No Path Weight</td></tr>
: ctsRows
}
</thead>
</table>
</div>
<br/><br/>
<div className="table-container">
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Public APIs affected</th><th>Path Weight</th>
</tr>
{
apis.length === 0 ?
<tr><td className="info-row">No affected APIs found</td><td>No Path Weight</td></tr>
: apiRows
}
</thead>
</table>
</div>
</div>
);
}
});
JS代码
var apiTable = React.createClass({displayName: 'apiTable',
getInitialState: function() {
return {
data: []
};
},
componentDidMount: function() {
$.ajax({
url: 'report/api?id=' + this.props.id,
dataType: 'json',
method: 'GET' ,
success: function(res) {
this.setState({data: res.data});
}.bind(this),
error: function(xhr, status, err) {
React.unmountComponentAtNode(document.getElementById('alert'));
React.renderComponent(React.DOM.span({className: "alert alert-danger"}, "Error"), document.getElementById('alert'));
console.error('Search error', err.toString());
},
complete: function(data) {
React.unmountComponentAtNode(document.getElementById('alert'));
}
});
},
render: function() {
var apis = this.state.data.api || [];
var ctsTests = this.state.data.cts || [];
var apiRows = apis.map(function(apiName) {
return (React.DOM.tr(null, React.DOM.td({className: "col-md-2"}, apiName.api), React.DOM.td({className: "col-md-2"}, apiName.pathWeight)));
});
var ctsRows = ctsTests.map(function(test) {
return (React.DOM.tr(null, React.DOM.td(null, test.cts), React.DOM.td(null, test.pathWeight)));
});
return (
React.DOM.div(null,
React.DOM.div({className: "table-container"},
React.DOM.table({className: "table table-bordered table-hover"},
React.DOM.thead(null,
React.DOM.tr(null,
React.DOM.th({className: "col-md-2"}, "CTS test cases"),
React.DOM.th({className: "col-md-2"}, "Path Weight")
),
ctsTests.length === 0 ?
React.DOM.tr(null, React.DOM.td({className: "info-row"}, "No CTS test cases found"), React.DOM.td(null, "No Path Weight"))
: ctsRows
)
)
),
React.DOM.br(null), React.DOM.br(null),
React.DOM.div({className: "table-container"},
React.DOM.table({className: "table table-bordered table-hover"},
React.DOM.thead(null,
React.DOM.tr(null,
React.DOM.th(null, "Public APIs affected"), React.DOM.th(null, "Path Weight")
),
apis.length === 0 ?
React.DOM.tr(null, React.DOM.td({className: "info-row"}, "No affected APIs found"), React.DOM.td(null, "No Path Weight"))
: apiRows
)
)
)
)
);
}
});
如有任何帮助,我们将不胜感激
您可以console.log
apis
和ctsTests
查看数据是否正确
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 单击以调用 JavaScript 函数时,按钮不断消失
- 为什么 Javascript 中的类变量在尝试多次调用它们或将它们分配给局部变量时会消失
- 在react js中进行ajax调用后,该列将消失
- 加载动画在第二次 ajax 调用之前消失
- setInterval JavaScript 调用在 ajax 加载后不会消失
- Jquery ajax 调用从日期选择器上选择消失文本框
- 调用一个不存在和已经消失的属性
- 通过按钮调用函数后悬停效果消失
- 调用. Modal ('hide')后,Modal不会消失
- 在select输入时多次调用select2()会使其消失
- 当调用框架(和执行上下文)消失时处理回调