将过滤元素与同位素组合
我目前正在使用同位素来过滤出版物列表,但希望能够将标准的、记录的链接过滤器方法与选择元素结合起来,因为我的第二个过滤器列表相当长。
我正在努力解决的问题是处理两种不同类型的元素,并将选定的值组合到一个选项数组中。我可以使过滤器彼此独立工作(代码如下),但它们需要协同工作。如何将两个不同的操作(单击或更改)和两个属性(class= 或 value=)组合到一个选项数组中以传递给同位素过滤器?
var $container = $('#library');
// select ccskills publications by default
$container.isotope({ filter: '.ccskills' });
var $optionSets = $('#library-options .option-set'),
$optionLinks = $optionSets.find('a');
$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents('.option-set');
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
// make option object dynamically, i.e. { filter: '.my-filter-class' }
var options = {},
key = $optionSet.attr('data-option-key'),
value = $this.attr('data-option-value');
// parse 'false' as false boolean
value = value === 'false' ? false : value;
options[ key ] = value;
$container.isotope( options );
return false;
});
// using the 'chozen' plugin to style my select element
$(".chzn-select").chosen().change(
function() {
var industry = $("option:selected", this).val();
$container.isotope({filter: industry});
}
);
I'm currently using Isotope to filter a list of publications, but would like to be able to combine the standard, documented link-filter method with a select element, as my second list of filters is quite long.
The bit I'm struggling with is dealing with two distinct types of element and combining selected values into one option array. I can make the filters work independently of each other (code below) but they need to work together. How can I combine the two different actions (click or change) and two attributes (class= or value=) into one options array to pass to the isotop filter?
var $container = $('#library');
// select ccskills publications by default
$container.isotope({ filter: '.ccskills' });
var $optionSets = $('#library-options .option-set'),
$optionLinks = $optionSets.find('a');
$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents('.option-set');
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
// make option object dynamically, i.e. { filter: '.my-filter-class' }
var options = {},
key = $optionSet.attr('data-option-key'),
value = $this.attr('data-option-value');
// parse 'false' as false boolean
value = value === 'false' ? false : value;
options[ key ] = value;
$container.isotope( options );
return false;
});
// using the 'chozen' plugin to style my select element
$(".chzn-select").chosen().change(
function() {
var industry = $("option:selected", this).val();
$container.isotope({filter: industry});
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个多维数组,其中每个过滤器类型都有一个数组维度。我以前做过,有一个现场演示示例 这里!
请参阅此处!对于js。
编辑(2020 年 2 月):演示和 JavaScript 的链接已更新为 2020 年,带有回溯机器 URL
在我的 js 文件中,您会对名为 getComboFilters(filters) 的最终函数感兴趣,
该函数处理所有各种过滤器数组集的推送、拼接和排序,但要使用它,您需要将其添加到您的过滤器例程中......好吧,我说例程,但它实际上是例程,因为您似乎调用过滤器方法 2 个单独的时间:
以及稍后:
如果您要一起过滤类型,则所有过滤器类型都必须彼此了解,这意味着它们必须位于相同的 javascript 封装和过滤器方法内只需调用一次,然后根据所有过滤器类型条件对其进行测试。
使用 getComboFilter(filters) 函数,您可以像这样调用组合过滤器方法:
最后,完全集成到您的文件中将是这样的:
希望这对某人有帮助!该演示非常流畅。
You need a multi-dimensional array, with an array dimension for each filter type you have. I've done that before and there's a live demo example here!
See here! for the js.
EDIT (Feb, 2020): Links to demo and JavaScript have been updated for 2020 with wayback machine urls
In my js file you'll be interested in the end function called getComboFilters( filters )
This function handles all of the pushing, splicing and sorting of the various sets of filter arrays but to use it you need to add it to your filter routine... well I say routine but it routines really because you seem to call the filter method 2 separate times:
and later:
If your going to filter types together all of your filter types are going to have to know about each other, meaning they're going to have to be inside the same javascript enclosure and the filter method needs to be called only once at which time it gets tested against all your filter type conditions.
using the getComboFilter( filters ) function you call the combination filter method like this:
Finally, the full integration into your file would be somethign like this:
Hope this helps somebody! The demo is pretty slick.