ショッピングカートの実現(三)


実装方法と論理コード注釈には
class Goods {
  constructor() {
    Goods.list();
  }
  //        
  // static     
  //           
  static list () {
    //  ajax  
    axios.get('http://localhost/day1012-27/cart2/server/server.php?fn=lst')
      .then(res => { //res        
        // console.log(res);
        //        
        let { meta, data } = JSON.parse(res);
        console.log(meta, data);
        //       
        let html = '';
        if (meta.status == 200) {
          //                 
          //     
          data.forEach(ele => {
            let { id, goodsName, goodsImg, price } = ele;
            html += `

${goodsName}

¥${price}カートに れる
`; }) // let cont = document.querySelector('#cont') cont.innerHTML = html; } }) } // static addCart (id, num) { console.log(id, num); /* : 1 cart key 2 2-1 2-2 3 cart */ // local let cartGoods = localStorage.getItem('cart'); // console.log(cartGoods); //2 if (cartGoods) { cartGoods = JSON.parse(cartGoods); //2 // for in attr id for (let attr in cartGoods) { if (attr == id) { //2-1 num = num + cartGoods[id]; //cartGoods[id] id id num 1 }//2-2 } cartGoods[id] = num; // cartGoods localStorage.setItem('cart', JSON.stringify(cartGoods)) } else { //3 cart cartGoods = { [id]: num }; console.log(cartGoods); // cartGoods = JSON.stringify(cartGoods); localStorage.setItem('cart', cartGoods); } } } new Goods();

PHP部

[
    'status'=>200,
    'msg'=>'  '
  ],
  'data'=>$data
]);
}

?>
[
         "status"=>200,
        "msg"=>"      "
       ],
       "data"=>$res
     ]);
   }
 }
?>