【问题标题】:Call a Function on Tab click based on key?基于键在选项卡单击上调用函数?
【发布时间】:2020-03-20 09:29:37
【问题描述】:

这些是我的选项卡和表格,当您单击选项卡时,我想根据 tabKey 调用不同的函数。

<Paper className={classes.root} style = {{paddingTop:50}}>
<Grid>
<TabSelector
  displayType="button"
  showTab={"Org Details"}
  showCount={false}
  highlightTab={true}
  onClick={() => this.getTabs.bind(this)}
>
  <Tab name="Org Details" tabKey="gridOrgDetails">
    <p>
      <Grid>
        <Paper className={classes.root} style={{ paddingTop: 50 }}>
          <a href="#gridOrgDetails" id="gridOrgDetails" />
          <Paper style={{ backgroundColor: "#759FEB" }}>
            <Typography> ORG Details </Typography>
          </Paper>
          <EnhancedTable
            checkBoxEnabled={false}
            Data={{ rows: this.getOrg(), headCells: orgDeatils }}
            rowsPerPage={5}
            orderBy="Call_Date_vod__c"
            order="desc"
          />
        </Paper>
      </Grid>
    </p>
  </Tab>

  <Tab
    name="License Details"
    tabKey="gridLicenseDetails"
    onClick={() => this.getLicenseDetails()}
  >
    <p>
      <Grid>
        <Paper className={classes.root} style={{ paddingTop: 50 }}>
          <a href="#gridLicenseDetails" id="gridLicenseDetails" />
          <Paper style={{ backgroundColor: "#759FEB" }}>
            <Typography> License Details </Typography>
          </Paper>
          <EnhancedTable
            checkBoxEnabled={false}
            Data={{ rows: this.getOrg(), headCells: licenseDetails }}
            rowsPerPage={5}
            orderBy="Call_Date_vod__c"
            order="desc"
          />
        </Paper>
      </Grid>
    </p>
  </Tab>
</TabSelector>;

这是我正在切换键的功能,但这似乎不起作用。

谁能帮帮我?

getTabs(f){
  console.log(f.tabKey)
  switch(f.tabKey) {
      case "gridOrgDetails":
          return this.getOrgDetails();
          break;
      case "gridLicenseDetails":
          return this.getLicenseDetails();
          break;
  }
}

【问题讨论】:

    标签: javascript reactjs button react-redux onclick


    【解决方案1】:

    这是你试图实现 jsFiddle 的基本表示

    link

    const User = (props) => <div onClick = {props.onClick}>I am user 
    {props.user}</div>
    
    class App extends React.Component {
     constructor(props) {
     super(props)
    
     }
     getTabs(f){
      switch(f) {
      case "gridOrgDetails":
         console.log("gridOrgDetails")
          break;
      case "gridLicenseDetails":
           console.log("gridLicenseDetails")
          break;
      }
      }
      render() {
    return (
      <div>
       <div >
             <User user="a" onClick={this.getTabs.bind(this,"gridOrgDetails")} />
             <User user="b" onClick={this.getTabs.bind(this, "gridLicenseDetails")}/>
       </div>
      </div>
      )
     }
    }
    
    ReactDOM.render(<App />, document.querySelector("#app"))
    

    【讨论】:

    • 这对我不起作用。你有更好的解决方案吗??
    • 我想从 标签中获取 tabkey 而不将其作为参数传递。有什么办法可以访问吗?或者,如果我单击按钮,它将把按钮键作为标签键和与该 tabKey 关联的调用函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多