作业38

作业38

1.完成商品数量的加减,注意商品数量如果低于0个,则自动删除当前商品,并完成购物车总价格的计算。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    #goods table{
        width: 900px;
        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>
    <script src="vue.js"></script>
</head>
<body>
<!--1. 在作业.html的代码基础上,完成商品数量的加减,注意商品数量如果低于0个,则自动删除当前商品-->
<!--2. 在作业.html的代码基础仧,完成购物车总价格的计算。-->
<!--3. 使用ajax获取北京天气,并把昨天和未来5天天气情况以表格格式展示到html页面中。-->
    <div id="goods">
        <button @click="is_show=true;goods_index=-1;">添加商品</button>
        <table>
            <tr>
                <th>商品编号</th>
                <th>商品标题</th>
                <th>商品数量</th>
                <th>商品单价</th>
                <th>商品总价</th>
                <th>操作</th>
            </tr>
            <tr v-for="goods,index in goods_list">
                <td>{{index+1}}</td>
                <td>{{goods.name}}</td>
                <td>
                    <button @click="reduce(index)">-</button>
                    <input type="text" size="2" v-model="goods.num">
                    <button @click="add(index)">+</button>
                </td>
                <td>{{goods.price.toFixed(2)}}</td>
                <td>{{goods.price.toFixed(2)*goods.num}}</td>
                <td>
                    <button @click="update(index)">编辑</button>
                    <button @click="del(index)">删除</button>
                </td>
            </tr>
            <tr>
                <td colspan="5">总计: {{total}}元</td>
            </tr>
        </table>
        <div class="box" v-show="is_show">
            商品标题: <input type="text" v-model="goods_name"><br><br>
            商品数量: <input type="text" v-model="goods_num"><br><br>
            商品价格: <input type="text" v-model="goods_price"><br><br>
            <button @click="save">保存</button>
            <button @click="cancel">取消</button>
        </div>
    </div>
    <script>
        var vm = new Vue({
            el:"#goods",
            data:{
                is_show:false,
                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:{
                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.is_show=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.is_show=true;
                    // 显示当前编辑的商品信息
                    this.goods_index=index;
                    this.goods_name=this.goods_list[index].name;
                    this.goods_num=this.goods_list[index].num;
                    this.goods_price=this.goods_list[index].price;
                    // 当用户点击保存时,修改对应数据
                },
                reduce(index){
                    this.goods_list[index].num--;
                    if(this.goods_list[index].num<=0){this.goods_list.splice(index,1)}
                },
                add(index){
                    this.goods_list[index].num++
                },
            },
            computed:{
                total(){
                    let sum=0;
                    for (let goods of this.goods_list){
                        sum += (goods.num)*(goods.price)
                    }
                    return sum
                }
            }
        })
    </script>
</body>
</html>

2.使用ajax获取北京天气,并把昨天和未来5天天气情况以表格格式展示到html页面中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="axios.js"></script>
</head>
<body>
<!--使用ajax获取北京天气,并把昨天和未来5天天气情况以表格格式展示到html页面中。-->
<!--http://wthrcdn.etouch.cn/weather_mini?city=城市名称-->

<div id="app">
    请输入城市:<input type="text" class="form-control" v-model="city">
    <button @click="get_weather(city)" @click="show=!show">获取天气</button>
    <table class="table table-hover table-stripped">
        <tr>
            <th>日期</th>
            <th>最高温度</th>
            <th>风力</th>
            <th>最低温度</th>
            <th>风向</th>
            <th>天气</th>
        </tr>
<!--        昨天天气!!!!!!!!!!!!!!!!!!!参数顺序都不一样-->
        <tr v-for="item in forecast">
            <th>{{item.date}}</th>
            <th>{{item.high}}</th>
            <th v-if="item.fl">{{item.fl}}</th>
            <th v-else>{{item.fengli}}</th>
            <th>{{item.low}}</th>
            <th v-if="item.fx">{{item.fx}}</th>
            <th v-else>{{item.fengxiang}}</th>
            <th>{{item.type}}</th>
        </tr>
<!--        <tr v-for="inf in forecast">-->
<!--            <th v-for="data in inf">{{data}}</th>-->
<!--        </tr>-->
    </table>
</div>



<script>
    let vm=new Vue({
        el:'#app',
        data:{
            url:'http://wthrcdn.etouch.cn/weather_mini?',
            city:'北京',
            forecast:{'0':'还未查看'},
            yest:{'0':'还未查看'},
            show:false
        },
        methods:{
            get_weather(city){
                axios.get(this.url,{params:{'city':city}})
                    .then(response=> {
                        console.log(response)
                        this.forecast=response.data.data.forecast
                        // this.yest=response.data.data.yesterday
                        this.forecast.unshift(response.data.data.yesterday)
                    })
                    .catch(error=>{
                        console.log(error)
                    })
            }
        }
    })



</script>

</body>
</html>

猜你喜欢

转载自www.cnblogs.com/achai222/p/13160782.html
38