Guest User

Untitled

a guest
Aug 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. class AjouterFacture extends Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. rowData: [],
  6. Produits: [],
  7. Quantite: "",
  8. Prix: ""
  9. };
  10.  
  11. this.handleRowChange = this.handleRowChange.bind(this);
  12. this.handleRowDelete = this.handleRowDelete.bind(this);
  13. this.handleRowAdd = this.handleRowAdd.bind(this);
  14. this.getTotal = this.getTotal.bind(this);
  15. this.pushToCaller = this.pushToCaller.bind(this);
  16. }
  17.  
  18. pushToCaller() {
  19. this.handleRowChange(this.state.id, {
  20. Quantite: parseInt(this.state.Quantite, 10),
  21. selectprdt: this.state.selectprdt,
  22. Prix: parseFloat(this.state.Prix),
  23. });
  24. }
  25.  
  26. render() {
  27.  
  28. return (<div className="animated fadeIn">
  29.  
  30.  
  31.  
  32. <h6> Veuillez ajouter au moins un produit : </h6>
  33. <Table >
  34. <thead >
  35. <tr>
  36. <th>PRODUIT</th>
  37. <th>QUANTITE</th>
  38. <th>PRIX UNITAIRE</th>
  39. <th>TOTAL</th>
  40. <th></th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. {this.state.rowData.map((index) =>
  45.  
  46. <tr key={index} id={index}
  47. onChange={this.handleRowChange}>
  48.  
  49.  
  50. <td> <Input type="select" name="selectedcl" id="selectcl"
  51. placeholder="Veuillez sélectionner un produit" value={this.state.rowData.selectprdt}
  52. onChange={this.handleselectprdtChange} >
  53. <option key={-1} hidden>Choisisr un produit</option>
  54.  
  55.  
  56. { this.state.Produits.map((pdt, i) =>
  57. <option key={i} >{pdt.Nomp}</option>
  58. )}
  59.  
  60.  
  61. </Input>
  62. </td>
  63. <td><Input type="number"
  64. placeholder="0" value={this.state.rowData.Quantite} onChange={this.handleQuantiteChange}/></td>
  65. <td>
  66. <InputGroup ><Input type="text"
  67. value={this.state.rowData.Prix} onChange={this.handlePrixChange} />
  68. <InputGroupAddon addonType="prepend">
  69. <InputGroupText><i ></i></InputGroupText>
  70. </InputGroupAddon>
  71. </InputGroup >
  72. </td>
  73.  
  74. <td >
  75. <p >{this.state.Quantite * this.state.Prix} </p>
  76.  
  77. </td>
  78. <td>
  79. <Button onClick={this.handleRowDelete} active style={center} >Effacer</Button>
  80. </td> </tr> )}
  81.  
  82.  
  83.  
  84. <tr>
  85.  
  86. <td></td>
  87. <td></td>
  88. <td></td>
  89. <td></td>
  90. <td><Button onClick={this.handleRowAdd} >Ajouter une ligne</Button></td>
  91. </tr>
  92. </tbody>
  93.  
  94. <tfoot>
  95. <tr>
  96.  
  97. <th></th>
  98. <th >Grand total :</th>
  99. <th>{this.getTotal()} </th>
  100. <th></th>
  101. </tr>
  102. </tfoot>
  103.  
  104. </Table>
  105.  
  106.  
  107. </div>);
  108. }
  109. getTotal() {
  110. let grandTotal = 0;
  111. const rowTotals = this.state.rowData.map(row => this.state.Quantite * this.state.Prix);
  112. if (rowTotals.length > 0) {
  113. grandTotal = rowTotals.reduce((acc, val) => acc + val);
  114. }
  115. return grandTotal;
  116. }
  117. handleRowChange(row, data) {
  118. const rowDataCopy = this.state.rowData.slice(0);
  119. rowDataCopy[row] = data;
  120. this.setState({
  121. rowData: rowDataCopy
  122.  
  123. });
  124. }
  125. handleRowDelete(row) {
  126. const rowDataCopy = this.state.rowData.slice(0);
  127. rowDataCopy.splice(row, 1);
  128. this.setState({
  129. rowData: rowDataCopy
  130. });
  131. }
  132. handleRowAdd() {
  133. const rowDataCopy = this.state.rowData.slice(0);
  134. rowDataCopy.push({selectprdt:'', Quantite : "", Prix :"" });
  135. this.setState({
  136. rowData: rowDataCopy
  137. });
  138. }
  139.  
  140. }
  141. export default AjouterFacture;
Add Comment
Please, Sign In to add comment