vue笔记-05(vue-增删改查案例知识点)

2020-05-06  本文已影响0人  7ColorLotus
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01商品管理示例</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
            integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</head>
<body>
    <div id="app">
        <div class="panel panel-default">
            <label>id</label>: <input type="text" v-model="id">
            <label>车型</label>: <input type="text" v-model="type" @keyup.enter="add">
            <input type="button" value="添加" @click="add">

            <input type="text" v-model="searchKey" @keyup.enter="search" v-focus v-color="'blue'">
            <input type="button" value="查询" @click="search">
        </div>

        <table class="table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>车型</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="car in searchResult" :key="car.id">
                    <td>{{ car.id }}</td>
                    <td>{{ car.type }}</td>
                    <td>{{ car.time|chineseDateFilter|fullDateStrFilter }}</td>
                    <td>
                        <a href=";" @click.prevent="delCar(car.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        //全局过滤器,必须写在vm 定义前
        Vue.filter('chineseDateFilter', function(date){
            var y = date.getFullYear();
            var m = date.getMonth() + 1 + '';
            var d = date.getDay() + '';

            return y + '-' + m + '-' + d;
        })

        Vue.directive('focus', {
            inserted: function(el, binding){
                el.focus()
            }
        })

        var vm = new Vue({
            el: '#app',
            data: {
                id: '',
                type: '',
                searchKey: '',
                cars: [
                    {id : '1', type: '宝马', time: new Date()},
                    {id : '2', type: '奔驰', time: new Date()},
                    {id : '3', type: '奔奔', time: new Date()}
                ],
                searchResult: []
            },
            mounted: function(){ //初始加载调用方法
                this.search()
            },
            methods: {
                add(){
                    this.cars.push({id: this.id, type: this.type, time: new Date()})
                    this.searchResult.push({id: this.id, type: this.type, time: new Date()})
                },
                delCar(carId){
                    let delIndex = this.cars.findIndex(item => item.id == carId)
                    if(delIndex != -1){
                        this.cars.splice(delIndex, 1)
                    }
                    console.log(this.cars)
                    this.search()
                },
                search(){
                    this.searchResult = [];
                    var totalCars = this.cars;
                    var _this = this;
                    totalCars.forEach(car => {
                        if(car.type.indexOf(_this.searchKey) > -1){
                            _this.searchResult.push(car);
                        }
                    });
                }
            },
            filters: { //局部过滤器
                fullDateStrFilter(str){
                    dateArray = str.split('-');
                    var y = dateArray[0];
                    var m = dateArray[1];
                    m = m.padStart(2, 0);
                    var d = dateArray[2];
                    d = d.padStart(2, 0);
                    return y + '-' + m + '-' + d;
                }
            },
            directives: {
                'fontweight':{
                    bind: function(el, binding){
                        el.style.fontWeight = binding.value
                    }
                },
                'fontsize':function(el, binding){ //bind和updated的简写
                        el.style.fontSize = binding.value + 'px'
                },
                'color':function(el,binding){
                    el.style.color = binding.value
                }
            }
        })

        
    </script>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读