【问题标题】:How to display the price for each rows on dynamic table using ReactJS?如何使用 ReactJS 在动态表上显示每一行的价格?
【发布时间】:2019-01-25 22:04:28
【问题描述】:

我有一个动态表,对于每一行,我想在选择产品名称后,显示价格,当我输入数量时,也会显示总(价格*数量)列。

我的问题是当我为任何行选择产品名称时,表格上的每一行都会显示相同的价格,当我想要总显示时它总是有 NaN,如下图所示:

我的代码:

class AjouterFacture extends Component {
  constructor(props) {
    super(props);
    this.state = {

      rowData: [],

      Produits: [],
      QuantiteF: "",
      Prix: [],
      id: 0,


    };
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleRowDelete = this.handleRowDelete.bind(this);
    this.handleRowAdd = this.handleRowAdd.bind(this);

    this.handleselectChange = this.handleselectChange.bind(this);
    this.PrixDisplay = this.PrixDisplay.bind(this);
  }
  componentWillReceiveProps(nextProps) {
    console.log("nextProps", nextProps);
  }
  componentDidMount() {

    axios({
      method: "get",
      url: "/app/getNomprod/",
      withCredentials: true,
    }).then(response => {
      if (response && response.data) {
        this.setState({
          Produits: response.data
        });
      }
    }).catch(error => console.log(error));
  }

  handleQuantiteChange(index, value) {
    const rowDataCopy = this.state.rowData.slice(0);
    rowDataCopy[index] = Object.assign({}, rowDataCopy[index], {
      QuantiteF: parseInt(value, 10)
    });
    this.setState({
      rowData: rowDataCopy
    });
  }

  handleselectprdtChange(index, value) {
    const rowDataCopy = this.state.rowData.slice(0);
    rowDataCopy[index] = Object.assign({}, rowDataCopy[index], {
      selectprdt: value
    });
    this.setState({
      rowData: rowDataCopy,
    });

  render() {

    let {
      Clients
    } = this.state.Clients;
    var Cd = {
      pointerEvents: 'none'
    }
    let {
      Produits
    } = this.state;
    let {
      rowData
    } = this.state.rowData;
    let {
      Prix
    } = this.state.Prix;

    return (<div className="animated fadeIn">


 <h6>  <Label ><strong>Veuillez ajouter au moins un produit :  </strong></Label></h6>
        <Table responsive style={items} >
        <thead style={back}>
                  <tr>
                    <th>PRODUIT</th>
                    <th>QUANTITE</th>
                    <th>PRIX UNITAIRE</th>
                    <th>TOTAL</th>
                    <th></th>
                  </tr>
                  </thead>
                  <tbody>
                {this.state.rowData.map((data, index) => (
              <tr key={index} id={index}>
                <td>
                  {" "}  <Input type="select" name="selectprdt" id="selectprdt"
                          placeholder="Veuillez sélectionner un produit"  value={data.selectprdt}
                    onChange={(e) => this.handleselectprdtChange(index, e.target.value)} >
           <option  key={-1} hidden>Choisisr un produit</option>


                     {  this.state.Produits.map((pdt, i) => 
                     <option key={i}>{pdt.Nomp}</option>


                     )} 


                      </Input>
                    </td>
                    <td><Input type="number" 
                          value={data.QuantiteF || 0} onChange={(e) => this.handleQuantiteChange(index, e.target.value)}/></td>
                    <td>


                    {  this.state.Prix.map(pr => 
      <p key={index} >{pr.PrixV} </p>
                       )} 

                        </td>

                <td  > 


                     <p key={index} className='pa2 mr2 f6'>{(data.QuantiteF || 0) * (parseInt(this.PrixDisplay(data.selectprdt)|| 0))}  </p>



                    </td>
                    <td>
                     <Button onClick={(e) => this.handleRowDelete(index)} active style={center}  size="sm" color="danger" className="btn-pill" aria-pressed="true">Effacer</Button>
      </td>{" "}
              </tr>
            ))}



                  <tr>

            <td/>
            <td/>
            <td/>
            <td/>
            <td><Button onClick={this.handleRowAdd} active style={center}  size="sm" color="info" className="btn-pill" aria-pressed="true">Ajouter une ligne</Button></td>
          </tr>
        </tbody>



        </Table>


        </div>
        );
  }
  PrixDisplay(selectprdt) {
    return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
      if (response && response.data) {
        this.setState({
          Prix: response.data
        });
      }
    }).catch(error => {
      console.error(error);
    });
  }

  handleRowDelete(row) {
    const rowDataCopy = this.state.rowData.slice(0);
    rowDataCopy.splice(row, 1);
    this.setState({
      rowData: rowDataCopy
    });
  }
  handleRowAdd() {
    let id = this.state.id;
    id = id++;
    const rowDataCopy = this.state.rowData.slice(0);
    rowDataCopy.push({
      selectprdt: "",
      QuantiteF: 0,
      Prix: ""
    });
    this.setState({
      rowData: rowDataCopy,
      id: id
    });
  }
}
export default AjouterFacture;

请问我该如何解决?

【问题讨论】:

    标签: javascript reactjs html-table rows jsx


    【解决方案1】:
    1. 关于显示价格。您没有节省价格(连接)。而是在 PrixDisplay 中重置它。所以在 state.Prix 里面你只有一个值。您可能需要解析为 state.rowData[i].Prix。并在渲染中实际显示它。完全删除 state.Prix。
    2. 关于 NaN。因为您的 PrixDisplay 函数返回 Promise 它无法被解析。所以南。了解第一个问题和解决方案。第二个将随之而来。

    另外,作为建议,请查看 Array 的 map、filter 方法并尝试重构。它将使您的代码更加愉快和简洁。

    【讨论】:

    • 我更改了{ this.state.rowData[index].Prix.map(pr =&gt; &lt;p key={index} &gt;{pr.PrixV} &lt;/p&gt; )},但仍然是同样的问题。
    • 好的。将 handleselectprdtChange 更改为此 codepen.io/anon/pen/bxbvKm。而在内部渲染而不是&lt;td&gt;state.Prix.map(pr =&gt; &lt;p key={index} &gt;{pr.PrixV} &lt;/p&gt;)}&lt;/td&gt; 只是&lt;td&gt;{data.Prix}&lt;/td&gt;
    • 当我运行它时,我得到:TypeError:Cannot read property 'slice' of undefined on rowDataCopy: [ 在渲染之前有let { Prix } = this.state.Prix;
    • 你需要显示行的价格,而不是从某个地方得到它
    • 当我选择产品名称时,它仍然在选项 choissir un produit 上,我无法选择名称
    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2014-12-23
    • 2018-09-16
    相关资源
    最近更新 更多