01_22.格式化商品创建时间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .wrapper {
            width: 800px;
            margin: 20px auto;
        }
        .operation {
            margin-bottom: 10px;
            text-align: center;
            line-height: 20px;
            font-size: 18px;
        }
        .operation input {
            padding: 5px;
            border: 1px solid deepskyblue;
        }
        .operation button {
            border-radius: 3px;
            background-color: deepskyblue;
        }
        .search {
            text-align: left;
            line-height: 20px;
            font-size: 18px;
        }
        .search input {
            padding: 5px;
            border: 1px solid deeppink;
        }
        #tb{
            width: 800px;
            border-collapse: collapse;
            margin: 20px auto;
        }
        #tb th{
            background-color: #0094ff;
            color:white;
            font-size: 16px;
            padding: 5px;
            text-align: center;
            border: 1px solid black;
        }
        #tb td{
            padding: 5px;
            text-align: center;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <div id="app" class="wrapper">
        <div class="operation">
            <input type="text" placeholder="请输入编号" v-model="newGoods.id">
            <input type="text" placeholder="请输入名称" v-model="newGoods.name">
            <button type="button" v-on:click="addGoods">添加数据</button>
        </div>
        <!-- <div class="search">
            <input type="text" placeHolder="请输入筛选内容">
        </div> -->
        <table id="tb">
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            <!-- 没有数据才显示, 有数据隐藏 -->
            <tr v-if="goodsList.length == 0">
                <td colspan="4">列表无数据</td>
            </tr>
            <!-- 渲染商品列表 -->
            <tr v-for="item in goodsList">
                <td>{{ item.id }}</td>
                <td>{{ item.name }}</td>
                <td>{{ item.ctime | date }}</td>
                <td>
                    <!-- @符号是v-on的简写方式 -->
                    <a href="#" @click="deleteGoods(item.id)">删除</a>
                </td>
            </tr>
        </table>
    </div>

    <script src="vue.js"></script>
    <script>
        // 实现一个处理日期的过滤器
        Vue.filter('date', function(time) {
            var date = new Date(time);
            return `${ date.getFullYear() }-${ date.getMonth() + 1 }-${ date.getDate() }`;
        });


        new Vue({
            el: '#app',
            data: {
                // 商品列表
                goodsList: [
                    { id: 1, name: '法拉利', ctime: new Date() },
                    { id: 2, name: '玛莎拉蒂', ctime: new Date() },
                    { id: 3, name: '兰博基尼', ctime: new Date() }
                ],
                // 新商品数据
                newGoods: {
                    id: '',
                    name: ''
                }
            },
            methods: {

                // 点击添加按钮, 拿到两个表单的值, 组成一个商品对象push到商品列表中, 视图会自动更新
                addGoods() {

                    // 添加商品
                    this.goodsList.push({
                        id: this.newGoods.id,
                        name: this.newGoods.name,
                        ctime: new Date()
                    });

                    // 清空表单数据
                    this.newGoods.id = '';
                    this.newGoods.name = '';
                },

                // 删除商品, 需要拿到要删除商品的id
                deleteGoods(id) {

                    // 方式一: 遍历商品列表依次与id比较, 找到要删除的商品删除
                    // for(var i = 0; i < this.goodsList.length; i++) {
                    //  if(this.goodsList[i].id == id) {
                    //      this.goodsList.splice(i, 1);
                    //  }
                    // }

                    // 方式二: 使用filter方法实现
                    // this.goodsList = this.goodsList.filter(function(goods, i) {
                    //  return goods.id != id;  // 不删除的商品留下
                    // });

                    this.goodsList = this.goodsList.filter(goods => goods.id != id);
                }
            }
        });
    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容