2017-09-12 12 views
1

사용자가 제품 수량을 선택할 수있는 입력을 포함하는 반응 구성 요소를 JSON 배열에서 가져 왔습니다. 현재의 문제는 모든 제품 가격에 수량을 곱하여 합계로 출력하는 방법이 필요하다는 것입니다. 각 구성 요소에 대해이 작업을 수행했지만 실제로 수량이 변경되면 업데이트 된 누적 합계가 필요합니다. 여기에있는 어려움은 배열의 일부 값이 주변 장치로 분류되지 않을 수 있으므로 포함되지 않는다는 것입니다.총 배열 값에 반응 값의 입력 값을 곱합니다.

새로운 반응을 보이기 위해서는 누군가가 해결책을 제안 할 수 있기를 바랍니다. 모든 조언을 크게 주시면 감사하겠습니다.

약자로 내 코드

import React, { Component } from "react"; 
    import "./App.css"; 
    import productdata from "./catalog.json"; 
    import productSort from "./OrderDetails"; 

    class App extends Component { 
     constructor(props) { 
     super(props); 

     this.state = { 
      BundleVal: "", 
      ProductCounter: [], 
      TotalCounter: [], 
      PeripheralCounter: 0, 
      PriceCounter: 0 

     }; 
     } 

     updateBundle = val => { 
     this.setState({ 
      BundleVal: val 
     }); 
     }; 

     updateQuantity = e => { 
     var pCounter = this.state.ProductCounter; 
     var el = parseInt(e.target.id.split("_")[1], 10); 
     pCounter[el] = parseInt(e.target.value, 10); 
     this.setState({ ProductCounter: pCounter }); 
     }; 

     render() { 
     const BundleProducts = [].concat(productSort).map((item, i) => 
      <div key={item.id}> 
      {item.id} <br /> 
      {item.name} <br /> 
      {item.description} <br /> 
      Installation: {item.price.installation} <br /> 
      Monthly: {item.price.recurring} <br /> 
      <input 
       type="number" 
       onChange={this.updateQuantity} 
       value={this.state.ProductCounter[item.id] || 0} 
       id={"product_" + item.id} 
      /> 
      <br /> 
      {this.state.ProductCounter[item.id] || 0}<br /> 

      Installation Total: £{parseFloat(this.state.ProductCounter[item.id] * item.price.installation, 10).toFixed(2)} 
      <br /> 
      Monthly Total: £{parseFloat(this.state.ProductCounter[item.id] * item.price.recurring, 10).toFixed(2)} 
      <br /> 
      <hr /> 
      </div> 
     ); 



     this.state.PeripheralCounter = this.state.ProductCounter 
      .filter(function(qty, pid) { 
      pid = String(pid); 
      for (var i = 0; i < productSort.length; i++) { 
       var p = productSort[i]; 
       if (p.isPeripheral === true && p.id === pid) { 
       return true; 
       } 
      } 
      return false; 
      }) 
      .reduce(function(count, carry) { 
      return count + carry; 
      }, 0); 




     let bname = null; 
     let maxperipherals = null; 
     if (this.state.BundleVal === "1") { 
      bname = productdata.data.bundles[0].name; 
      maxperipherals = productdata.data.bundles[0].maximumPeripherals; 
     } else if (this.state.BundleVal === "2") { 
      bname = productdata.data.bundles[1].name; 
      maxperipherals = productdata.data.bundles[1].maximumPeripherals; 
     } else if (this.state.BundleVal === "3") { 
      bname = productdata.data.bundles[2].name; 
      maxperipherals = productdata.data.bundles[2].maximumPeripherals; 
     } else if (this.state.BundleVal === "4") { 
      bname = productdata.data.bundles[3].name; 
      maxperipherals = productdata.data.bundles[3].maximumPeripherals; 
     } else { 
      bname = null; 
     } 

     return (
      <div> 
      <h2>Order</h2> 
      Chosen Bundle: {bname} 
      <br /> 
      Number of Peripherals: {this.state.PeripheralCounter} 
      <br /> 
      Maximum Number of Peripherals: {maxperipherals} 
      <br /> 
      Peripherals Installation Total: {} 
      <br /> 
      <Bundle updateBundle={this.updateBundle} /> 
      <h3>Products</h3> 
      {BundleProducts} 

      </div> 
     ); 
     } 
    } 



    class Bundle extends React.Component { 
     constructor(props) { 
     super(props); 

     this.state = { 
      BundleVal: "" 
     }; 
     } 

     updatebundle = e => { 
     this.props.updateBundle(e.target.value); 
     this.setState({ BundleVal: e.target.value }); 
     }; 

     render() { 
     return (
      <div> 
      <h4>Bundle</h4> 
      <input 
       type="radio" 
       value="1" 
       onChange={this.updatebundle} 
       checked={this.state.BundleVal === "1"} 
      /> 
      {" "}Indoor Camera RAPID 
      <input 
       type="radio" 
       value="2" 
       onChange={this.updatebundle} 
       checked={this.state.BundleVal === "2"} 
      /> 
      {" "}Alarm Only RAPID 
      <input 
       type="radio" 
       value="3" 
       onChange={this.updatebundle} 
       checked={this.state.BundleVal === "3"} 
      /> 
      {" "}Outdoor Camera RAPID 
      <input 
       type="radio" 
       value="4" 
       onChange={this.updatebundle} 
       checked={this.state.BundleVal === "4"} 
      /> 
      {" "}Build Your Own Bundle 
      </div> 
     ); 
     } 
    } 

    class Product extends React.Component { 
     constructor(props) { 
     super(props); 

     this.state = { 
      ProductVal: "" 
     }; 
     } 

     updateproduct = e => { 
     this.props.updateProduct(e.target.value); 
     this.setState({ ProductVal: e.target.value }); 
     }; 

     render() { 
     return (
      <div> 
      <h4>Product</h4> 
      <br /> 
      <input type="number" onChange={this.updateproduct} /> 

      </div> 
     ); 
     } 
    } 
    export default App; 

