vue学习日记一

添加列表及删除列表:

 效果图:

 

 html及js代码:

<div id="app">
    <p>姓名:<input type="text" v-model="name" placeholder="请输入姓名"></p>
    <p>年龄:<input type="text" v-model='age' placeholder="请输入年龄"></p>
    <p><input type="button" value="添加" @click="addList"></p>
    <table>
        <thead>
            <tr>
                <th><input type="checkbox" v-model="checked" @click="checkedAll"></th>
                <th>编号</th>
                <th>姓名</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody>
            <template v-if="items.length!=0">
                <tr v-for="(item,index) in items">
                    <td><input type="checkbox" v-model="checkedIds" :value="index"></td>
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.age}}</td>
                </tr>
            </template>
            <template v-else>
                <tr>
                    <td colspan="4">暂无数据!</td>
                </tr>
            </template>
        </tbody>
    </table>
    <p><input type="button" value="删除" @click="deleteList"></p>
    <p>{{tip}}</p>
</div>
var vue = new Vue({
        el:'#app',
        data:{
            checked:false,
            name:'',
            age:'',
            items:[],
            checkedIds:[],
            tip:''
        },
        methods:{
            addList:function(){
                this.items.push({name:this.name,age:this.age});
                this.reset();
            },
            checkedAll:function(){
                this.checked = !this.checked;
                if(this.checked){
                    for(var i = 0; i < this.items.length; i++){
                        this.checkedIds.push(i);
                    }
                }else{
                    this.checkedIds = [];
                }
            },
            deleteList:function(){
                var ids = this.checkedIds;
                if(ids.length == 0){
                    this.tip = '请至少选择一项!'
                }else{
                    for(var i = ids.length-1; i >= 0; i--){
                        this.items.splice(ids[i],1)
                    }
                    this.tip = '删除成功!'
                    this.reset();
                }
            },
            reset:function(){
                this.name = '';
                this.age = '';
                this.checkedIds = [];
                this.checked = false;
            }
        }
    });

vue-resource跨域请求——百度搜索提示:

 效果图:

 

 html及js代码: 

<div id="app">
    <input type="text" placeholder="请输入搜索内容" v-model="value" @keyup="getSearchResult">
    <ul v-if="items.length!=0">
        <li v-for="item in items">{{item}}</li>
    </ul>
    <ul v-else>
        <li>暂无数据</li>
    </ul>
</div> 
var vue = new Vue({
        el:'#app',
        data:{
            value:'',
            items:[]
        },
        methods:{
            getSearchResult:function(){
                var that = this;
                this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?',{
                    params:{
                        wd:this.value
                    },
                    jsonp:'cb'
                }).then(function(res){
                    that.items = JSON.parse(res.bodyText).s;
                },function(error){

                })
            }
        }
    });

 

posted @ 2017-12-29 13:39  白色斑马线  阅读(449)  评论(0编辑  收藏  举报