vue-table实现表单操作

vue-demo.html

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" type="text/css" href="vue-css-demo.css" />
<body>

   <div id="app">
       <table>
           <thead>
               <tr>
                   <th>ID</th>
                   <th>商品名</th>
                   <th>单价</th>
                   <th>数量</th>
                   <th>操作</th>
               </tr>
           </thead>
           <tbody>
           <tr v-for="(item,index) in books">
               <td>{{item.id}}</td>
               <td>{{item.name}}</td>
               <td>{{item.price | showPrice }}</td>
               <td>
                   <button v-bind:disabled="item.num <=1" v-on:click="decrement(index)">-</button>
                   {{item.num}}
                   <button v-on:click="increment(index)">+</button>
               </td>
               <td><button v-on:click="removeOne(index)">delete</button></td>
           </tr>
           </tbody>
       </table>
       <div>总价:{{getTotal}}</div>
   </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="vue-js-demo.js"></script>
</html>

vue-js-demo.js

const app = new Vue({
    el:"#app",
    data:{
        //存入数据
        books:[
            {
                id:1,
                name:"计算机网络",
                price: 10,
                num:0
            },
            {
                id:2,
                name:"J2EE",
                price: 11,
                num:0
            },
            {
                id:3,
                name:"计算机组成原理",
                price: 12,
                num:0
            },
            {
                id:4,
                name:"计算机基础",
                price: 13,
                num:0
            }
        ],
    },
    methods:{
        increment(index){
            this.books[index].num++
        },
        decrement(index){
            this.books[index].num--
        },
        //删除:splice(start,count) 从哪里开始,删除多少个数  splice可用于删除,修改,新增
        removeOne(index){
            this.books.splice(index,1);
        }
    },
    computed: {
        getTotal(){
            let all = 0;
            for(var i=0;i<this.books.length;i++){
                all+=this.books[i].price*this.books[i].num;
            }
                return all;
            }
    },

    //过滤器:返回特定格式的数据展示
    filters:{
        showPrice(price){
            return '¥'+price.toFixed(2)
        }
    }

})

效果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值