【问题标题】:How to conditionally render the button in react-bootstrap-table ? (The button for each row)如何有条件地呈现 react-bootstrap-table 中的按钮? (每行的按钮)
【发布时间】:2017-08-31 21:04:29
【问题描述】:

我正在尝试通过将 row.id 与数据库中的 item.id 进行比较,有条件地呈现 react-bootstrap-table 中的按钮。

我设法通过使用 dataFormat 道具添加了一个按钮,但我无法有条件地显示一个按钮

  • 我正在使用该表来显示从数据库中获取的组。
  • 获取所有组后,我会将它们的 ID (row.id) 与数据库中的组进行比较。
  • 如果匹配,我会显示Button1
  • 如果它们不匹配,我会显示Button2

我尝试了很多尝试,但我的解决方案没有给我想要的结果

这是我的代码:

  • 如果我在数据库中已经有 8 个组,则这 8 个组的按钮应该是 red,其文本与其他按钮不同。
  • 如果组不在数据库中,它的按钮应该是blue

    class App extends Component {
    
    constructor() {
      super()
      this.state = {
         json: [], // json(coming from the Meetup-api)
         jsonFromDatabase: [],
      }
      this.cellButton = this.cellButton.bind(this);
     }
    
    cellButton(cell, row, enumObject, rowIndex) {
      let theButton
      for(var group in this.state.jsonFromDatabase){
      if (this.state.jsonFromDatabase[group].id !== row.id){
         // Display this button if the group is not in the database
         theButton = <button style={{ backgroundColor: "blue"}}
                         type="button"
                         onClick={() => this.onClickGroupSelected(cell, row, rowIndex)}>
                       Process the group
                     </button>
       } else {
         // Display this button if the group is already in the database
         theButton = <button style={{ backgroundColor: "red" }}
                         type="button"
                         onClick={() => this.onClickGroupToUpdate(cell, row, rowIndex)}>
                       Update the group
                     </button>
         }
       }
      return theButton
    }
    
    render() {
        return (
          <BootstrapTable data={this.state.json} options={ this.options } >
               <TableHeaderColumn isKey dataField='id' width='100'>ID</TableHeaderColumn>
              <TableHeaderColumn dataField='name' width='300'>Group Name</TableHeaderColumn>
              <TableHeaderColumn dataField='button' width='100' dataFormat={this.cellButton}>Generate Group Page</TableHeaderColumn>
            </BootstrapTable>
        )
      }
    }
    

我尝试的另一个不成功的变体:

  cellButton(cell, row, enumObject, rowIndex) {
  let theButton

  Object.keys(this.state.jsonFromDatabase).map((key) => {
  if (this.state.jsonFromDatabase[key].id  !== row.id){
    return (
      <button style={{ backgroundColor: "blue"}}
          type="button"
          onClick={() => this.onClickGroupSelected(cell, row, rowIndex)}>
         Process the group
      </button>
    )
   } else {
       ....
       ....
   }
  })
 }

【问题讨论】:

    标签: javascript reactjs firebase react-bootstrap-table


    【解决方案1】:

    您的逻辑似乎是正确的,但实现有点错误。

    cellButton(cell, row, enumObject, rowIndex) {
      let theButton;
      let groupExistsInDatabase = false;
      for(var group in this.state.jsonFromDatabase){
        if (this.state.jsonFromDatabase[group].id === row.id){
          // make groupExistsInDatabase true if the group is found in the database
          groupExistsInDatabase = true;
          break;
        }
      }
    
      if(groupExistsInDatabase === true) {
        theButton = <button style={{ backgroundColor: "red" }} type="button" onClick={() => this.onClickGroupToUpdate(cell, row, rowIndex)}>
                      Update the group
                    </button>
      } else {
        theButton = <button style={{ backgroundColor: "blue" }} type="button" onClick={() => this.onClickGroupSelected(cell, row, rowIndex)}>
                      Process the group
                    </button>
      }
      return theButton;
    }
    

    此解决方案应该有效。如果有一些修改,请告诉我。

    【讨论】:

    • 很好,这个解决方案工作得很好,但你能否将 !== 更改为 === 。因为目标是检查存储在数据库中的group.id 是否等于row.id。除此之外,它工作正常
    • 快速提问,假设我想在用户点击时临时更改按钮文本。例如:如果用户点击Process the group 按钮,文本将更改为Processing 1 秒,以留出一些时间来获取数据。你会怎么做?我尝试在按下按钮时使用setState 这样做,但它正在更改所有按钮?
    • @AziCode 我认为您可以使用 setState 但存储 cell row and column as well 并为该行和列更改文本。当您的数据被提取时,然后从 setState 中删除该单元格行和列。这两个操作都可以在componentWillUpdate()
    • 我实际上找到了一个解决方案:当用户点击button 时,我基本上将rowIndex 存储在state 中。然后在 cellButton 函数中检查存储的rowIndex 是否为===rowIndex 并在Button 上显示适当的文本,这样我可以向用户显示正在执行的操作。谢谢
    • @AziCode 为此欢呼。但是尝试同时单击多个按钮,所有这些按钮都必须有文本,并且单击一个按钮不应该改变另一个按钮的 loading 状态
    【解决方案2】:

    问题是你是returning 来自单元格格式化程序function 的一键式。

    使用这个:

    cellButton(cell, row, enumObject, rowIndex) {
        let uiItems = [];
        for(var group in this.state.jsonFromDatabase){
            if (this.state.jsonFromDatabase[group].id !== row.id){
                uiItems.push(<button style={{ backgroundColor: "blue"}}
                                type="button"
                                onClick={() => this.onClickGroupSelected(cell, row, rowIndex)}>
                                    Process the group
                            </button>)
            }else{
                    uiItems.push(<button style={{ backgroundColor: "red" }}
                                    type="button"
                                    onClick={() => this.onClickGroupToUpdate(cell, row, rowIndex)}>
                                       Update the group
                                </button>)
            }
        }
        return uiItems;
    }
    

    或使用map,像这样:

    cellButton(cell, row, enumObject, rowIndex) {
        return this.state.jsonFromDatabase.map((group, i) => {
            if (group.id !== row.id){
                return  <button style={{ backgroundColor: "blue"}}
                            type="button"
                            onClick={() => this.onClickGroupSelected(cell, row, rowIndex)}>
                                Process the group
                        </button>
            }else{
                return  <button style={{ backgroundColor: "red" }}
                            type="button"
                             onClick={() => this.onClickGroupToUpdate(cell, row, rowIndex)}>
                                Update the group
                        </button>
            }
        })
    }
    

    【讨论】:

    • 对不起,我忘了在我的问题中添加它,但我实际上做了bind构造函数中的格式函数(我编辑了我的问题)
    • 按钮没有反映在 ui 中?还有一件事将 console.log 放在这个函数中,并检查它是否进入这个函数。
    • 它在函数内部,如您在屏幕截图中所见。如果组已经在数据库中,我会返回一个红色的 Btn,如果不在数据库中,我会返回一个蓝色的 Btn。
    • 那是什么问题?
    • 只需从单元格格式化程序函数返回 uiItems,它将是一个包含所有按钮的数组,就像我做的那样。是的,我非常熟悉react-bootstrap-table :)
    猜你喜欢
    • 1970-01-01
    • 2020-01-07
    • 2016-10-06
    • 2017-12-15
    • 1970-01-01
    • 2016-10-08
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多