반응형
VueJ를 사용한 체크박스 필터링s
이것은 저의 첫 번째 VueJS 프로젝트입니다(jQuery에서 이행).여러 개의 체크박스로 그리드를 필터링하려고 합니다.JavaScript에서 볼 수 있듯이 선택한 항목(v-model="체크된 위치")에서 설정된 값으로 어레이를 필터링하는 filterJobs 함수가 있습니다.array.includes는 하나의 값을 기준으로 필터링하는 것으로 나타나며 여러 값을 필터링하고 싶습니다.어레이를 팝업 또는 슬라이스하고 싶지 않습니다.어레이 위치를 선택 해제하면 삭제되어 재바인딩되지 않기 때문입니다.
let careers = careers || {};
careers.taleo = (function($){
let app;
let init = function() {
app = new Vue({
el: '#app',
data: {
locations: ['Scottsdale, AZ','Chandler, AZ','Irvine, CA','Denver, CO','Chicago, IL','Rockville, MD','Kansas City, MO','Charlotte, NC','Red Bank, NJ','Henderson, NV','Melville, NY','Allentown, PA','Irving, TX'],
jobs: null,
checkedLocations: []
},
created: function () {
this.fetchData();
},
methods: {
fetchData: function () {
this.jobs = [
{ id: '1', title: 'Circuit Court Clerk', description: 'lorem ipsum', location: 'Charlotte, NC', department: 'Sales and Marketing', date: '23/10/17' },
{ id: '2', title: 'Tie Buyer', description: 'lorem ipsum', location: 'Irvine, CA', department: 'Media Relations', date: '21/10/16' },
{ id: '3', title: 'Leaded Glass Installer', description: 'lorem ipsum', location: 'Kansas City, MO', department: 'Public Relations', date: '16/09/17' },
{ id: '4', title: 'Wheat Inspector', description: 'lorem ipsum', location: 'Red Bank, NJ', department: 'Quality Assurance', date: '12/06/16' },
{ id: '5', title: 'Executive Officer, Special Warfare Team', description: 'lorem ipsum', location: 'Irving, TX', department: 'Public Relations', date: '19/03/18' },
{ id: '6', title: 'Wildlife Control Agent', description: 'lorem ipsum', location: 'Irvine, CA', department: 'Sales and Marketing', date: '07/01/17' },
{ id: '7', title: 'Arresting Gear Operator', description: 'lorem ipsum', location: 'Charlotte, NC', department: 'Asset Management', date: '04/09/17' },
{ id: '8', title: 'Arresting Gear Operator', description: 'lorem ipsum', location: 'Chandler, AZ', department: 'Tech Support', date: '01/04/17' },
{ id: '9', title: 'Biogeographer', description: 'lorem ipsum', location: 'Chicago, IL', department: 'Quality Assurance', date: '01/05/17' },
{ id: '10', title: 'LAN Systems Administrator', description: 'lorem ipsum', location: 'Scottsdale, AZ', department: 'Customer Relations', date: '29/04/18' },
{ id: '11', title: 'Copper Plater', description: 'lorem ipsum', location: 'Irving, TX', department: 'Tech Support', date: '17/08/17' },
{ id: '12', title: 'Leaded Glass Installer', description: 'lorem ipsum', location: 'Rockville, MD', department: 'Sales and Marketing', date: '02/11/16' },
{ id: '13', title: 'Line Cook', description: 'lorem ipsum', location: 'Chandler, AZ', department: 'Advertising', date: '02/12/17' },
{ id: '14', title: 'Special Education Teaching Assistant', description: 'lorem ipsum', location: 'Red Bank, NJ', department: 'Payroll', date: '02/05/17' },
{ id: '15', title: 'Clarinetist', description: 'lorem ipsum', location: 'Melville, NY', department: 'Payroll', date: '30/05/17' },
{ id: '16', title: 'Arresting Gear Operator', description: 'lorem ipsum', location: 'Henderson, NV', department: 'Payroll', date: '23/02/18' },
{ id: '17', title: 'Wheat Inspector', description: 'lorem ipsum', location: 'Red Bank, NJ', department: 'Tech Support', date: '12/08/16' },
{ id: '18', title: 'Wildlife Control Agent', description: 'lorem ipsum', location: 'Melville, NY', department: 'Payroll', date: '03/05/17' },
{ id: '19', title: 'Print Retoucher', description: 'lorem ipsum', location: 'Chicago, IL', department: 'Sales and Marketing', date: '19/06/16' },
{ id: '20', title: 'Mathematical Statistician', description: 'lorem ipsum', location: 'Scottsdale, AZ', department: 'Tech Support', date: '10/07/16' }
];
},
filterJobs: function(event) {
app.jobs = app.jobs.filter(function (job) {
return job.location.includes(app.checkedLocations);
});
// filterJobs: function(event) {
// app.jobs = app.jobs.filter( function( location ) {
// return !app.checkedLocations.includes( location );
// } );
}
}
});
};
return { init, app };
})();
(function(){
careers.taleo.init();
})();
#app {
padding: 25px;
}
#app ul {
margin-left: 0;
padding: 0;
}
#app ul li {
list-style: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js"></script>
<div id="app">
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<h3>Locations</h3>
<ul>
<li v-for="location in locations">
<input type="checkbox" v-model="checkedLocations" v-on:click="filterJobs" v-bind:value="location" /> {{location}}
</li>
</ul>
<span>Checked locations: {{ checkedLocations }}</span>
</div>
<div class="col-md-9">
<div class="jobs">
<div class="job" v-for="job in jobs">
<div class="card" style="margin: 5px 0">
<div class="card-block">
<h4 class="card-title">
{{ job.title }}
</h4>
<small class="pull-right text-muted" style="font-size: 12x">
{{job.date}}
</small>
<p class="card-text">
<!-- {{job.description}} -->
</p>
<button class="btn btn-sm btn-primary">View</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
https://codepen.io/neil/pen/WjXrxx
필터링된 위치에 계산된 값을 사용합니다.
computed:{
filteredJobs(){
// if there are no checked locations, return everything.
// You could remove this if you only want to return
// checked locations.
if (!this.checkedLocations.length)
return this.jobs
return this.jobs.filter(j => this.checkedLocations.includes(j.location))
}
}
템플릿을 수정합니다.
<div class="job" v-for="job in filteredJobs">
filterJobs 메서드도 삭제합니다.
예.
기본적으로는 필터가 필요했습니다.
this.checkedLocations.includes(job.location).
또한 데이터가 유출되기 때문에 필터링할 때마다 데이터를 변경할 필요가 없습니다.그러나 계산된 값을 사용하는 것이 Vue에게 더 적합합니다.
작업을 나열하는 줄에 v-if 사용:
<div class="job" v-if=filterJobs(job) v-for="job in jobs">
다음과 같은 메서드 filterJobs를 사용합니다.
filterJobs: function(data) {
if (this.checkedLocations.length == 0) return true;
return this.checkedLocations.includes(data.location);
}
언급URL : https://stackoverflow.com/questions/43925816/checkbox-filtering-with-vuejs
반응형
'programing' 카테고리의 다른 글
| MinGW의 "Unknown type name 'uint8_t' 메시지 (0) | 2022.08.01 |
|---|---|
| vue 구성 요소에서 vuex에서 필터링된 데이터가 반응하지 않음 (0) | 2022.08.01 |
| MySQL JDBC 드라이버 5.1.33 - 타임존 문제 (0) | 2022.08.01 |
| C 프로그래밍용 REP가 있습니까? (0) | 2022.08.01 |
| Android, 문자열에서 리소스 ID를 가져오고 있습니까? (0) | 2022.08.01 |