vue--filters (private and public)

1. Basic use of filters

1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>Document</title>
 8 </head>
 9 <body>
10      <div id="app">
11 {{Date()|dateFrm}}
12      </div>
13 </body>
14 <script src="vue2.4.4.js"></script>
15 <script>
16 new View({
17         el:"#app",
18         data:{},
19         methods:{},
20         mounted:{},
21         filters:{
22 // The first parameter of the method in the filter The first parameter is: the object data of the current call filter
23             dateFrm:function(time){
24                 // return "2017-11-16";
25                 console.log(time);
26                 
27             }
28         }
29     });
30 </script>
31 </html>

2. Examples of private filter usage

  1 <!DOCTYPE html>
  2 <html lang="en">
  3
  4 <head>
  5     <meta charset="UTF-8">
  6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8     <title>Document</title>
  9     <style>
 10         #app {
 11             width: 600px;
 12             margin: 10px auto;
 13         }
 14
 15         .tb {
 16             border-collapse: collapse;
 17             width: 100%;
 18         }
 19
 20         .tb th {
 21             background-color: #0094ff;
 22             color: white;
 23         }
 24
 25 .tb td,
 26         .tb th {
 27             padding: 5px;
 28             border: 1px solid black;
 29             text-align: center;
 30         }
 31
 32         .add {
 33             padding: 5px;
 34             border: 1px solid black;
 35             margin-bottom: 10px;
 36         }
 37     </style>
 38 </head>
 39
 40 <body>
 41     <div id="app">
 42         <div class="add">
 43             编号: <input id="id" v-color="color" v-focus="focus" type="text" v-model="id">品牌名称: <input v-model="name" type="text">
 44             <button @click="add">添加</button>
 45         </div>
 46         <div class="add">品牌名称:<input type="text"></div>
 47         <div>
 48             <table class="tb">
 49                 <tr>
 50 <th>number</th>
 51 <th>Brand Name</th>
 52 <th>Founded</th>
 53 <th>Operation</th>
 54                 </tr>
 55                 <tr v-if="list.length <= 0">
 56 <td colspan="4">No brand data</td>
 57                 </tr>
 58 <!-- When adding: key="index", all parameters must be written completely -->
 59                 <tr v-for="(item,key,index) in list" :key="index">
 60                     <td>{{item.id}}</td>
 61                     <td>{{item.name}}</td>
 62                     <td>{{item.ctime | dateFrm("/") |addNoon}}</td>
 63 <!-- When using vue to register events, we can't see it in the dom element -->
 64                     <td><a href="javascript:void(0)" @click="del(item.id)">删除</a></td>
 65                 </tr>
 66             </table>
 67         </div>
 68
 69     </div>
 70 </body>
 71
 72 </html>
 73 <script src="vue2.4.4.js"></script>
 74 <script>
 75 // First define the custom instruction
 76 // directive has two parameters
 77 //Parameter 1: Custom instruction v-focus
 78 //Parameter two: object, many methods can be added to the object
 79 // Add an inserted method: and this method has two parameters:
 80 //Parameter 1: el The object currently using the custom command
 81 //Parameter two: obj refers to the expression set after it (v-color="color" )
 82     //{expression:"color",value:"red",}
 83     Vue.directive("focus", {
 84         inserted: function (el, obj) {
 85             // console.log(el);
 86 el.focus ();
 87         }
 88     });
 89     Vue.directive("color", {
 90         inserted: function (el, obj) {
 91             // obj.style.color = "red";
 92             obj.style.color = obj.value;//????????????
 93             console.log(obj.value);
 94         }
 95     });
 96
 97 var vm = new Vue({
 98         el: "#app",
 99         data: {
100             color: "green",
101             id: 0,
102             name: '',
103             list: [
104                 { "id": 1, "name": "itcast", "ctime": Date() },
105                 { "id": 2, "name": "黑马", "ctime": Date() }
106             ]
107         },
108         // mounted(){
109         //     this.getFocus()
110         // },
111         methods: {
112             add: function () {
113 //Push id and name to the list array
114                 this.list.push({ "id": this.id, "name": this.name, "ctime": Date() });
115             },
116             del: function (id) {
117
118 // Get the subscript according to the id
119 // By default, traverse the list collection and pass each element in the collection into the function's item,
120                 var index = this.list.findIndex(function (item) {
121 //According to the id attribute in the item to determine whether the item is in the above id
122 //Corresponding data, if it returns a true, if it returns false, continue to traverse the following data, and so on
123 return item.id == id; //If it returns true, the findIndex method will return the id corresponding to this item to the outside for acceptance
124                 });
125 // Delete the corresponding data in the list set according to the subscript
126 // splice (start to delete subscript, delete length)               
127                 this.list.splice(index, 1);
128             }
129
130         },
131         filters: {
132 // Private filter: the first parameter of the method in the filter The first parameter is: the object data of the current call filter
133             dateFrm: function (time,spliceStr) {
134                 // return "2017-11-16";
135                 var date = new Date(time);
136 // get year
137                 var year = date.getFullYear();
138 // get month
139                 var month = date.getMonth() + 1;
140 // get day
141                 var day = date.getDate();
142                 return year+spliceStr+month+spliceStr+day;
143             },
144             addNoon:function(time) {
145 return time+"afternoon";
146             }
147         }
148     });
149
150 </script>

 

 