이 코드는 또한

{ 
    "timestamp": 1502121471, 
    "data": { 
     "adverts": [], 
     "bundles": [{ 
      "id": "1", 
      "name": "Bundle 1", 
      "description": "Bundle 1 Description", 
      "maximumPeripherals": 32, 
      "available": true, 
      "count": 0, 
      "price": { 
       "installation": "99.99", 
       "recurring": "23.99" 
      }, 
      "image": { 
       "file": "bundle-one.png", 
      }, 
      "products": ["1", "2", "3"] 
     }, { 
      "id": "2", 
      "name": "Bundle 2", 
      "description": "Bundle 2 Description", 
      "maximumPeripherals": 32, 
      "available": true, 
      "count": 0, 
      "price": { 
       "installation": "99.99", 
       "recurring": "23.99" 
      }, 
      "image": { 
       "file": "bundle-two.png", 

      }, 
      "products": ["1", "2", "2", "2", "2"] 
     }], 
     "products": [{ 
      "id": "1", 
      "name": "Product 1", 
      "description": "Product 1 Description", 
      "maximumQuantity": 1, 
      "isPeripheral": false, 
      "isAvailable": true, 
      "price": { 
       "upfront": null, 
       "installation": "0.00", 
       "recurring": "0.00" 
      }, 
      "image": { 
       "file": "product-one.png", 
      } 
     }, { 
      "id": "2", 
      "name": "Product 2", 
      "description": "Product 2 Description", 
      "maximumQuantity": null, 
      "isPeripheral": true, 
      "isAvailable": true, 
      "count": 0, 
      "price": { 
       "upfront": "60.00", 
       "installation": "9.60", 
       "recurring": "1.25" 
      }, 
      "image": { 
       "file": "product-two.png", 
      } 
     }, { 
      "id": "3", 
      "name": "Product Three", 
      "description": "Product Three Description", 
      "maximumQuantity": null, 
      "isPeripheral": true, 
      "isAvailable": true, 
      "count": 0, 
      "price": { 
       "upfront": "132.00", 
       "installation": "9.60", 
       "recurring": "2.75" 
      }, 
      "image": { 
       "file": "product-three.png", 
      } 
     }] 
    } 
} 
다음과 같이

var productSort = productdata.data.products; 

productSort.sort(function(a, b){return a.id - b.id}); 

내 JSON 값의 예는 별도의 구성 요소에서 화학식 당긴 이하

답변

0

약간의 생각이 들었지만 대답이 있습니다. 기본적으로 BundleProducts로 만든 배열을 반복하고 설치 비용과 수량 입력을 포함하는 소품 내부의 자식을 곱하는 for 루프입니다 (이 경우 각각 10과 19). 누구든지 더 나은 해결책을 가지고 있다면 언제든지 게시하십시오.

var myTotal = 0; 
    for(var i = 0, len = BundleProducts.length; i < len; i++) { 
    myTotal += BundleProducts[i].props.children[19] * BundleProducts[i].props.children[10]; 
}