2019-01-16品牌案例Vue

2019-01-16  本文已影响0人  浮图塔断了几层

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Document</title>

  <script src="./lib/vue-2.4.0.js"></script>

  <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">

</head>

<body>

  <div id="app">

    <div class="panel panel-primary">

        <div class="panel-heading">

          <h3 class="panel-title">添加品牌</h3>

        </div>

        <div class="panel-body form-inline">

          <label>

            Id:

            <input type="text" class="form-control" v-model="id">

          </label>

          <label>

            Name:

            <input type="text" class="form-control" v-model="name">

          </label>

              <input type="button" value="添加" class="btn btn-primary" @click="add()">

          <label>

            搜索名称关键字:

            <input type="text" class="form-control"  v-model="keywords" >

          </label>

        </div>

      </div>

    <table class="table table-bordered table-hover table-striped">

        <thead>

            <tr>

                <th>id</th>

                <th>name</th>

                <th>ctime</th>

                <th>operation</th>

            </tr>

        </thead>

    <tbody>

        <tr v-for="item in search(keywords)" :key="item.id" >

            <td>{{item.id}}</td>

            <td>{{item.name}}</td>

            <td>{{item.ctime|forMate}}</td>

            <td ><a href="" @click.prevent="del(id)">删除</a></td>

        </tr>

    </tbody>

        </table>

  </div>

  <script>

Vue.filter('forMate',function(ctime){

          var dt=new Date(ctime)

          var  y=dt.getFullYear()

          var  m=dt.getMonth()+1

          var  d=dt.getDate()

          return y+'-'+m+'-'+d

      })

    var vm = new Vue({

      el: '#app',

        data: {

            id:'',

            name:'',

            keywords:'',

            list:[

                {id:1,name:'阿迪达斯',ctime:new Date()},

                {id:2,name:'vue好难啊',ctime:new Date()},

                {id:3,name:'阿迪达斯',ctime:new Date()},

                {id:4,name:'阿迪达斯',ctime:new Date()}

            ]

        },

        methods:{

            add(){

                var car={id:this.id,name:this.name,ctime:new Date()}

                this.list.push(car)

                this.id=this.name='';

            },

            del(id){

                var index=this.list.findIndex(item=>{

                    if(item.id=id){

                        return true;

                    }

                })

                this.list.splice(index,1)

            },

          search(keywords){

                var newList=this.list.filter(item=>{

                    if(item.name.includes(keywords)){

                        return item

                    }

                })

                return newList;

            }

        }

        });

  </script>

</body>

</html>

上一篇 下一篇

猜你喜欢

热点阅读