【问题标题】:React JS display clicked table rowReact JS 显示点击的表格行
【发布时间】:2015-04-10 17:40:25
【问题描述】:

我目前正在尝试通过调用某个 API 构建一个使用 JSON 数组的简单应用程序来学习 React。然后我想在表格中显示数组的结果,并能够单击其中一行并从该行获取数据以更新页面的另一部分。

我已成功调用 API 并在表格中显示了正确的数据,但我正在努力弄清楚如何在单击页面的另一部分后显示数据。我创建了一个单击处理程序事件,可以将信息记录到控制台,但不知道如何在页面上为单击的相关行显示此数据。

所以我目前在我的页面中有这个:

<div class="container"></div>

<script type="text/jsx">

    var ShowData = React.createClass({

          getInitialState: function() {
            return { data: [] };
          },

          componentDidMount: function() {
            $.get(this.props.source, function(data) {
              if (this.isMounted()) {
                this.setState({
                    data: data
                });
              }
            }.bind(this));
          },

          handleClick: function(i) {
            console.log('You clicked: ' + this.state.data[i].event + this.state.data[i].name);
          },

          render: function() {
            var self = this;
            return (
                <table className="m-table">
                    <thead>
                            <tr>
                                <th>Event</th>
                                <th>Name</th>
                            </tr>
                        </thead>
                    <tbody>
                    {this.state.data.map(function(type, i){
                   return (
                        <tr>
                            <td title="Type" onClick={self.handleClick.bind(this, i)} key={i}>{type.event}</td>
                            <td title="Type">{type.name}</td>
                        </tr>
                    )
                })}

                        </tbody>
                    </table>
            );
          }

        });

  React.render(
          <ShowData source="url of api" />,
          document.getElementById('container')
        );

</script>

此脚本下方是另一个容器,我想在单击表格行时显示结果。

总而言之,我想在表格中显示来自 API 调用的数据,单击其中一个表格行后,我想获取表格行数据并将其格式化到页面上的另一个容器中。

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    我会将表格组件和显示区域组件都移动到负责管理状态的父组件中。父组件将传递一个回调以处理向下单击表格的行,这与下面对您的代码示例的编辑行类似。 (提醒:写这篇文章时,由于旅行,过去 40 多个小时没有睡觉......)

    <div class="container"></div>
    
    <script type="text/jsx">
    
        var TableComponent = React.createClass({   
              handleClick: function(i) {
                 this.props.clickHandler(i);
              },
    
              render: function() {
                var self = this;
                return (
                    <table className="m-table">
                        <thead>
                                <tr>
                                    <th>Event</th>
                                    <th>Name</th>
                                </tr>
                            </thead>
                        <tbody>
                        {this.props.data.map(function(type, i){
                       return (
                            <tr>
                                <td title="Type" onClick={self.handleClick.bind(this, i)} key={i}>{type.event}</td>
                                <td title="Type">{type.name}</td>
                            </tr>
                        )
                    })}
    
                            </tbody>
                        </table>
                );
              }
    
            });
    
      var DetailsArea = React.createClass({
          render: function() {
              var rowDetails = this.props.rowDetails;
    
              return <div>{rowDetails.name}</div>;
          }
      });
    
      var ParentComponent = React.createClass({
          getInitialState: function() {
              return {data: []};
          },
    
          componentDidMount: function() {
              $.get(this.props.source, function(data) {
                if (this.isMounted()) {
                  this.setState({
                      data: data
                  });
                }
              }.bind(this));
           },
    
           rowClickHandler: function(rowIndex) {
               this.setState({selectedRow: rowIndex});
           },
    
           render: function() {
               var tableData = this.state.data;
               return (
                   <TableComponent data={tableData} clickHandler={rowClickHandler} />
                   <DetailsArea rowDetails={tableData[this.state.selectedRow]} />
           }
      });
    
      React.render(
              <ParentComponent source="url of api" />,
              document.getElementById('container')
            );
    
    </script>
    

    【讨论】:

    • 我觉得有必要指出,如果你这样做,你将为每次调用 render 创建一个新函数,这反过来又会使你的 DOM 无效,因为函数引用发生了变化。这将导致 React 删除/添加所有 onClick 事件侦听器。让事件冒泡而不是在 DOM 中更高的位置捕捉​​它,这部分 DOM 不一定会改变。
    • 我刚刚了解到 React 以不同的方式订阅事件处理程序。所以它并没有我最初想的那么糟糕。您可以在此处阅读更多相关信息 (facebook.github.io/react/docs/…),但您确实应该在 DOM 中捕捉到该事件。
    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多