如何在ReactJs中渲染函数onclick提交按钮?

时间:2018-02-24 13:42:42

标签: javascript html json reactjs

当我点击提交按钮时,应该从JSON数据中显示hour_slotshour,但是当我点击提交按钮时,不会显示任何内容。我正在尝试使用showslots()函数在提交按钮下方显示HTML内容。

datapicker.js:

import React, { Component } from 'react';
import data from './data';
import './datepicker.css';

class DatePicker extends Component {

  constructor(props){
    super(props);
     this.state = {
        counter:0
     };
   }

  increment(){
    if(this.state.counter < 6){
      this.setState(prevState => ({counter: prevState.counter + 1}))
    }
  }

  decrement(){
    if(this.state.counter > 0){
      this.setState(prevState => ({counter: prevState.counter-1}))
   }
  }

  showslots(){
        if(data.available_slots[this.state.counter].date_slots.length === 0){
            return(
                <p>No slots</p>
                )
        }else {
            return(

                    data.available_slots[this.state.counter].data_slots.map(obj =>{

                        <div id="slotinfo">
                            <p>Hour : {obj.hour}</p>
                            <p>Hour slots : {obj.hour_slots[0]}</p>
                            <p>Hour slots : {obj.hour_slots[0]}</p> 
                        </div>
                    })
                )
        }

  }

  render() {
    return (
      <div>

        <div id="center">
        <div className="title">
            <p>Pick a Date</p>
        </div>
        <div className="increment">
          <button type="button" className="btn btn-success" id="plus" onClick={this.increment.bind(this)}>+</button>
        </div>
        <div className="display">
          <input type="text" id="date" value={data.available_slots[this.state.counter].date}/>
        </div>
        <div className="decrement">
          <button type="button" className="btn btn-danger" id="minus" onClick={this.decrement.bind(this)}>-</button> 
        </div>
        <div className="status">
          { data.available_slots[this.state.counter].date_slots.length === 0 ? 
          <p>No slots available for today</p> : <p>Slots available for today</p> }
        </div>
        <div className="submit">
          <button type="button" className="btn btn-primary" id="submit" onClick={this.showslots.bind(this)}>Book Slot</button> 
        </div>
      </div>

      </div>
    );
  }
}

export default DatePicker;

data.js:

const data = {
        "template_type": "slot_picker",
        "selection_color": "#000000",
        "secondary_color": "#808080",
        "title": "Available Slots for Dr. Sumit",
        "available_slots": [
          {
            "date": "Wed, Dec 06",
            "date_slots": [

            ]
          },
          {
            "date": "Thu, Dec 07",
            "date_slots": [

            ]
          },
          {
            "date": "Fri, Dec 08",
            "date_slots": [

            ]
          },
          {
            "date": "Sat, Dec 09",
            "date_slots": [

            ]
          },
          {
            "date": "Today",
            "date_slots": [
              {
                "hour": "8",
                "hour_slots": [
                  {
                    "08:10 AM": "slotId001"
                  },
                  {
                    "08:50 AM": "slotId005"
                  }
                ]
              },
              {
                "hour": "3",
                "hour_slots": [
                  {
                    "03:00 PM": "slotId005"
                  },
                  {
                    "03:30 PM": "slotId007"
                  }
                ]
              }
            ]
          },
          {
            "date": "Tomorrow",
            "date_slots": [

            ]
          },
          {
            "date": "Wed, Dec 13",
            "date_slots": [
              {
                "hour": "4",
                "hour_slots": [
                  {
                    "04:30 PM": "slotId105"
                  },
                  {
                    "04:50 PM": "slotId106"
                  }
                ]
              },
              {
                "hour": "5",
                "hour_slots": [
                  {
                    "05:30 PM": "slotId202"
                  },
                  {
                    "05:45 PM": "slotId208"
                  }
                ]
              }
            ]
          }
        ]
      };

 export default data;

当我点击提交按钮时,它应该显示插槽详细信息,但在提交按钮下面没有显示任何内容,因为在提交按钮下没有动态生成html。看截图: enter image description here

4 个答案:

答案 0 :(得分:2)

我在以下代码中找到了一个主要错误

                data.available_slots[this.state.counter].data_slots.map(obj =>{

                    <div id="slotinfo">
                        <p>Hour : {obj.hour}</p>
                        <p>Hour slots : {obj.hour_slots[0]}</p>
                        <p>Hour slots : {obj.hour_slots[0]}</p> 
                    </div>
                })

在map函数中,传递一个不返回任何内容的函数。对于相同的语法,正确的代码是:

                data.available_slots[this.state.counter].data_slots.map(obj =>{
                    return (
                      <div id="slotinfo">
                        <p>Hour : {obj.hour}</p>
                        <p>Hour slots : {obj.hour_slots[0]}</p>
                        <p>Hour slots : {obj.hour_slots[0]}</p> 
                      </div>
                    );
                })

请注意退货声明。

如果您不想使用return语句,只需使用圆括号替换函数定义周围的花括号。

