【问题标题】:Dynamically displaying data from a clickable table row into a modal将可点击表格行中的数据动态显示到模式中
【发布时间】:2017-02-16 05:42:12
【问题描述】:

我正在尝试创建一个由数据行组成的组件,单击该组件时,会打开一个包含与该表行相关的信息的模式。例如,当用户点击“团队 1”时,会出现一个模式,显示一个新表格,其中显示分配给该团队的每个用户。

我已经设法使用手动提供的参数来实现这一点,但是我不知道如何根据单击的表格行使模态动态显示数据。 Here is a link to a jsfiddle that i've made to show my problem.

    getInitialState: function () {
    return {
      teams:[
        {
          id: '1',
          teamName: 'team 1',
          users: ['dave', 'steve', 'jim', 'barry', 'tom', 'harry']
        },
      ]
    };


    render: function () {
    var self = this;
    var projectsTable = this.state.teams.map(function (obj, index) {
      return (
        <tr className="table-teamProject" key={index} data-toggle="modal" data-target="#projectUsersModal" data-id='3'>
          <div className="mCellsContainer">
            <div className="mCellsNames">{obj.teamName}</div>
            <div className="mCellsCount">{obj.users.length} Users</div>
          </div>
        </tr>
      );
    });

    var projectUsersModal = this.state.teams.map(function (obj, index) {
      return (
        <div className="modal projectUsersModal fade" id="projectUsersModal" tabIndex={-1} role="dialog" aria-labelledby="myModalLabel">
          <div className="modal-dialog" role="document">
            <div className="modal-content">
              </div>
            </div>
          </div>
      );
    });

    return (
      <div>
        <div className="projectsColContainer">
          <div className="panel panel-default">
            <div className="panel-heading">Projects</div>
            <table className="scroll-table">
              {projectsTable}
              {projectUsersModal}
            </table>
          </div>
        </div>
      </div>
    );
  }

【问题讨论】:

  • 小提琴缺少桌子。你能更新一下吗
  • 其中还有很多错误,所以它甚至无法运行。
  • 它显示在我的小提琴link,我不明白为什么会这样?

标签: javascript html twitter-bootstrap reactjs jsx


【解决方案1】:

render() 方法正在为您的团队数组中的每个团队创建一个隐藏模式,无论用户是否请求显示模式(单击团队的链接) .更好的方法是按需创建特定的模式,即当用户点击团队的链接时。

这可以通过创建一个点击处理程序来完成,在该函数中,您可以通过设置模态所涉及的团队的 id 来修改状态,如下所示:

onClickTeam: function(teamId) {
  this.setState({ 
    openModalTeamId: this.state.openModalTeamId == teamId ? null : teamId 
  });
}

然后在您的 render() 方法中,您将要检查此 openModalTeamId 状态属性是否具有某些价值,如果是,并且由于您将团队的 id 存储在其中,您会想要寻找这个特定的团队使用Array.prototype.find 在您的状态团队数组中,然后使用返回的结果来构造您的模态内容。

render: function() {
  ...

  var modalBody;
  if (this.state.openModalTeamId) {
    var team = this.state.teams.find(function(el) {
      return el.id == self.state.openModalTeamId 
    });

    modalBody = 
      ...
      <div className="modal-body">
        Lets assume this is your modal containing the 
        following info about the selected team:
        <br /><br />
        {JSON.stringify(team)}
        <br /><br />
        <div onClick={(this.onClickTeam.bind(this, team.id))}>
          Click me to close
        </div>
      </div>
      ...
  }

  ...
}

一旦你有了它,你就可以将这个新的modalBody 变量附加到你的渲染器的 JSX 中,就像你在代码中使用 projectUsersModal 变量一样。如果没有点击任何团队,则此变量将为undefined,并且不会显示任何模式。

return (
  <div>
    <div className="projectsColContainer">
      <table className="scroll-table">
        {projectsTable}
        {modalBody}
      </table>
    </div>
  </div>
);

jsFiddle

【讨论】:

    【解决方案2】:

    您可以使用https://github.com/fckt/react-layer-stack

    它允许您使用闭包中的变量(如果您将其提供给 Layer 的“使用”属性,它将自动传播),还可以将事件数据从切换到模式窗口设置。您也可以使用 zIndex 层层叠叠“堆叠”。

    import { Layer, LayerContext } from 'react-layer-stack'
    // ... for each `object` in array of `objects`
    const modalId = 'DeleteObjectConfirmation' + objects[rowIndex].id
    return (
        <Cell {...props}>
            // the layer definition. The content will show up in the LayerStackMountPoint when `show(modalId)` be fired in LayerContext
            <Layer use={[objects[rowIndex], rowIndex]} id={modalId}> {({
                hideMe, // alias for `hide(modalId)`
                index } // useful to know to set zIndex, for example
                , e) => // access to the arguments (click event data in this example)
              <Modal onClick={ hideMe } zIndex={(index + 1) * 1000}>
                <ConfirmationDialog
                  title={ 'Delete' }
                  message={ "You're about to delete to " + '"' + objects[rowIndex].name + '"' }
                  confirmButton={ <Button type="primary">DELETE</Button> }
                  onConfirm={ this.handleDeleteObject.bind(this, objects[rowIndex].name, hideMe) } // hide after confirmation
                  close={ hideMe } />
              </Modal> }
            </Layer>
    
            // this is the toggle for Layer with `id === modalId` can be defined everywhere in the components tree
            <LayerContext id={ modalId }> {({showMe}) => // showMe is alias for `show(modalId)`
              <div style={styles.iconOverlay} onClick={ (e) => showMe(e) }> // additional arguments can be passed (like event)
                <Icon type="trash" />
              </div> }
            </LayerContext>
        </Cell>)
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2016-09-22
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多