• Vue的过滤器(filter)


           在Vue.js中,过滤器主要用于文本的格式化,或者数组数据的过滤与排序等。从Vue.js2.0.0版本开始,内置的过滤器被删除了,如果使用过滤器,需要自己编写。过滤器可以用在两个地方:双花括号插值和v-bind表达式,使用时通过管道符( | )添加到表达式的尾部使用。

            <!-- 在双花括号中 -->

            {{ message | capitalize }}

            <!-- 在 `v-bind` 中 -->

            <div v-bind:id="rawId | formatId"></div>

    过滤器本质是一个函数。Vue中的过滤器分为两种:局部过滤器和全局过滤器

    一、全局过滤器:使用Vue.filter()方法来注册,该方法接收两个参数,第一个参数是过滤器的ID(即名字),第二个参数是一个函数对象,过滤器要实现的功能在这个函数中定义。语法形式如下:

    Vue.filter(id,[definition])

          示例:

    1. <div id="app">
    2.     <input type="text" v-model="message">
    3.     {{ message | capitalize }}
    4. </div>
    5. <script>
    6.      Vue.filter('capitalize'function (value) {
    7.           if (!value) return ''
    8.           value = value.toString()
    9.           return value.toUpperCase();
    10.      })
    11.      new Vue({
    12.          el:'#app',
    13.          data:{
    14.            message:'java'
    15.          }
    16.      })
    17. </script>

          过滤器函数总接收表达式的值 (之前的操作链的结果) 作为第一个参数。在上述例子中,capitalize 过滤器函数将会收到 message 的值作为第一个参数。

    二、局部过滤器:是在Vue实例的选项对象中使用filters选项来注册

           例如:

    1. <div id="app">
    2.      <input type="text" v-model="message">
    3.      {{ message | capitalize }}
    4. </div>
    5. <script>
    6. new Vue({
    7.        el:'#app',
    8.        data:{
    9.            message:'java'
    10.        },
    11.        filters:{
    12.            capitalize:function (value) {
    13.               if (!value) return ''
    14.               value = value.toString()
    15.               return value.toUpperCase();
    16.             }
    17.          }
    18.     })
    19. </script>

    三、过滤器的串联:

            1、{{ message | filterA | filterB }}

                    filterA 被定义为接收单个参数的过滤器函数,表达式 message 的值将作为参数传入到函数中。然后继续调用同样被定义为接收单个参数的过滤器函数 filterB,将 filterA 的结果传递到 filterB 中。

            2、过滤器是 JavaScript 函数,因此可以接收参数:

                  {{ message | filterA('arg1', arg2) }}

                    这里,filterA 被定义为接收三个参数的过滤器函数。其中 message 的值作为第一个参数,普通字符串 'arg1' 作为第二个参数,表达式 arg2 的值作为第三个参数。

    四、综合示例

          

    1. <style>
    2.     div{
    3.         width: 850px;
    4.         margin: 50px auto;
    5.     }
    6. </style>
    7. <div id="app">
    8.        <h2 style="text-align: center;">过滤器应用举例</h2>
    9.        <div>
    10.         <label>
    11.             ID:
    12.             <input type="text" placeholder="请输入编号" v-model="id" v-bind:disabled="id_flag">
    13.         </label>
    14.         <label>
    15.             Name:
    16.             <input type="text" placeholder="请输入名称" v-model="name">
    17.         </label>
    18.         <label>
    19.             Delivery Time:
    20.             <input  type="datetime-local" v-model="ctime"/>
    21.         </label>
    22.         <button @click="add" v-bind:disabled="add_flag">Add</button>
    23.         <button v-bind:disabled="edit_flag" @click="edit_option">Edit</button>
    24.         <br><br>
    25.         <label>
    26.             请输入搜索关键字:
    27.             <input type="text" placeholder="请输入搜索关键字" v-model="keyword">
    28.         </label>
    29.         <button @click="search(keyword)">Search</button>
    30.        </div>
    31.        <table border="1">
    32.            <thead>
    33.                <tr>
    34.                    <th width="100">编号</th>
    35.                    <th width="200">名称</th>
    36.                    <th width="300">出厂时间</th>
    37.                    <th width="200">操作</th>
    38.                </tr>
    39.            </thead>
    40.            <tbody>
    41.                <tr  v-for="(item,index) in search(keyword)" :key="item.id" align="center">
    42.                    <td>{{ item.id }}</td>
    43.                    <td>{{ item.name }}</td>
    44.                    <td>{{ item.ctime | dateFormat }}</td>
    45.                    <td>
    46.                        <a href="#" @click.prevent="edit(item)">{{ item.options[0] }}</a>
    47.                        &nbsp;&nbsp;&nbsp;
    48.                        <a href="#" @click.prevent="del(index)">{{ item.options[1] }}</a>
    49.                    </td>
    50.                </tr>
    51.            </tbody>
    52.        </table>
    53.    </div>
    54.    <script>
    55.        new Vue({
    56.            el: '#app',
    57.            data: {
    58.                id: '',
    59.                name: '',
    60.                ctime: new Date(),
    61.                keyword: '',
    62.                id_flag: false, //id框可用
    63.                add_flag: false,//add按钮可用
    64.                edit_flag: true, //edit按钮不可用
    65.                list: [
    66.                    { id:1001,name:'宝马520',ctime:new Date(),options:['Edit','Delete'] },
    67.                    { id:1002,name:'宝马730',ctime:new Date(),options:['Edit','Delete'] },
    68.                    { id:1003,name:'宝马X5',ctime:new Date(),options:['Edit','Delete'] },
    69.                    { id:1004,name:'奔驰s350',ctime:new Date(),options:['Edit','Delete'] },
    70.                    { id:1005,name:'奔驰s450',ctime:new Date(),options:['Edit','Delete'] },
    71.                    { id:1006,name:'红旗L5',ctime:new Date(),options:['Edit','Delete'] },
    72.                    { id:1007,name:'红旗L7',ctime:new Date(),options:['Edit','Delete'] },
    73.                    { id:1008,name:'红旗X90',ctime:new Date(),options:['Edit','Delete'] }
    74.                ]
    75.            },
    76.            filters: { //局部过滤器
    77.             dateFormat: function(d){
    78.                 let date = new Date(d);
    79.                 let dy = date.getFullYear();
    80.                 let dm = date.getMonth()+1;
    81.                 dm = dm>=10?dm:'0'+dm;
    82.                 let dd = date.getDate();
    83.                 dd = dd>=10?dd:'0'+dd
    84.                 let h = date.getHours();
    85.                 h = h>=10?h:'0'+h
    86.                 let m = date.getMinutes();
    87.                 m = m>=10?m:'0'+m
    88.                 let s = date.getSeconds();
    89.                 s = s>=10?s:'0'+s
    90.                 return `${dy}-${dm}-${dd}T${h}:${m}:${s}`
    91.             }
    92.            },
    93.            methods:{
    94.                add(){
    95.                    let temp_id = this.id;
    96.                    let temp_name = this.name;
    97.                    let temp_date = this.ctime;
    98.                    this.list.push({
    99.                        id: temp_id,
    100.                        name: temp_name,
    101.                        ctime: temp_date,
    102.                        options:['Edit','Delete']
    103.                    });
    104.                    this.id = '';
    105.                    this.name = '';
    106.                    this.ctime = '';
    107.                },
    108.                edit(item){
    109.                    this.add_flag = true; //add按钮不可用
    110.                    this.edit_flag = false; //edit按钮可用
    111.                    this.id = item.id;
    112.                    this.name = item.name
    113.                    this.ctime = this.$options.filters.dateFormat(item.ctime);
    114.                    this.id_flag = true //id框不可用
    115.                },
    116.                edit_option(){
    117.                   let arr = this.list.filter(temp=>{ //查找需要编辑的元素
    118.                       return temp.id === this.id
    119.                   })
    120.                   arr[0].name = this.name
    121.                   arr[0].ctime = this.ctime
    122.                   this.list = this.list.map(item=>{ //替换元素
    123.                         return item.id === arr[0].id?arr[0]:item
    124.                   })
    125.                   this.id_flag = false //id框可用
    126.                   this.add_flag = false //add按钮可用
    127.                   this.edit_flag = true //edit按钮不可用
    128.                   this.id=''
    129.                   this.name=''
    130.                   this.ctime = ''
    131.                },
    132.                del(index){
    133.                    this.list.splice(index,1)
    134.                },
    135.                search(name){
    136.                       var arr=[];
    137.                       this.list.forEach((item,i)=>{
    138.                         if(item.name.indexOf(name)!=-1){
    139.                           arr.push(item);
    140.                         }
    141.                       });
    142.                       return arr
    143.                }
    144.            }
    145.        })
    146.    </script>

     

  • 相关阅读:
    Aword无题
    MATLAB算法实战应用案例精讲-【回归算法】XGBoost算法(附Java、Python和R语言代码)
    企业现在开始准备应对2024技术趋势了
    如何设计一个网络爬虫?
    Vue----计算属性
    2022 最新 互联网 Java 工程师面试题
    英特尔:如何从“小芯片”布局到通用量子计算机
    .NET 8 Release Candidate 1 (RC1)现已发布,包括许多针对ASP.NET Core的重要改进!
    Makefile(make)之(3)输出变量值
    ant design vue 表单出错,滚动到对应项
  • 原文地址:https://blog.csdn.net/m0_37911706/article/details/125506588