使用vue的组件,来写一个表格

在全局注册一个组件。该组件是负责展示表格数据的。

该页面可以在前端进行删除和查找 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>田贵梯组件间传值</title>
    <style>
        body{
            width: auto;
            height: 300px;
            text-align: center;

        }
        #app{
            margin: auto;
            display: inline-block;
        }
        button{
            height: 30px;
            width: 60px;
            border-radius: 5px;
            border: 0;
            color:#ffffff;
            background-color:deepskyblue;
        }
        td{
            width: 100px;
            font-size: x-small;
            text-align: left;
        }
        .selectMod{
            text-align: left;
        }
        .selectMod input{
            height: 20px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
   
    <div id="app">   
       <div class="selectMod">
        名称 
        <input v-model="seleData">
        <button @click="select">查找</button>
       </div>
        <my-component :items="viewData" :price="prices" ></my-component>      
    </div>
   
     
    <script src="vue.js"></script>
    <script>

        Vue.component('my-component',{
        
            props:['items','price'],
            template:`
            <table border="2" cellspacing="0">
                <tr >
                    <td >
                      <h5>ID</h5>
                      </td >
                     <td >
                        <h5>主标题</h5>
                      </td >
                      <td >
                        <h5>起步价格</h5>
                      </td > 
                      <td >
                        <h5>显示</h5>
                      </td >
                      <td >
                        <h5>操作</h5>
                    </td >
                 </tr>
                
                 <tr  v-for="item in items" >
                    <td >
                       {{item.id}}
                      </td >
                     <td >
                       {{item.title}}
                      </td >
                      <td >
                       {{item.price_info}}
                      </td > 
                      <td >
                       {{item.is_show}}
                      </td >
                      <td >
                      <button @click="link(item.id)" >删除</button>
                    </td >
                 </tr>

                 <tr> 
                    <td colspan="5" text-align=right >
                        总价为:¥{{price}}
                    </td> 
                </tr>
             </table>
            `,
            methods:{
                link(deleteid){
                    var r=confirm("确定删除id为"+deleteid+"的记录吗");
                        if (r==true)
                        {
                            for(let i=0;i<this.items.length;i++){
                            let item = this.items[i]
                            if(item.id==deleteid){
                                this.items.splice(i,1)
                            }
                        }
                        }
                        else
                        {
                           alert("取消删除")
                        }
                      
                    }
            }
        })

        new Vue({
            el:'#app',
            data:{
                seleData:"",//展示数据
                prices:0,//总价格
                viewData:[],//子组件表格数据
                tableData:[{
                        "id": 287,
                        "title": "严选新式样板间",
                        "price_info": 29.9,
                        "is_show": 1
                    }, {
                        "id": 286,
                        "title": "无“油”无虑的甜蜜酥脆",	
                        "price_info": 45,
                        "is_show": 1
                    }, {
                        "id": 283,
                        "title": "孩子成长中少不了的一双鞋",
                        "price_info": 78,
                        "is_show": 1
                    }, {
                        "id": 282,
                        "title": "成就一室笋香1",
                        "price_info": 121,
                        "is_show": 1
                    }, {
                        "id": 281,
                        "title": "条纹新风尚",
                        "price_info": 29,
                        "is_show": 1
                    }, {
                        "id": 277,
                        "title": "治愈生活的满怀柔软",
                        "price_info": 66.78,

                        "is_show": 1
                    }, {
                        "id": 274,
                        "title": "没有软木拖,怎么过夏天",
                        "price_info": 50.99,
                        "is_show": 1
                    }, {
                        "id": 272,
                        "title": "料理也要精细简单",
                        "price_info": 69,
                        "is_show": 1
                    }, {
                        "id": 271,
                        "title": "选式新懒人",
                        "price_info": 15.3,
                        "is_show": 1
                    }, {
                        "id": 268,
                        "title": "米饭好吃的秘诀:会呼吸的锅",
                        "price_info": 20.1,
                        "is_show": 1
                    }]
            },
            methods:{
                select(){
                    let num=0
                    let price=0
                    this.viewData=[]
                    for(let i=0;i<this.tableData.length;i++){
                        let item = this.tableData[i]
                       if(item.title.indexOf(this.seleData)>0){
                          this.viewData[num]=item
                          price+=item.price_info
                          num++
                       }
                    }
                    if(this.viewData.length==0){
                        alert("抱歉没有您要找的记录")
                        this.viewData=this.tableData
                    for(let i =0; i<this.tableData.length;i++){
                    let item = this.tableData[i];
                    this.prices+=item.price_info    
                }
                    }else{
                        this.prices=price
                    }
                    
                }
            },
            mounted(){
                console.log(this.tableData)
                this.viewData=this.tableData
                for(let i =0; i<this.tableData.length;i++){
                    let item = this.tableData[i];
                    this.prices+=item.price_info    
                }
                this.prices=this.prices.toFixed(2)
                 console.log(this.prices)
        
    }

        });
    </script>
</body>
<script>
</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weighless1129

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值