javascript - How do I improve my Vue.js code style and Javascript skills?
问题描述
I'm creating dynamic search for my page with Vue.js. I'm not very good with Javascript but I managed to get code below working :)
Is this style good and should I use more anonymous functions? Especially the filter() -function is very weird for me... parameters and returning anonymous functions kind of... Could someone check this code and give me some good learning resources how could I deeply understand this code and learn these things. I know if I would add anonymous function inside my filter return it would not work because of the variable scope.
But this works now :)
EDIT: Actually the v-if is not needed anymore because of the filtering methdod!
<div id="app" class="container">
Search: <input type="text" v-model="filter_search"/>
<div class="col-lg-8">
<div v-for="dep in departments">
<h2>{{dep}}</h2>
<div v-for="person in filtered_persons(dep)">
<template v-if="person.dep == dep">
<p>{{ person.fname }}</p>
</template>
</div>
</div>
</div>
</div>
var person_array = [
{
'fname': 'Jaakko',
'lname': 'Möttönen',
'title': 'Johtaja',
'dep': 'Hallinto'
},
{
'fname': 'Matti',
'lname': 'Ääninen',
'title': 'Projektipäällikkö',
'dep': 'Projektijohto'
},
];
var department_array = [
'Hallinto',
'Projektijohto'
];
new Vue({
el: '#app',
data: {
filter_search: null,
persons: person_array,
departments: department_array
},
methods: {
filtered_persons(dep) {
if (this.filter_search) {
return this.persons.filter(item => {
return (
(item.fname.toLowerCase().indexOf(this.filter_search.toLowerCase()) > -1)
&& (dep == item.dep)
);
});
} else {
return this.persons.filter(item => {
return (
(dep == item.dep)
);
});
}
}
},
});
解决方案
For filtering computed
properties are good. As soon as you change this.persons
or this.filter_search
the method filtered_persons
will run
var person_array = [
{
fname: "Jaakko",
lname: "Möttönen",
title: "Johtaja",
dep: "Hallinto"
},
{
fname: "Matti",
lname: "Ääninen",
title: "Projektipäällikkö",
dep: "Projektijohto"
}
];
let deps = person_array.reduce((a, v) => {
let i = a.findIndex(dep => dep.name == v.dep);
if (i !== -1) {
a[i].persons.push(v);
return a;
}
a.push({ name: v.dep, persons: [v] });
return a;
}, []);
new Vue({
el: "#app",
data: {
filter_search: "",
departments: deps
},
computed: {
filtered_deps() {
return this.departments.filter(dep =>
dep.persons.some(person =>
person.fname.toLowerCase().includes(this.filter_search.toLowerCase())
)
);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app" class="container">
Search: <input type="text" v-model="filter_search" />
<div class="col-lg-8">
<div v-for="dep in filtered_deps">
<h2>{{ dep.name }}</h2>
<div v-for="person in dep.persons">
<p>{{ person.fname }}</p>
</div>
</div>
</div>
</div>
As you can see i use an arrow function there without curly brackets {}
That means that the value / expression on the right side of the arrow =>
will be returnend and so i dont need to use return
推荐阅读
- jenkins - Jenkins:不允许脚本使用方法 org.thoughtslive.jenkins.plugins.jira.api.ResponseData getData
- android - 为什么我得到 - 无法解决 com.squareup.retrofit2: retrofit:2.3.0
- system.io.file - LiteDB 是否支持连接字符串中的长文件名(>=260 字符)
- ios - 如何编辑当前视图控制器导航栏上的后退按钮(不是左按钮)?
- asp.net-core - 创建 dockerfile - 使用 .NET 框架作为运行时的 ASP.NET Core
- ios - iOS 音量仅在播放时可调
- oauth-2.0 - 在 pythonanywhere 上安装后出现 OAuth 错误
- c++ - Valgrind 检测 glewInit() 和 glfwInit() 内存泄漏
- java - 如何获取 servlet 的路径以包含在 .jsp 文件中?
- neo4j - Neo4j 密码查询 - 如何获取特定标签数据?