|
步骤
1
:
一个过滤器
步骤
2
:
多个过滤器
步骤
3
:
全局过滤器
定义一个 首字母大写 过滤器 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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
定义两个过滤器,分别是首字母大写和尾字母大写 然后通过如下方式同时首字母大小写 {{ 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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
在上面的例子里可以看到,过滤器是定义在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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|