Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ReactJS获取行的总数?_Javascript_Reactjs_Row_Addition_Dynamic Tables - Fatal编程技术网

Javascript 如何使用ReactJS获取行的总数?

Javascript 如何使用ReactJS获取行的总数?,javascript,reactjs,row,addition,dynamic-tables,Javascript,Reactjs,Row,Addition,Dynamic Tables,我有一个动态表,如下图所示: 我试图显示“总计”的值,但它始终为0。这是每行的总和 我的代码如下: class AjouterFacture extends Component { constructor(props) { super(props); this.state = { rowData: [], alert: null, Produits: [], QuantiteF: "", Prix: [],

我有一个动态表,如下图所示:

我试图显示“总计”的值,但它始终为0。这是每行的总和

我的代码如下:

class AjouterFacture extends Component {
  constructor(props) {
    super(props);
    this.state = {
      rowData: [],
      alert: null,
      Produits: [],
      QuantiteF: "",
      Prix: [],
      id: 0,

      displayedPrice: 0
    };

    this.handleRowDelete = this.handleRowDelete.bind(this);
    this.handleRowAdd = this.handleRowAdd.bind(this);
    this.getTotal = this.getTotal.bind(this);
    this.handleselectprdtChange = this.handleselectprdtChange.bind(this);
    this.PrixDisplay = this.PrixDisplay.bind(this);
    this.handleQuantiteChange = this.handleQuantiteChange.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)
     // QuantiteF: value

    });

    this.setState({
      rowData: rowDataCopy
    });
  }

  handleselectprdtChange(index, value) {
    const rowDataCopy = this.state.rowData.slice(0);
    rowDataCopy[index] = Object.assign({}, rowDataCopy[index], {
      selectprdt: value
    });
    return axios
      .get('/app/getPrixprod/' + value)
      .then(response => {
        if (response && response.data) {
          this.setState(({Prix}) => ({
            rowData: rowDataCopy,
            Prix: [...Prix, ...response.data]

          }));
        }
      })
      .catch(error => {
        console.error(error);
      });
  }

  render() {
    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="text" 
                          value={data.QuantiteF || 0} onChange={(e) => this.handleQuantiteChange(index, e.target.value)}/></td>


                    <td>


                    {<p key={index}>{this.state.Prix[index] && this.state.Prix[index].PrixV} </p>}

                        </td>

                <td  > 


                    { <p key={index} className='pa2 mr2 f6'>{(data.QuantiteF || 0) * ((this.state.Prix[index] && this.state.Prix[index].PrixV)|| 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>

        <tfoot>
          <tr>

            <th></th>
            <th >Grand total :</th>
            <th>{this.getTotal()} </th>
            <th></th>
          </tr>
</tfoot>

        </Table>


        </div>);
  }


  getTotal() {
    let grandTotal = 0;
    const rowTotals = this.state.rowData.map(row => (row.QuantiteF * row.Prix) || 0);
    if (rowTotals.length > 0) {
      grandTotal = rowTotals.reduce((acc, val) => acc + val);
    }
    return grandTotal;
  }
  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;
我的桌子:

CREATE TABLE factures (
 Nomp varchar(20) REFERENCES produits (Nomp),
 QuantiteF  varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
我想得到每一行的总数!我的场景是,当我选择产品名称时,将显示其价格,当我输入数量编号时,将显示总计列!我想显示每行的总和


我如何解决这个问题?

尝试删除
const rowTotals=this.state.rowData.map(row=>(row.QuantiteF*row.Prix)| 0)中的
|124; 0
@evgenifotia仍然是
0
尝试添加此
rowDataCopy[index].Prix=response.data
this.setState(({Prix})=>({rowData:rowDataCopy,Prix:[…Prix,…response.data]})之前啊,你的response.data是一个数组,只需执行
rowDataCopy[index].Prix=response.data[0]
rowDataCopy[index]=Object.assign({},rowDataCopy[index],{QuantiteF:parseInt(value,10)//QuantiteF:value})之后,是否可以控制台记录`rowDataCopy[index]`
handleQuantitechRange
中,因为它是唯一一个更新数量的地方,控制台也会记录
CREATE TABLE factures (
 Nomp varchar(20) REFERENCES produits (Nomp),
 QuantiteF  varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;