我正在将从 API 获取的数据打印到表格中,并且在将行数值固定为小数时遇到了一些困难。如果 API 中的行数据由数值组成,即 ie10.0, 333, 8 or 100
等,则最后以十进制值呈现它 -> 10.00, 333.00, 100.00
。
我熟悉的.toFixed(2)
函数在 React 中的功能与我以前在 javaScript 中编写的方式不同。我认为我在误导 ES6 标准,但我不确定。如果我避免使用.toFixed(2)
.
这是我的代码示例rows.toFixed(2)
,但它不能正常工作:
class App extends React.Component
{
constructor()
{
super();
this.state = {
rows: [],
columns: []
}
}
componentDidMount()
{
fetch( "http://ickata.net/sag/api/staff/bonuses/" )
.then( function ( response )
{
return response.json();
} )
.then( data =>
{
this.setState( { rows: data.rows, columns: data.columns } );
} );
}
render()
{
return (
<div id="container" className="container">
<h1>Final Table with React JS</h1>
<table className="datagrid">
<thead>
<tr> {
this.state.columns.map(( column, index ) =>
{
return ( <th>{column}</th> )
}
)
}
</tr>
</thead>
<tbody> {
this.state.rows.toFixed(2).map( row => (
<tr>{row.toFixed(2).map( cell => (
<td>{cell}</td>
) )}
</tr>
) )
}
</tbody>
</table>
</div>
)
}
}
ReactDOM.render( <div id="container"><App /></div>, document.querySelector( 'body' ) );
欢迎您直接向我的 Repo 投稿:Fetching API data into a table
以下是我的示例,当.toFixed
使用 时:
不幸的是,我无法在ReactJs.org找到相关文档
它也不起作用rows[].length.toFixed(2)
。
任何建议将不胜感激!