答案 1 :(得分:1)

您需要有一个额外的状态变量isSubmitted,最初设置为false以了解是否已单击提交按钮。单击“提交”按钮,调用函数将isSubmitted的状态值更新为true

setSubmitted(){
  this.setState({ isSubmitted:true })
}

并将按钮的onClick功能更改为setSubmitted

<button type="button" className="btn btn-primary" id="submit" onClick={this.setSubmitted.bind(this)}>Book Slot</button>

并调用showslots()应该显示的位置(以及条件以便仅在单击提交按钮后显示插槽),在渲染方法中如下所示:

{this.state.isSubmitted && this.showslots()}

答案 2 :(得分:0)

datepicker.js:

import React, { Component } from 'react';
import data from './data';
import './datepicker.css';

class DatePicker extends Component {

  constructor(props){
    super(props);
     this.state = {
        counter:0,
        issubmitted:false
     };
   }

  increment(){
    if(this.state.counter < 6){
      this.setState(prevState => ({counter: prevState.counter + 1}))
    }
  }

  decrement(){
    if(this.state.counter > 0){
      this.setState(prevState => ({counter: prevState.counter-1}))
   }
  }

  formsubmitted(){
    this.setState({
        issubmitted:true
    })
  }

  showslots(){
        if(data.available_slots[this.state.counter].date_slots.length === 0){
            return(
                <p>No slots</p>
                )
        }else {
            return(

                    data.available_slots[this.state.counter].date_slots.map(obj =>{

                        return (
                  <div id="slotinfo">
                    <p>Hour : {obj.hour}</p>
                    <p>Hour slots : {obj.hour_slots[0]}</p>
                    <p>Hour slots : {obj.hour_slots[0]}</p> 
                  </div>
                );
                    })
                )
        }

  }

  render() {
    return (
      <div>

        <div id="center">
        <div className="title">
            <p>Pick a Date</p>
        </div>
        <div className="increment">
          <button type="button" className="btn btn-success" id="plus" onClick={this.increment.bind(this)}>+</button>
        </div>
        <div className="display">
          <input type="text" id="date" value={data.available_slots[this.state.counter].date}/>
        </div>
        <div className="decrement">
          <button type="button" className="btn btn-danger" id="minus" onClick={this.decrement.bind(this)}>-</button> 
        </div>
        <div className="status">
          { data.available_slots[this.state.counter].date_slots.length === 0 ? 
          <p>No slots available for today</p> : <p>Slots available for today</p> }
        </div>
        <div className="submit">
          <button type="button" className="btn btn-primary" id="submit" onClick={this.formsubmitted.bind(this)}>Book Slot</button> 
        </div>
      </div>

      <div>
        {this.state.issubmitted ? this.showslots() : <p>No slots</p>}
      </div>


      </div>
    );
  }
}

export default DatePicker;

答案 3 :(得分:0)

@ water-man,你能测试下面的代码:

import React, {Component} from 'react';
import data from './data.js';

class DatePicker extends Component {

  constructor(props) {
    super(props);
    this.state = {
      counter: 0,
      data: data
    };
    this.showslots = this.showslots.bind(this);
  }

  increment() {
    if (this.state.counter < 6) {
      this.setState(prevState => ({counter: prevState.counter + 1}))
    }
  }

  decrement() {
    if (this.state.counter > 0) {
      this.setState(prevState => ({counter: prevState.counter - 1}))
    }
  }

  showslots() {
    const counter = this.state.counter;
    if (this.state.data.available_slots[counter].date_slots.length === 0) {
      return (
        <p>No slots</p>
      )
    } else {
      const counter = this.state.counter;
      const data_slots = this.state.data.available_slots[counter].date_slots;
      return (
        data_slots.map(obj => {
          return(
          <div id="slotinfo">
            <p>Hour : {obj.hour}</p>
            <p>Hour slots : {obj.hour_slots[0]}</p>
            <p>Hour slots : {obj.hour_slots[0]}</p>
          </div>
          )
        })
      )
    }

  }

  render() {
    console.log(data);
    return (
      <div>

        <div id="center">
          <div className="title">
            <p>Pick a Date</p>
          </div>
          <div className="increment">
            <button type="button" className="btn btn-success" id="plus" onClick={this.increment.bind(this)}>+</button>
          </div>
          <div className="display">
            <input type="text" id="date" value={data.available_slots[this.state.counter].date}/>
          </div>
          <div className="decrement">
            <button type="button" className="btn btn-danger" id="minus" onClick={this.decrement.bind(this)}>-</button>
          </div>
          <div className="status">
            { data.available_slots[this.state.counter].date_slots.length === 0 ?
              <p>No slots available for today</p> : <p>Slots available for today</p> }
          </div>
          <div className="submit">
            <button type="button" className="btn btn-primary" id="submit" onClick={this.showslots}>Book Slot</button>
          </div>
        </div>

      </div>
    );
  }
}

export default DatePicker;