如何在 react-bootstrap-table 中有条件地渲染按钮? (每行的按钮)

How to conditionally render the button in react-bootstrap-table ? (The button for each row)

我试图通过比较 row.id 和我数据库中的 item.id 来有条件地呈现 react-bootstrap-table 中的按钮。

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

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

这是我的代码:

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

  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 {
       ....
       ....
   }
  })
 }

问题是您是 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>
        }
    })
}

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

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;
}

此解决方案应该有效。让我知道是否有一些修改。