vue基础购物车

简介: vue基础购物车

html:


<div id="box">
    <table border="1" cellspacing="0" cellpadding="20">
    <thead>
      <tr>
      <th>名称</th>
      <th>价格</th>
      <th>数量</th>
      <th>操作</th>
      </tr>
    </thead>
    <tbody id="tbody">
      <tr v-for="(item,index) in list" :key="item.id"
      :style="{'background-color':item.quantity>0?'#c4c4c4' : ''}">
      <td>{{item.name}}</td>
      <td>{{item.number}}</td>
      <td>
        <button @click="item.quantity--" v-show="item.quantity>0">-</button>
        <span>{{item.quantity}}</span>
        <button @click="item.quantity++">+</button>
      </td>
      <td>
        <button @click="edit(item,index)">编辑</button>
        <button @click="deletes(list,index)">删除</button>
        <!-- 删除头部,但只能删除头部 -->
      </td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
      <th></th>
      <th>总价:¥{{all_price()}}</th>
      <th>总数:{{all_num()}}</th>
      <th></th>
      </tr>
    </tfoot>
    </table>
    <button @click="hi()">添加商品</button>
    <div v-show="add">
    <input type="text" v-model="product_name" placeholder="请输入商品名称">
    <input type="text" v-model.number="commodity_prices" placeholder="请输入商品价格">
    <input type="text" v-model.number="quantity_of_commodity" placeholder="请输入商品数量">
    <button @click="edit_save(list,product_name,commodity_prices,quantity_of_commodity)">确定</button>
    <button @click="hide()">取消</button>
    </div>
    <div v-show="add_s">
    <input type="text" v-model="edit_data.name" placeholder="请输入商品名称">
    <input type="text" v-model.number="edit_data.number" placeholder="请输入商品价格">
    <input type="text" v-model.number="edit_data.quantity" placeholder="请输入商品数量">
    <button @click="edit_savesss(item,index)">确定</button>
    <button @click="hidess()">取消</button>
    </div>
  </div>

vue:


<script>
    const {
    createApp,
    ref,
    reactive
    } = Vue;
    createApp({
    setup() {
      let add = ref(false)
      let add_all = ref(false)
      // 商品全部为false
      const list = ref([{
        id: 1,
        name: "衣服",
        quantity: 0,
        number: "120",
      },
      {
        id: 2,
        name: "鞋子",
        quantity: 0,
        number: "80",
      },
      {
        id: 3,
        name: "wa6子",
        quantity: 0,
        number: "860",
      },
      ])
      // 创建一个数组
      const edit_data = reactive({
      id: 1,
      name: "",
      quantity: 0,
      number: 0,
      })
      // 商品总数
      let all_num = () => {
      let quantity = 0;
      list.value.forEach(item => {
        quantity += item.quantity
      })
      return quantity;
      }
      // 定义下标
      const edit_index = ref('0')
      // 总价
      let all_price = () => {
      // console.log(JSON.parse(JSON.stringify(list.value)));
      // return JSON.parse(JSON.stringify(list.value)).reduce((a, b) => a + (b.quantity * b.number), 0);
      return list.value.reduce((a, b) => a + (b.quantity * b.number), 0);
      }
      // let all_price = () => {
      //  return list.value.reduce((a, b) =>a.quantity * a.number + b.quantity * b.number);
      // }
      // 点击添加商品显示输入框
      let commodities = () => {
      add.value = true;
      }
      // 点击添加商品
      const product_name = ref('')
      console.log(product_name);
      // 获取商品名称
      const commodity_prices = ref('')
      console.log(commodity_prices);
      // 获取商品价格
      const quantity_of_commodity = ref('')
      console.log(quantity_of_commodity);
      // 获取商品数量
      let edit = (item, index) => {
      add_all.value = true
      Object.assign(edit_data, item)
      // add.value = true;
      edit_index.value = index;
      }
      // 编辑确定
      let ensure = () => {
      list.value[edit_index.value] = edit_data;
      add_all.value = false;
      }
      // 编辑取消关闭
      let abolish = () => {
      add_all.value = false;
      }
      // 新增取消关闭
      let hide = () => {
      add.value = false
      console.log(add);
      }
      // 新增确认
      const edit_save = (list, product_name, commodity_prices, quantity_of_commodity) => {
      let ojj = {
        name: product_name,
        quantity: commodity_prices,
        number: quantity_of_commodity,
      }
      list.push(ojj)
      add.value = true;
      // counts()
      deletes()
      }
      // 删除
      function deletes(list, i) {
      list.splice(i, 1);
      }
      return {
      hide,
      product_name,
      commodity_prices,
      quantity_of_commodity,
      list,
      add,
      commodities,
      deletes,
      all_num,
      all_price,
      edit,
      edit_data,
      edit_index,
      edit_save,
      add_all,
      ensure,
      abolish
      }
    }
    }).mount('#box')
  </script>


相关文章
|
1天前
|
JavaScript 前端开发 Java
开发语言漫谈-Vue
Vue严格说来不是一门语言
|
2天前
|
JavaScript 前端开发 C++
【Vue】Vue快速入门、Vue常用指令、Vue的生命周期
【Vue】Vue快速入门、Vue常用指令、Vue的生命周期
13 2
|
4天前
|
存储 缓存 JavaScript
谈谈你对vue的了解
谈谈你对vue的了解
14 3
|
5天前
|
JavaScript API
Vue axjx 跨域请求
Vue应用中解决跨域问题可通过配置代理服务器。方法一是设置`devServer.proxy`到目标服务器端口,如`http://localhost:3000`,然后使用axios发送请求。方法二是为多个代理配置,如`/api`和`/api2`,分别指向不同端口,利用`pathRewrite`重写路径。请求时需加上相应前缀,如`/api/index`。
14 1
|
5天前
|
JavaScript 前端开发 Java
JavaSE精选-Vue
JavaSE精选-Vue
7 1
|
5天前
|
JavaScript
Vue父子组件传值
<!-- 父组件向子组件传地值 -->
15 2
|
5天前
|
JavaScript Java
Vue : v-if, v-show
Vue : v-if, v-show
13 0
Vue : v-if, v-show
|
5天前
|
JavaScript 前端开发
Vue : 监视属性
Vue : 监视属性
9 0
|
5天前
|
缓存 JavaScript
Vue: 事件修饰符, 键盘事件, 鼠标事件,计算属性
Vue: 事件修饰符, 键盘事件, 鼠标事件,计算属性
23 2
|
5天前
|
JavaScript 安全
一文彻底学会vue的组件通信
Vue组件通信包括父向子通过`props`传递数据,如`&lt;Student :name=&quot;18&quot;&gt;&lt;/Student&gt;`,子组件通过`$emit`触发自定义事件与父组件通信,如`this.$emit(&#39;myEvent&#39;, this.name)`。此外,使用全局事件总线(`Vue.prototype.$bus`)可在多级组件间通信,通过`$on`订阅和`$emit`发布事件。还有消息订阅与发布模式,利用`pubsub-js`库实现更灵活的组件交互。
15 0
一文彻底学会vue的组件通信