2. Global/public filters

  1 <!DOCTYPE html>
  2 <html lang="en">
  3
  4 <head>
  5     <meta charset="UTF-8">
  6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8     <title>Document</title>
  9     <style>
 10         #app {
 11             width: 600px;
 12             margin: 10px auto;
 13         }
 14
 15         .tb {
 16             border-collapse: collapse;
 17             width: 100%;
 18         }
 19
 20         .tb th {
 21             background-color: #0094ff;
 22             color: white;
 23         }
 24
 25 .tb td,
 26         .tb th {
 27             padding: 5px;
 28             border: 1px solid black;
 29             text-align: center;
 30         }
 31
 32         .add {
 33             padding: 5px;
 34             border: 1px solid black;
 35             margin-bottom: 10px;
 36         }
 37     </style>
 38 </head>
 39
 40 <body>
 41     <div id="app">
 42         <div class="add">
 43             编号: <input id="id" v-color="color" v-focus="focus" type="text" v-model="id">品牌名称: <input v-model="name" type="text">
 44             <button @click="add">添加</button>
 45         </div>
 46         <div class="add">品牌名称:<input type="text"></div>
 47         <div>
 48             <table class="tb">
 49                 <tr>
 50 <th>number</th>
 51 <th>Brand Name</th>
 52 <th>Founded</th>
 53 <th>Operation</th>
 54                 </tr>
 55                 <tr v-if="list.length <= 0">
 56 <td colspan="4">No brand data</td>
 57                 </tr>
 58 <!-- When adding: key="index", all parameters must be written completely -->
 59                 <tr v-for="(item,key,index) in list" :key="index">
 60                     <td>{{item.id}}</td>
 61                     <td>{{item.name}}</td>
 62                     <td>{{item.ctime | dateFrm("/") |addNoon}}</td>
 63 <!-- When using vue to register events, we can't see it in the dom element -->
 64                     <td><a href="javascript:void(0)" @click="del(item.id)">删除</a></td>
 65                 </tr>
 66             </table>
 67         </div>
 68
 69     </div>
 70 </body>
 71
 72 </html>
 73 <script src="vue2.4.4.js"></script>
 74 <script>
 75 // Use global filter (public filter)
76     Vue.filter("dateFrm", function (time,spliceStr) {
 77         // return "2017-11-16";
 78         var date = new Date(time);
 79 // get the year
 80         var year = date.getFullYear();
 81 // get month
 82         var month = date.getMonth() + 1;
 83 // get day
 84         var day = date.getDate();
 85         return year + spliceStr + month + spliceStr + day;
 86     });
87
 88
 89 // First define the custom instruction
 90 // directive has two parameters
 91 //Parameter 1: Custom instruction v-focus
 92 //Parameter two: object, many methods can be added to the object
 93 // Add an inserted method: and this method has two parameters:
 94 //Parameter 1: el The object currently using the custom command
 95 //Parameter two: obj refers to the expression set after it (v-color="color" )
 96     //{expression:"color",value:"red",}
 97     Vue.directive("focus", {
 98         inserted: function (el, obj) {
 99             // console.log(el);
100 el.focus ();
101         }
102     });
103     Vue.directive("color", {
104         inserted: function (el, obj) {
105             // obj.style.color = "red";
106             obj.style.color = obj.value;//????????????
107             console.log(obj.value);
108         }
109     });
110
111 var vm = new Vue({
112         el: "#app",
113         data: {
114             color: "green",
115             id: 0,
116             name: '',
117             list: [
118                 { "id": 1, "name": "itcast", "ctime": Date() },
119                 { "id": 2, "name": "黑马", "ctime": Date() }
120             ]
121         },
122         // mounted(){
123         //     this.getFocus()
124         // },
125         methods: {
126             add: function () {
127 //Push id and name to the list array
128                 this.list.push({ "id": this.id, "name": this.name, "ctime": Date() });
129             },
130             del: function (id) {
131
132 // Get the subscript according to the id
133 // By default, traverse the list collection and pass each element in the collection into the function's item,
134                 var index = this.list.findIndex(function (item) {
135 //According to the id attribute in the item to determine whether the item is in the above id
136 //Corresponding data, if it returns a true, if not, return false, continue the traversal of the following data, and so on
137 return item.id == id; //If it returns true, the findIndex method will return the id corresponding to this item to the outside for acceptance
138                 });
139 // Delete the corresponding data in the list set according to the subscript
140 // splice (start to delete subscript, delete length)               
141                 this.list.splice(index, 1);
142             }
143
144         }
145     });
146
147 </script>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324951081&siteId=291194637