【问题标题】:ReactJS: How to render columns horizontallyReactJS:如何水平渲染列
【发布时间】:2018-05-16 23:00:08
【问题描述】:

我在水平排序获取的 API 数据的列时遇到了问题。我正在尝试将从 API 获取的数据打印到表中。无论如何,行水平打印得很好,但是我用于行的函数 this.state.data.map() 对列的功能不同。我认为这是 ES6 标准,但我不确定。 Here is my printed issue.

这是我的代码示例:

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="table">
                    <thead> {
                        this.state.columns.map(( column ) => (
                            <tr>

                                <th>{column[0]}</th>
                                <th>{column[1]}</th>
                                <th>{column[2]}</th>
                                <th>{column[3]}</th>
                            </tr>
                        ) )}
                            </thead>
                            <tbody> {
                                this.state.rows.map(( row ) => (
                                    <tr>
                                        <td>{row[0]}</td>
                                        <td>{row[1]}</td>
                                        <td>{row[2]}</td>
                                        <td>{row[3]}</td>
                                    </tr>
                                ) )
                            }
                            </tbody>
                        </table>
                    </div>
        )
    }
}


ReactDOM.render( <div id="container"><App /></div>, document.querySelector( 'body' ) );

如果我为“th”元素赋予值,我能够打印硬编码,但我想动态打印它,以防 API 中的数据已更改。

欢迎您直接向我的 Repo 投稿:Fetching API data into a table

Here is how looks like my example, when columns values has been hardcoded within 'th' elements.

任何建议将不胜感激!

【问题讨论】:

  • 您能否也发布硬编码列名时的外观?
  • 嗨@NanduKalidindi 是的,我当然更新了问题描述。见这个例子i.stack.imgur.com/FuW9Q.jpg

标签: javascript reactjs babeljs jsx


【解决方案1】:

不知道你为什么要做列 [0]、[1]、[2] 和 [3]。地图中的每一列都是一个字符串。如果您执行 columns[0] ,它将返回您的情况中发生的第一个字符。 Full 名称、Job 标题、Age Bonus 在您的第一张图像中呈现。列中的前四个字符。

我添加的key属性只是React要求提供唯一键,与问题无关。

this.state.columns.map((column, index) => (
    <tr>
        <th key={"columns-" + index.toString()}>
            {column}
        </th>
    </tr>
))

【讨论】:

  • 这个,实际上返回了第一行之上的所有列值。但是上面的例子是完美的。 @NanduKalidindi 我还有一个问题,看起来你会很熟悉。如何自定义表格的端视图?它不能与单独的 css 文件一起使用,在网络上进行了简短的研究之后,看起来我必须使用 npm 包来处理 css。但我还有疑问?
  • @antdimit 如果你想使用css 创建一个文件,将它导入到你的组件中。然后,无论您想在何处应用该类,只需向元素 className="some-class" 添加一个属性。查看他们import ./App.css 的 react hello world 示例。检查这个例子stackblitz.com/edit/react-vy5dnh?file=index.js
【解决方案2】:

var data = {
columns: [
"Full name",
"Job title",
"Age",
"Bonus",
],
rows: [
[
"John Smith",
"team lead front-end",
30,
444.08,
],
[
"Tom Jones",
"front-end developer",
25,
333.3,
],
[
"Deborah Barnes",
"front-end developer",
21,
233.66,
],
[
"Isaac Roberson",
"technical support",
44,
353,
],
[
"Josh Brown",
"team lead back-end",
35,
353,
],
[
"Chester Mckinney",
"back-end developer",
33,
223.27,
],
[
"Ora Burton",
"back-end developer",
32,
192.92,
],
[
"Jim Brown",
"technical support",
19,
98.99,
],
],
}

class App extends React.Component
{
    constructor()
    {
        super();
        this.state = {
            rows: [],
            columns: []
        }
    }

    componentDidMount()
    {

     
             this.setState( { rows: data.rows, columns: data.columns } );

    }


    render()
    {

        return (
            <div id="container" className="container">
                <h1>Final Table with React JS</h1>
                   <table className="table">
                    <thead>
                            <tr>
                             {this.state.columns.map(( column, index ) =>                                  {
       
                                 return (<th>{column}</th>)
                              }
                               )
                              }
                            </tr>
                            </thead>
                            <tbody> {
                                this.state.rows.map(( row ) => (
                                    <tr>
                                        <td>{row[0]}</td>
                                        <td>{row[1]}</td>
                                        <td>{row[2]}</td>
                                        <td>{row[3]}</td>
                                    </tr>
                                ) )
                            }
                            </tbody>
                        </table>
                    </div>
        )
    }
}


ReactDOM.render( <div id="container"><App /></div>, document.querySelector( 'body' ) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

希望对你有帮助

【讨论】:

  • 您使用的是哪个框架??您可以为它编写 index.html 中可用的任何样式表的 css
  • 简直完美,符合我的要求。非常感谢!
  • 我正在使用 unpkg.com/react@16.0.0/umd/react.production.min.jsunpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js,但是当我将自定义 css 文件添加为 它不起作用根本不读。
  • 那么,您使用的是哪些捆绑器,如果您使用的是 webpack,请在配置中添加 css 加载器和样式加载器,然后导入 css 文件。导入“./styles.css”
  • 还有一件事检查你是否给出了正确的 href 。我可以它本质上是绝对的。检查浏览器中的文件并检查它有什么href
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 2012-09-26
  • 2018-06-21
  • 2012-01-17
  • 2020-04-04
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多