vue之表格制作

vue表格制作,代码如下:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>

</head>
<body>
<div id="app1">
    <table border="1">
        <tr>
            <th v-for="item in tableHeader">{{item}}</th>
        </tr>
        <tr v-for="(item,index) in dat">
            <td >{{index}}</td>
            <td >{{item.bookName}}</td>
            <td >{{currency}}{{item.price}}</td>
            <td >{{item.quantity}}</td>
            <td>
                <button v-on:click="reduce(index)">-</button><button v-on:click="add(index)">+</button><button v-on:click="clear(index)">清空</button>

            </td>
        </tr>
    </table>
    <p>总价:{{currency}}{{totalPrice()}}</p>

</div>

<script>
    const va = new Vue({
        el:'#app1',
        data:{
            tableHeader:["序号","书名","价格","数量","操作"],
            dat:[
                {bookName:"围城",price:"75",quantity:"0"},
                {bookName:"骆驼祥子",price:"55",quantity:"0"},
                {bookName:"百年孤独",price:"69",quantity:"0"},
                {bookName:"梦里花落知多少",price:"39",quantity:"0"}

            ],
            currency:"¥"
        },
        methods:{
            reduce(index){
                if(this.dat[index].quantity >= 1) return this.dat[index].quantity--
            },
            add(index){
                return this.dat[index].quantity++
            },
            clear(index){
                return this.dat[index].quantity = 0
            },
            totalPrice(){
                let da = this.dat.map(function (item){
                    return item['price'] * item['quantity']
                }).reduce(function (preVales,n){
                    return preVales + n
                },0);
                return da

            }
        }
    })
</script>

</body>
</html>

 

效果:

  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值