简体   繁体   English

如何使用 ReactJS 在动态表上显示每行的价格?

[英]How to display the price for each rows on dynamic table using ReactJS?

I have a dynamic table and for each rows, I want after I select the product name, The price will be displayed and when I put the quantity, The total (price* quantity) column will be displayed also.我有一个动态表,对于每一行,我想在选择产品名称后,将显示价格,当我输入数量时,还将显示总(价格*数量)列。

My issue is when I select the name of the product for any row, the same price will be displayed for each row on the table, also when I want the total display it's always having NaN as you see below on the figure :我的问题是,当我为任何行选择产品名称时,表格上的每一行都会显示相同的价格,而且当我想要总显示时,它总是具有 NaN,如下图所示:

在此处输入图片说明

My code :我的代码:

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;

How can I fix that please ?请问我该如何解决?

  1. About showing price.关于显示价格。 You are not saving the price(concatanating).您没有节省价格(连接)。 Rather resetting it inside PrixDisplay.而是在 PrixDisplay 中重置它。 So inside state.Prix you have only one value.所以在 state.Prix 中你只有一个值。 You probably need to parse into state.rowData[i].Prix.您可能需要解析为 state.rowData[i].Prix。 And show actually it inside render.并在渲染中实际显示它。 Remove the altogether state.Prix.删除完全state.prix。
  2. About the NaN.关于 NaN。 Because your PrixDisplay function return Promise it can not be parsed.因为您的 PrixDisplay 函数返回 Promise 它无法解析。 So NaN.所以NaN。 Understand first issue and solution.了解第一个问题和解决方案。 The second will come with it.第二个将随之而来。

Also, as a suggestion, please look to map, filter methods of Array and try to refactore.另外,作为建议,请查看 Array 的映射、过滤方法并尝试重构。 It will make you code more pleasant and concise.它会让你的代码更加愉快和简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM