how2j.cn

步骤 1 : 一个过滤器   
步骤 2 : 多个过滤器   
步骤 3 : 全局过滤器   

步骤 1 :

一个过滤器

定义一个 首字母大写 过滤器

filters:{
capitalize:function(value) {
if (!value) return '' //如果为空,则返回空字符串
value = value.toString()
return value.charAt(0).toUpperCase() + value.substring(1)
}
}

然后在视图里如下方式使用:

{{ data|capitalize }}
"); window.frames["iframe_show7814"].document.write(decodeHtml($("textarea#stepcodeTextarea7814").val())); window.frames["iframe_show7814"].document.close(); $(window.frames["iframe_show7814"]).load(function(){ $("#iframe_show7814").height($("#iframe_show7814").contents().find("body").height()+showittryitheight); }); $("#iframe_show7814").height($("#iframe_show7814").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <style> table tr td{ border:1px solid gray; padding:10px; } table{ border-collapse:collapse; width:800px; table-layout:fixed; } tr.firstLine{ background-color: lightGray; } </style> <div id="div1"> <table align="center" > <tr class="firstLine"> <td>输入数据</td> <td>过滤后的结果</td> </tr> <tr> <td align="center"> <input v-model= "data" /> </td> <td align="center"> {{ data|capitalize }} </td> </tr> </table> </div> <script> new Vue({ el: '#div1', data: { data:'' }, filters:{ capitalize:function(value) { if (!value) return '' //如果为空,则返回空字符串 value = value.toString() return value.charAt(0).toUpperCase() + value.substring(1) } } }) </script>
"); window.frames["iframe7814"].document.write(decodeHtml(code7814)); window.frames["iframe7814"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7814"]).load(function(){ $("#iframe7814").height($("#iframe7814").contents().find("body").height()+showittryitheight); }); $("#iframe7814").height($("#iframe7814").contents().find("body").height()+showittryitheight); alreadyWriteCode7814 = code7814; $("#rendering7814").hide(); $("#rendered7814").show(); } var tRereshRetry2DemoPanel7814 = setInterval(rereshRetry2DemoPanel7814,1000); var binded7814 = false; $("textarea#stepcodeTextarea7814").keyup(function(){ if(!binded7814){ $(window).bind('beforeunload',function(){ binded7814 = true; return "xxxx"; }); } var newCode = $(this).val() code7814 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7814!=newCode){ // window.frames["iframe7814"].document.write("
"); // window.frames["iframe7814"].document.write(decodeHtml($("textarea#stepcodeTextarea7814").val())); // window.frames["iframe7814"].document.close(); // $(window.frames["iframe7814"]).load(function(){ // $("#iframe7814").height($("#iframe7814").contents().find("body").height()+showittryitheight); // }); // code7814 = newCode; // } }); $(".tryButton7814").click(function(){ $("#tryDiv7814").show(); $("#stepcodeTextarea7814").focus(); $("#stepcodeTextarea7814").height(200); $("#iframe7814").height(0); window.frames["iframe7814"].document.write("
"); window.frames["iframe7814"].document.write(decodeHtml($("textarea#stepcodeTextarea7814").val())); window.frames["iframe7814"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7814"]).load(function(){ $("#iframe7814").height($("#iframe7814").contents().find("body").height()+showittryitheight); }); $("#iframe7814").height($("#iframe7814").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7814.focus(); editor7814.setSize(null, "250"); $("#rendering7814").hide(); $("#rendered7814").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7814 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7814"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7814.on("change",function(doc){ if(!binded7814){ $(window).bind('beforeunload',function(){ binded7814 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7814 = newCode; $("textarea#stepcodeTextarea7814").val(newCode); if(alreadyWriteCode7814!=code7814){ lastModifedTime7814 = new Date().getTime(); $("#rendering7814").show(); $("#rendered7814").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7814 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7814.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7814").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

多个过滤器

定义两个过滤器,分别是首字母大写和尾字母大写
然后通过如下方式同时首字母大小写

{{ data|capitalize|capitalizeLastLetter }}
"); window.frames["iframe_show7815"].document.write(decodeHtml($("textarea#stepcodeTextarea7815").val())); window.frames["iframe_show7815"].document.close(); $(window.frames["iframe_show7815"]).load(function(){ $("#iframe_show7815").height($("#iframe_show7815").contents().find("body").height()+showittryitheight); }); $("#iframe_show7815").height($("#iframe_show7815").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <style> table tr td{ border:1px solid gray; padding:10px; } table{ border-collapse:collapse; width:800px; table-layout:fixed; } tr.firstLine{ background-color: lightGray; } </style> <div id="div1"> <table align="center" > <tr class="firstLine"> <td>输入数据</td> <td>过滤后的结果</td> </tr> <tr> <td align="center"> <input v-model= "data" /> </td> <td align="center"> {{ data|capitalize|capitalizeLastLetter }} </td> </tr> </table> </div> <script> new Vue({ el: '#div1', data: { data:'' }, filters:{ capitalize:function(value) { if (!value) return '' //如果为空,则返回空字符串 value = value.toString() return value.charAt(0).toUpperCase() + value.substring(1) }, capitalizeLastLetter:function(value) { if (!value) return '' //如果为空,则返回空字符串 value = value.toString() return value.substring(0,value.length-1)+ value.charAt(value.length-1).toUpperCase() } } }) </script>
"); window.frames["iframe7815"].document.write(decodeHtml(code7815)); window.frames["iframe7815"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7815"]).load(function(){ $("#iframe7815").height($("#iframe7815").contents().find("body").height()+showittryitheight); }); $("#iframe7815").height($("#iframe7815").contents().find("body").height()+showittryitheight); alreadyWriteCode7815 = code7815; $("#rendering7815").hide(); $("#rendered7815").show(); } var tRereshRetry2DemoPanel7815 = setInterval(rereshRetry2DemoPanel7815,1000); var binded7815 = false; $("textarea#stepcodeTextarea7815").keyup(function(){ if(!binded7815){ $(window).bind('beforeunload',function(){ binded7815 = true; return "xxxx"; }); } var newCode = $(this).val() code7815 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7815!=newCode){ // window.frames["iframe7815"].document.write("
"); // window.frames["iframe7815"].document.write(decodeHtml($("textarea#stepcodeTextarea7815").val())); // window.frames["iframe7815"].document.close(); // $(window.frames["iframe7815"]).load(function(){ // $("#iframe7815").height($("#iframe7815").contents().find("body").height()+showittryitheight); // }); // code7815 = newCode; // } }); $(".tryButton7815").click(function(){ $("#tryDiv7815").show(); $("#stepcodeTextarea7815").focus(); $("#stepcodeTextarea7815").height(200); $("#iframe7815").height(0); window.frames["iframe7815"].document.write("
"); window.frames["iframe7815"].document.write(decodeHtml($("textarea#stepcodeTextarea7815").val())); window.frames["iframe7815"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7815"]).load(function(){ $("#iframe7815").height($("#iframe7815").contents().find("body").height()+showittryitheight); }); $("#iframe7815").height($("#iframe7815").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7815.focus(); editor7815.setSize(null, "250"); $("#rendering7815").hide(); $("#rendered7815").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7815 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7815"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7815.on("change",function(doc){ if(!binded7815){ $(window).bind('beforeunload',function(){ binded7815 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7815 = newCode; $("textarea#stepcodeTextarea7815").val(newCode); if(alreadyWriteCode7815!=code7815){ lastModifedTime7815 = new Date().getTime(); $("#rendering7815").show(); $("#rendered7815").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7815 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7815.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7815").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 3 :

全局过滤器

在上面的例子里可以看到,过滤器是定义在Vue对象里的。 但是有时候,很多不同的页面都会用到相同的过滤器,如果每个Vue对象里都重复开发相同的过滤器,不仅开发量增加,维护负担也增加了。
所以就可以通过全局过滤器的方式,只定义一次过滤器,然后就可以在不同的Vue对象里使用了。
注册全局过滤器:

Vue.filter('capitalize', function (value) {
if (!value) return ''
value = value.toString()
return value.charAt(0).toUpperCase() + value.slice(1)
})
Vue.filter('capitalizeLastLetter', function (value) {
if (!value) return '' //如果为空,则返回空字符串
value = value.toString()
return value.substring(0,value.length-1)+ value.charAt(value.length-1).toUpperCase()
})

然后就可以像前面的例子那样使用了
"); window.frames["iframe_show7816"].document.write(decodeHtml($("textarea#stepcodeTextarea7816").val())); window.frames["iframe_show7816"].document.close(); $(window.frames["iframe_show7816"]).load(function(){ $("#iframe_show7816").height($("#iframe_show7816").contents().find("body").height()+showittryitheight); }); $("#iframe_show7816").height($("#iframe_show7816").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <style> table tr td{ border:1px solid gray; padding:10px; } table{ border-collapse:collapse; width:800px; table-layout:fixed; } tr.firstLine{ background-color: lightGray; } </style> <div id="div1"> <table align="center" > <tr class="firstLine"> <td>输入数据</td> <td>过滤后的结果</td> </tr> <tr> <td align="center"> <input v-model= "data" /> </td> <td align="center"> {{ data|capitalize|capitalizeLastLetter }} </td> </tr> </table> </div> <script> Vue.filter('capitalize', function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) }) Vue.filter('capitalizeLastLetter', function (value) { if (!value) return '' //如果为空,则返回空字符串 value = value.toString() return value.substring(0,value.length-1)+ value.charAt(value.length-1).toUpperCase() }) new Vue({ el: '#div1', data: { data:'' } }) </script>
"); window.frames["iframe7816"].document.write(decodeHtml(code7816)); window.frames["iframe7816"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7816"]).load(function(){ $("#iframe7816").height($("#iframe7816").contents().find("body").height()+showittryitheight); }); $("#iframe7816").height($("#iframe7816").contents().find("body").height()+showittryitheight); alreadyWriteCode7816 = code7816; $("#rendering7816").hide(); $("#rendered7816").show(); } var tRereshRetry2DemoPanel7816 = setInterval(rereshRetry2DemoPanel7816,1000); var binded7816 = false; $("textarea#stepcodeTextarea7816").keyup(function(){ if(!binded7816){ $(window).bind('beforeunload',function(){ binded7816 = true; return "xxxx"; }); } var newCode = $(this).val() code7816 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7816!=newCode){ // window.frames["iframe7816"].document.write("
"); // window.frames["iframe7816"].document.write(decodeHtml($("textarea#stepcodeTextarea7816").val())); // window.frames["iframe7816"].document.close(); // $(window.frames["iframe7816"]).load(function(){ // $("#iframe7816").height($("#iframe7816").contents().find("body").height()+showittryitheight); // }); // code7816 = newCode; // } }); $(".tryButton7816").click(function(){ $("#tryDiv7816").show(); $("#stepcodeTextarea7816").focus(); $("#stepcodeTextarea7816").height(200); $("#iframe7816").height(0); window.frames["iframe7816"].document.write("
"); window.frames["iframe7816"].document.write(decodeHtml($("textarea#stepcodeTextarea7816").val())); window.frames["iframe7816"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7816"]).load(function(){ $("#iframe7816").height($("#iframe7816").contents().find("body").height()+showittryitheight); }); $("#iframe7816").height($("#iframe7816").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7816.focus(); editor7816.setSize(null, "250"); $("#rendering7816").hide(); $("#rendered7816").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7816 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7816"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7816.on("change",function(doc){ if(!binded7816){ $(window).bind('beforeunload',function(){ binded7816 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7816 = newCode; $("textarea#stepcodeTextarea7816").val(newCode); if(alreadyWriteCode7816!=code7816){ lastModifedTime7816 = new Date().getTime(); $("#rendering7816").show(); $("#rendered7816").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7816 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7816.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7816").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-Vue.js-过滤器 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图