반응형
Vue.js 계산 함수에서 다중 차원 배열 필터링
우선 순위가 3가지 다른 태그 도구 구성요소를 가지고 있다.이제 필터 검색을 하고 싶다.접속이 가능한가?tags[n]
계산에 의하면tagsFilter
기능?
현재 버전: https://jsfiddle.net/hej7L1jy/26/
다음으로 대체하고자 하는 항목:v-for="tag in tagsFilter"
a를 받는 순간
누구 아이디어 있는 사람?
Vue.js 템플릿:
<div id="app">
<input id="input-search" type="text" class="form-inline pull-right" v-model="textSearch" placeholder='Search...'>
<div v-for="n in prios">
<h3>{{n}} tag priority</h3>
<ul v-if="tagsFilter && tagsFilter.length">
<li :class="'tagPriority-'+n" v-for="tag in tagsFilter">
<label class="checkbox-inline"><input name="tags[]" type="checkbox" v-model="tagSelected" :value="tag.id"> {{tag.title}}</label>
</li>
</ul>
</div>
</div>
Vue.js 구성 요소:
new Vue({
el: '#app',
props: {
selectedTags: {
type: Array
}
},
data() {
return {
prios: [ 1, 2, 3 ],
tagSelected: [],
tags: [],
textSearch: ''
}
},
computed: {
tagsFilter: function() {
var textSearch = this.textSearch;
return this.tags.filter(function(el) {
return el.title.toLowerCase().indexOf(textSearch.toLowerCase()) !== -1;
});
}
},
created: function () {
this.tagSelected = this.selectedTags;
this.tags = {"1":[{"id":9,"title":"Auto"}],"2":[{"id":8,"title":"Elektroauto"}],"3":[{"id":10,"title":"BMW i3"},{"id":11,"title":"Opel Ampera"},{"id":12,"title":"Renault TWIZY"}]};
}
});
정의하더라도tags
당신 안에data
:
data() {
return {
tags: [],
}
},
콜백 오버라이드 this.tags
:
created: function () {
this.tagSelected = this.selectedTags;
this.tags = {"1":[{"id":9,"title":"Auto"}],"2":[{"id":8,"title":"Elektroauto"}],"3":[{"id":10,"title":"BMW i3"},{"id":11,"title":"Opel Ampera"},{"id":12,"title":"Renault TWIZY"}]};
}
아무 것도 없는 물체로 바꾸는 것.filter
방법의
계산된 속성을 사용한 필터링
템플릿:
<li :class="'tagPriority-'+n" v-for="tag in tags[0]">
된다
<li :class="'tagPriority-'+n" v-for="tag in tagsFilter[n]">
그리고
computed: {
tagsFilter: function() {
var textSearch = this.textSearch;
return this.tags.filter(function(el) {
return el.title.toLowerCase().indexOf(textSearch.toLowerCase()) !== -1;
});
}
},
다음이 됨:
computed: {
tagsFilter: function() {
var textSearch = this.textSearch;
var filteredTags = {};
var tags = this.tags;
Object.keys(this.tags).forEach(function(key) {
filteredTags[key] = tags[key].filter(function(el) {
return el.title.toLowerCase().indexOf(textSearch.toLowerCase()) !== -1;
});
});
return filteredTags;
},
},
기본적으로 우리는 각자를 반복하고 있다.this.tags
속성 및 필터링된 각 버전 추가filteredTags
.
메소드를 사용한 필터링
다른 방법(변경된 코드의 가장 적은 양)은 당신의 것을 변경하는 것이다.computed
다음과 같은 주장을 가진 방법으로:
<li :class="'tagPriority-'+n" v-for="tag in tags[0]">
된다
<li :class="'tagPriority-'+n" v-for="tag in tagsFilter(n)">
그리고
computed: {
tagsFilter: function() {
var textSearch = this.textSearch;
return this.tags.filter(function(el) {
return el.title.toLowerCase().indexOf(textSearch.toLowerCase()) !== -1;
});
}
},
다음이 됨:
methods: {
tagsFilter: function(i) {
var textSearch = this.textSearch;
return this.tags[i].filter(function(el) {
return el.title.toLowerCase().indexOf(textSearch.toLowerCase()) !== -1;
});
},
},
반응형
'programing' 카테고리의 다른 글
vue 관련 문제 다시 한 번 발생: vue를 2.5에서 2.6.8로 업데이트하고 [Vue warn]:구성 요소를 마운트하지 못함: 템플릿 또는 렌더 함수가 정의되지 않음 (0) | 2022.04.24 |
---|---|
JSON의 끈을 어떻게 벗어나야 할까? (0) | 2022.04.23 |
왜 정적으로 연결된 glibc가 낙담하는가? (0) | 2022.04.23 |
값 쌍의 Java 컬렉션?(튜플이요?) (0) | 2022.04.23 |
Vuejs는 이것을 타이프로 표현한다.$refs..값이 존재하지 않음 (0) | 2022.04.23 |