同位素过滤-重置为起始位置时出现的问题 [英] Isotope Filtering - Issues when reset to start position
问题描述
我已经建立了一个同位素网格布局来显示项目,该布局还使用了同位素过滤来更快地对项目进行排序.
I have built a Isotope grid layout to display projects, which also uses the Isotope filtering to sort through the projects quicker.
在您尝试将下拉列表放回默认位置所有服务"和[& 所有部门".然后,所有项目都将被隐藏,而所有它们都应显示出来.
The filtering works fine until you try to put the dropdowns back to their default position "All Services" & "All Sectors". Then all the projects are hidden, when they should all be displayed.
http://www.ellyon.co.uk/wp/projects/
可能是Jquery冲突吗?我已经将我的functions.php添加为不是100%确定ive正确添加了脚本.
Is it possibly a Jquery conflict? I've added my functions.php as im not 100% sure ive added scripts correctly.
functions.php
functions.php
function add_isotope() {
wp_register_script( 'isotope-init', get_template_directory_uri().'/js/isotope.js', array('jquery', 'isotope'), true );
wp_register_style( 'isotope-css', get_stylesheet_directory_uri() . '/styles/project.css' );
wp_enqueue_script('isotope-init');
wp_enqueue_style('isotope-css');
}
add_action( 'wp_enqueue_scripts', 'add_isotope' );
function modify_jquery() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', false, '3.2.1' );
wp_enqueue_script( 'jquery' );
}
}
add_action( 'init', 'modify_jquery' );
Isotope.js
Isotope.js
// Grid
var $grid = $('.grid').isotope({
itemSelector: '.grid-item',
layoutMode: 'packery',
columnWidth: '.grid-sizer',
packery: {
gutter: '.gutter-sizer'
}
});
// state variable
var $expandedItem;
// expand when clicked
$grid.on( 'click', '.grid-item', function( event ) {
var isExpanded = $expandedItem && event.currentTarget == $expandedItem[0];
if ( isExpanded ) {
// exit if already expanded
return;
}
// un-expand previous
if ( $expandedItem ) {
$expandedItem.removeClass('gigante');
}
// set new & expand
$expandedItem = $( event.currentTarget ).addClass('gigante');
$grid.isotope('layout');
});
$grid.on( 'click', '.close-button button', function( event ) {
$expandedItem.removeClass('gigante');
// reset variable
$expandedItem = null;
$grid.isotope('layout');
event.stopPropagation();
});
// Select Filters
$(function() {
var $container = $('.grid'),
$select = $('div#filterGroup select');
filters = {};
$container.isotope({
itemSelector: '.grid-item'
});
$select.change(function() {
var $this = $(this);
var $optionSet = $this;
var group = $optionSet.attr('data-filter-group');
filters[group] = $this.find('option:selected').attr('data-filter-value');
var isoFilters = [];
for (var prop in filters) {
isoFilters.push(filters[prop])
}
var selector = isoFilters.join('');
$container.isotope({
filter: selector
});
return false;
});
$grid.imagesLoaded().progress( function() {
$grid.isotope('layout');
});
});
推荐答案
星号*
是特例,需要与其他过滤器值区别对待,例如:
Asterisk, *
, is a special case and need to be handled differently from other filter values such that:
- 如果选择了一个或多个非星号值,则应忽略所有星号
- 如果未选择任何非星号值,则默认为单个星号(显示全部).
必须有多种方法来实施这些规则.这是一个:
There must be a number of ways to implement those rules. Here's one :
// Select Filters
$(function() {
var $grid = $('.grid');
var $selects = $('div#filterGroup select').change(function() {
var selector = $selects.get().map(function(el) { // map the select elements ...
return $(el).data('filter-value'); // ... to an array of filter-values
}).filter(function(val) {
return val !== '*' // filter out all '*' values
}).join('') || '*'; // if joined array is empty-string, then default to a single '*'
$grid.isotope({
'filter': selector
});
return false;
});
...
});
请注意,原始代码的filters
对象只会增加不必要的复杂性. DOM非常擅长表示自己的状态,因此,与在javascript中维护持久性映射相比,每次将两个选择元素中的任何一个发生更改时,将两个选择元素直接直接映射到Array都更为简单.
Note that the original code's filters
object only adds unnecessary complexity. The DOM is very good at representing its own state, so instead of maintaining a persistent mapping in javascript, it's simpler to map both select elements directly to Array every time there is a change to either one of them.
如果HTML的构造如下,则代码将稍微简化:
The code would simplify slightly if the HTML was constructed as follows :
<select class="filter option-set" data-filter-group="services">
<option value="">All Services</option>
<option value='.cladding'>Cladding</option>
...
</select>
<select class="filter option-set" data-filter-group="sectors">
<option value="">All Sectors</option>
<option value='.commerical'>Commerical</option>
...
</select>
然后,.filter()
删除星号的需求消失了,剩下了:
Then, the need to .filter()
out asterisks would disappear, leaving :
$(function() {
var $grid = $('.grid');
var $selects = $('div#filterGroup select').change(function() {
var selector = $selects.get().map(function(el) { // map the select elements ...
return $(el).val(); // ... to an array of values
}).join('') || '*'; // if joined array is empty-string, then default to a single '*'
$grid.isotope({
'filter': selector
});
return false;
});
...
});
这篇关于同位素过滤-重置为起始位置时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!