vue实现添加删除,筛选,还有自定义全局过滤器的功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/vue.min.js"></script>
    <style>
#th th{
    background-color: #50a9fa;
    color: aliceblue;
    font-size: large;
}
    </style>
</head>
<body >
<div id="app" align="center">
    <input type="text" v-model="id">
    <input type="text" v-model="pname">
    <input type="text" placeholder="请输入条件筛选内容" v-model="sname">
    <button @click="addData">添加数据</button>
    <table id="th" border="1"solid width="400px">
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <tr v-show="list.length==0">
            <td colspan="4">当前列表没有任何数据</td>
        </tr>
        <tr v-for="item in list |filterBy sname in 'name' ">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime|datefmt}}</td>
            <td>
                <a href="javascript:void(0)" @click="delData(item.id)">删除</a>
            </td>
        </tr>
    </table>
</div>
</body>
<script>
    //定义全局过滤器
    Vue.filter('datefmt',function (input) {
        var res="";
        var year=input.getFullYear();
        var month=input.getMonth()+1;
        var day=input.getDate();
        res=year+'-'+month+'-'+day;
        return res;//返回我们格式化以后的东西

    });
    new Vue({
        el:'#app',
        data:{
             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}
             ],
            id:0,
            pname:"",
            sname:""//自动获取到用户输入的条件筛选条件值
        },
        methods:{
            addData:function () {
                //包装成list要求的对象格式
                var p={id:this.id,name:this.pname,ctime:new Date()};
                //将p追加到list中
                this.list.push(p);
                //清空页面上的文本框中的数据
                this.id=0;
                this.pname="";

            },
            delData:function (id) {
                //提醒用户是否要删除数据
                if(!confirm('是否要删除数据?'))
                {//当用户点击取消按钮的时候,应该阻断这个方法中的后面的代码的继续执行





                    return ;
                }
                //调用list.findIndex()方法根据传入的id获取到这个要删除的索引值
                var index=this.list.findIndex(function (item) {
                    return item.id==id

                })
                //调用list.splice(元素的索引,元素的个数)
                this.list.splice(index,1);
            }
        }
    });
</script>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值