jspang 做个那个pos系统--学习笔记

/为什么不能使用Object.assign() //使用Object.assign之后数据会发生改变,但是试图没有跟新


<template>
  <div class="pos">
    <el-row>
      <el-col :span='7' class="pos-order" id="order-list">
        <el-tabs>
          <!--点餐-->
          <el-tab-pane label="点餐">
             <el-table :data= 'tableData'   style="width:100%;">
               <el-table-column prop="goodsName" label="商品名称"></el-table-column>
               <el-table-column prop="count" label="数量" width="70"></el-table-column>
               <el-table-column prop="price" label="金额" width="70"></el-table-column>
               <el-table-column  label="操作" width="100" fixed="right">
                 <template scope="scope">
                    <el-button type="text" size="small">删除</el-button>
                    <el-button type="text" size="small">增加</el-button>
                 </template>
               </el-table-column>
             </el-table>
             <div>
                 总数:0    总价:0 元
             </div>
             <div class="btn">
               <el-button type="warning">挂单</el-button>
               <el-button type="danger">删除</el-button>
               <el-button type="success">结账</el-button>
             </div>
          </el-tab-pane>
          <!--挂单-->
          <el-tab-pane label="挂单">

          </el-tab-pane>
          <!--外卖-->
          <el-tab-pane label="外卖">

          </el-tab-pane>
        </el-tabs>
      </el-col>
       <el-col :span='14' >
          <div class="goods">
            <div class="title">常用商品</div>
            <div class="goods-list">
              <ul>
                <li v-for="item  of oftenGoods" :key="item.id" @click="addOrderList(item)">
                  <span>{{item.goodsName}}</span>
                  <span class="o-price">¥{{item.price}}</span>
                </li>
              </ul>
            </div>
          </div>

          <div class="goods-type">
              <el-tabs>
                <el-tab-pane label="汉堡">
                    <div>
                      <ul class='cookList'>
                          <li v-for=" list in type0Goods" :key="list.id">
                              <span class="foodImg"><img :src="list.goodsImg" width="100%"></span>
                              <span class="foodName">{{list.goodsName}}</span>
                              <span class="foodPrice">¥{{list.price}}元</span>
                          </li>
                      </ul>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="小食">
                  <div>
                      <ul class='cookList'>
                          <li v-for=" list in type0Goods1" :key="list.id">
                              <span class="foodImg"><img :src="list.goodsImg" width="100%"></span>
                              <span class="foodName">{{list.goodsName}}</span>
                              <span class="foodPrice">¥{{list.price}}元</span>
                          </li>
                      </ul>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="饮料">
                  <div>
                      <ul class='cookList'>
                          <li v-for=" list in type0Goods2" :key="list.id">
                              <span class="foodImg"><img :src="list.goodsImg" width="100%"></span>
                              <span class="foodName">{{list.goodsName}}</span>
                              <span class="foodPrice">¥{{list.price}}元</span>
                          </li>
                      </ul>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="套餐">
                  <div>
                      <ul class='cookList'>
                          <li v-for=" list in type0Goods3" :key="list.id">
                              <span class="foodImg"><img :src="list.goodsImg" width="100%"></span>
                              <span class="foodName">{{list.goodsName}}</span>
                              <span class="foodPrice">¥{{list.price}}元</span>
                          </li>
                      </ul>
                    </div>
                </el-tab-pane>
              </el-tabs>
          </div>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'pos',
  data(){
    return{
        tableData: [],
        tableData1: [],
        oftenGoods:[],
        type0Goods:[],
        type0Goods1:[],
        type0Goods2:[],
        type0Goods3:[],
        totalMoney:0,
        totalCount:0
    }
  },
  //在虚拟dom加载完成之后
  mounted:function(){
    let orderHeight =  document.body.clientHeight;
    console.log(orderHeight);
    document.getElementById('order-list').style.height =orderHeight +'px';
  },
  created(){
      //获取常用商品
      axios.get('http://jspang.com/DemoApi/oftenGoods.php')
        .then(response => {
           // console.log( response );
            this.oftenGoods = response.data
        })
        .catch(error =>{
            console.log( error )
            alert('网路错误,不能访问')
        })
       //分类商品数据
       axios.get('http://jspang.com/DemoApi/typeGoods.php')
        .then(response => {
           // console.log( response );
            this.type0Goods = response.data[0]
             this.type0Goods1 = response.data[1]
              this.type0Goods2 = response.data[2]
               this.type0Goods3 = response.data[3]
        })
        .catch(error =>{
            console.log( error )
            alert('网路错误,不能访问')
        })
  },
  methods:{
      addOrderList(goods){

          this.totalMoney =0;
          this.totalCount =0;
            //商品是否已经存在于订单列表中
            let isHave = false;
            for(let i =0; i< this.tableData.length;i++ ){
                if(this.tableData[i].goodsId == goods.goodsId ){
                    isHave = true;
                }
            }
            //根据判断的值编写业务逻辑
            if(isHave){
               let arr = this.tableData.filter( ele =>
                   //过滤条件,返回
                   ele.goodsId == goods.goodsId
               )
               arr[0].count++;
            }else{
                //在这里面添加
                //let newGoods = Object.assign(goods,{count:1})     //这里为什么不能使用Object.assign()  //使用Object.assign之后数据会发生改变,但是试图没有跟新
                 let newGoods = {
                     goodsId:goods.goodsId,
                     goodsName:goods.goodsName,
                     price:goods.price,
                     count:1
                }
                this.tableData.push(newGoods)

                //this.$set(this.tableData,newGoods)
                 
                console.log(this.tableData)
            }
      }
  },
  
  watch:{
     tableData:{
        handler(a,b){
            this.tableData= a;
           console.log(a ,b)
        },
        deep:true
      },
  }
}
</script>

转载于:https://www.cnblogs.com/he-zhi/p/7257029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值