vue成绩单案例

HTML

 <div id="app">
       <table>
       <thead>
        <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>操作</th>
        </tr>
       </thead>

       <tbody v-if="list.length > 0">
        <tr v-for="(item,index) in list " :key="item.id">
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td :class="{red: item.agm < 60}">{{item.agm}}</td>
            <td><button @click="shchu(item.id)">删除</button></td>
        </tr>
       </tbody>

       <tbody v-else="list.length < 0">
        <tr>
            <td colspan="5">
                <p>没有数据</p>
            </td>
        </tr>
       </tbody>


       <tfoot>
        <tr>
            <td colspan="5">
                <span>总分:{{suan}}</span>
                <span>平均分:{{ping}}</span>
            </td>
        </tr>
       </tfoot>
    </table>
       <div>
        <form action="">
            <!-- trim 去掉首尾空格 number 转数字不会强转 -->
           <p>科目:<input v-model.trim="mu" type="text" placeholder="请输入科目"></p>
           <p>成绩:<input v-model.number="ji" type="text" placeholder="请输入成绩"></p>
           <button type="button" @click="add">添加</button>
        </form>
       </div>
    </div>

css

table{
    width: 400px;
    border-collapse: collapse;
}
table tr td{
    height: 30px;
    text-align: center;
    border: 1px solid rgb(205, 202, 202);
}
table thead tr th{
    height: 40px;
    border: 1px solid rgb(205,202,202);
    background-color: rgb(134, 133, 133);
}

.red{
    color: red;
}

js

  const app = new Vue({
            el:'#app',
            data:{
                list:[
                    {id:1,name:'数学',agm:30},
                    {id:2,name:'英语',agm:80},
                    {id:3,name:'历史',agm:90},
                ],
                mu:'',
                ji:'',
            },
            methods:{
                shchu(id){
                    console.log(id);
                    // 删除数据
                    this.list= this.list.filter(item =>item.id !==id)
                    
                },
                add(){
                    if(!this.mu){
                        alert("请输入科目")
                        return
                    }
                    if(typeof this.ji !== 'number'){
                        alert("请输入数字成绩")
                        return
                    }
                    // 添加数据
                    this.list.unshift({
                        id: +new Date(),
                        name:this.mu,
                        agm:this.ji
                    })
                    // 清空输入框
                    this.mu='',
                    this.ji=''
                }
            },
            // 计算
            computed:{
                //总合  箭头函数
                suan(){
                    //reduce 累加器 返回总和
                    return this.list.reduce((sum,item)=>sum +item.agm,0)
                },
                // // 不是箭头函数
                // suan(){
                //     function suan(){
                //        return this.list.reduce(sum,item)= sum+item.agm
                //     }
                // },

                //平均值
                ping(){
                    if(this.list.length === 0)
                    {
                        return 0
                    }
                   return (this.suan / this.list.length).toFixed(2)
                }
            }
            
        })
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

原生爱好者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值