0
관련된 값으로 태그가 지정된 모든 항목을 표시해야하는 JavaScript 필터에서 작업하고 있습니다. 현재로서는 할당 된 값 중 하나가있는 한 항목을 표시하는 대신 할당 된 각 값이있는 항목을 표시하도록 작업하고 있습니다.AND 필터를 OR 필터로 업데이트 Javascript
예를 들어, Cats and Dogs (캐츠와 개)를 확인하면 두 값이 할당 된 항목을 표시하는 대신 이러한 값 중 하나가 할당 된 모든 게시물을 표시하려고합니다.
내 코드의 예는 다음과 같습니다 :
var $filterCheckboxes = $('input[type="checkbox"], input[type="radio"]');
$filterCheckboxes.on('change', function() {
var selectedFilters = {};
$filterCheckboxes.filter(':checked').each(function() {
if (!selectedFilters.hasOwnProperty(this.name)) {
selectedFilters[this.name] = [];
}
selectedFilters[this.name].push(this.value);
});
var $filteredResults = data;
// console.log('filtered data: ' + $filteredResults);
$.each(selectedFilters, function(name, filterValues) {
$filteredResults = $filteredResults.filter(function(item) {
var matched = false,
currentFilterValues = item.Tags;
console.log(currentFilterValues);
$.each(currentFilterValues, function(_, currentFilterValue) {
if ($.inArray(currentFilterValue, filterValues) != -1) {
matched = true;
return false;
console.log(currentFilterValue);
}
});
return matched;
});
});
어떤 도움을 주시면 감사하겠습니다. 선택한 태그 (들)이 일치하는 경우, 그것은 볼 것이다 그래서 지금 확인란이 선택되었다 때마다
var $filterCheckboxes = $('input[type="checkbox"], input[type="radio"]');
$filterCheckboxes.on('change', function() {
var $filteredResults = data;
var selectedFilters = [];
$filterCheckboxes.filter(':checked').each(function() {
selectedFilters.push(this.value);
});
$filteredResults = data.filter(function(item) {
return (_.intersection(item.Tags, selectedFilters).length > 0);
});
});
:
당신은'console.log'의 결과물을 공유하고 어떤 출력이 달라야한다고 지정할 수 있습니까? – gurvinder372
그래서 첫 번째 체크 박스를 클릭하면 다음과 같이 출력됩니다 : (6) [ "블로그", "사례 연구", "건설 금융", "글로벌 비즈니스 모니터", "건설 금융", "통찰력" [ "Insight", "Construction", "Construction Finance"] [ "Insight"] (2) [ "블로그", "통찰력"] Finance "] –
블로그의 태그를 가지고있는 모든 항목을 보여 주며, 다음 항목 (예 : 사례 연구)을 확인하자마자 두 항목이 모두있는 항목 만 표시합니다. 어디에서 두 태그가 아닌 두 태그 중 하나를 가진 모든 항목을 표시하려고 시도해야합니다. –