码迷,mamicode.com
首页 > 其他好文 > 详细

vue使用Element UI案例(商品列表)

时间:2020-06-24 00:06:05      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:done   ble   method   handle   computed   ack   compute   高级   com   

Goods.vue

<template>
  <div id="goods">
    <el-button type="text" @click="dialogVisible = true">添加商品</el-button>
    <el-dialog
      title="添加商品"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <div class="demo-input-suffix">
        商品名称:
        <el-input
          placeholder="请输入内容"
          v-model="goods_name">
        </el-input>
      </div>
      <div class="demo-input-suffix">
        商品数量:
        <el-input
          placeholder="请输入内容"
          v-model="goods_num">
        </el-input>
      </div>
      <div class="demo-input-suffix">
        商品价格:
        <el-input
          placeholder="请输入内容"
          v-model="goods_price">
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
    <el-table
      :data="goods_list"
      border

      style="width: 100%">
      <el-table-column
        type="index"
        >
      </el-table-column>
      <el-table-column
        prop="name"
        label="商品名称"
        width="180">
      </el-table-column>

      <el-table-column
        prop="price"
        label="商品数量"
        width="180">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.num" @change="issub(scope.$index)" size="mini" :min="0"></el-input-number>
        </template>
      </el-table-column>

      <el-table-column
        prop="price"
        label="价格"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="update(scope)" type="text" size="small">编辑</el-button>
          <el-button type="text" size="small" @click="del(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-input v-model="total" style="width: 600px">
      <template slot="prepend">合价:</template>
      <template slot="append"></template>
    </el-input>

  </div>
</template>
<script>
  export default {
    name: "Goods",
    filters: {
      format(money) {
        return "" + money.toFixed(2);
      }
    },
    data() {
      return {
        dialogVisible: false,
        active: "zero_num",
        goods_name: "",
        goods_num: "",
        goods_price: "",
        goods_index: -1, // 当前本次操作的商品信息[-1表示新增,大于等于0表示编辑]
        goods_list: [
          {"name": "python入门", "num": 27, "price": 150},
          {"name": "python进阶", "num": 21, "price": 100},
          {"name": "python高级", "num": 17, "price": 75},
          {"name": "python研究", "num": 37, "price": 60},
          {"name": "python放弃", "num": 57, "price": 110},
        ]
      }
    },
    methods: {
      handleClose(done) {
        this.$confirm(确认关闭?)
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      issub(index) {
        if (this.goods_list[index].num === 0) {
          this.del(index)
        }
      },
      save() {
        // 保存数据[添加数据]
        if (this.goods_index == -1) {
          this.goods_list.push({
            "name": this.goods_name,
            "num": parseInt(this.goods_num),
            "price": parseFloat(this.goods_price),
          });
        } else {
          this.goods_list[this.goods_index].name = this.goods_name;
          this.goods_list[this.goods_index].num = parseInt(this.goods_num);
          this.goods_list[this.goods_index].price = parseFloat(this.goods_price);
        }
        this.cancel();
      },
      cancel() {
        this.dialogVisible = false;
        this.goods_index = -1;
        this.goods_name = "";
        this.goods_num = "";
        this.goods_price = "";
      },
      del(index) {
        // 删除数据
        this.goods_list.splice(index, 1);
      },
      update(index) {
        // 显示当前编辑的商品信息
        this.dialogVisible = true;
        console.log(index);
        this.goods_index = index.$index;
        this.goods_name = index.row.name;
        this.goods_num = index.row.num;
        this.goods_price = index.row.price;
        // 当用户点击保存时,修改对应数据
      },
    },
    computed: {
      total(){
        let ret = 0;
        this.goods_list.forEach(function (value, index) {
          // console.log(value,index);
          let sum_price = parseFloat(value.price) * parseFloat(value.num);
          ret = ret + sum_price
        });
        return ret
      }
    }
  }
</script>

<style scoped>
  #goods table {
    width: 600px;
    border: 1px solid #000;
    border-collapse: collapse;
  }

  #goods td, #goods th {
    border: 1px solid #000;
  }

  #goods .box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background-color: #eee;
    width: 280px;
    height: 160px;
    padding: 40px 80px;
  }
</style>

 

vue使用Element UI案例(商品列表)

标签:done   ble   method   handle   computed   ack   compute   高级   com   

原文地址:https://www.cnblogs.com/baicai37/p/13185035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!