14.Vue.js 小Demo

<!DOCTYPE html>
<html lang="en">

<head>
    <title>列表展示删除添加</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
    <style>
        #app {
            width: 600px;
            margin: 20px auto;
        }
        
        .dv1,
        .dv2 {
            border: 1px solid black;
            margin-bottom: 10px;
            padding: 5px;
        }
        
        table {
            width: 600px;
            border-right: 1px solid black;
            border-top: 1px solid black;
        }
        
        .tb th {
            width: 600px;
            background-color: blue;
            color: white;
            border-bottom: 1px solid #000;
        }
        
        .tb th,
        .tb td {
            border-left: 1px solid black;
            border-bottom: 1px solid #000;
            text-align: center;
            padding: 5px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="dv1">
            编号:
            <input type="text" v-model="newId" id='inputId' ref='inputRefId' v-myfocus v-mycolor='color'> 品牌名称:
            <input type="text" v-model="newName">
            <input type="button" value="添加" @click='addData'>
        </div>
        <div class="dv2">
            品牌名称:
            <input type="text" placeholder="请输入搜索条件" v-model='searchVal'>

        </div>
        <div class="tb">
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <th>编号</th>
                    <th>品牌名称</th>
                    <th>创立时间</th>
                    <th>操作</th>
                </tr>
                <tr v-for='(item,index) in newList' :key='index'>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <!-- 在需要过滤的数据后面加上管道符 |过滤器名称 -->
                    <td>{{item.ctime|fmtTime('/')}}</td>
                    <td><a href="#" @click='deleteData(index)'>删除</a></td>
                </tr>
                <tr v-if='list.length===0'>
                    <td colspan='4'>没有品牌数据</td>
                </tr>
            </table>
        </div>
    </div>
    <script>
        //通过Vue.filter()方法创建过滤器,它有两个参数,第一个参数是过滤器的名字,第二个参数是过滤器的处理函数,
        //这个处理函数有个默认的参数,表示需要过滤的数据

        //创建一个时间过滤器fmtTime
        Vue.filter('fmtTime', function(time, seprator) {
            var y = time.getFullYear();
            var m = time.getMonth() + 1;
            var d = time.getDate();
            return y + seprator + m + seprator + d

        })

        //这里实际上创建的是v-myfocus指令
        Vue.directive('myfocus', {
                //inserted钩子函数,它表示自定义指令插入到标签中的时候就执行
                //inserter钩子函数有两个参数(el,bingding) el表示使用自定义指令的元素,binding表示自定义指令的信息
                inserted(el, binding) {
                    el.focus();
                }
            })
            //创建是的v-mycolor='color'指令
        Vue.directive('mycolor', {
            inserted(el, binding) {
                //binding.value可以获取传入自定义指令中的属性的值
                el.style.color = binding.value
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
                newId: '',
                newName: '',
                color: 'red',
                searchVal: '',
                list: [{
                    id: 1,
                    name: '宝马',
                    ctime: new Date()
                }, {
                    id: 2,
                    name: '奔驰',
                    ctime: new Date()
                }, {
                    id: 3,
                    name: '大众',
                    ctime: new Date()
                }, {
                    id: 4,
                    name: '海马',
                    ctime: new Date()
                }, ]
            },
            // mounted() {
            // document.getElementById('inputId').focus()原生js方式实现的
            //通过this.$refs.ref的值--来获取dom
            // this.$refs.inputRefId.focus()
            // },
            methods: {
                deleteData(idx) {
                    this.list.splice(idx, 1)
                },
                addData() {
                    this.list.push({
                        id: this.newId,
                        name: this.newName,
                        ctime: new Date()
                    })
                }

            },
            computed: {
                newList() {

                    return this.list.filter(value => value.name.indexOf(this.searchVal) !== -1)
                }
            }
        })
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VIVI读书吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值