admin管理员组文章数量:1414628
I'm not advanced enough in JS to get this working fully.
I have two select fields with options:
<select name="" id="filter-position">
<option value="all">Filter by position</option>
<option value=".instructor">Instructor</option>
<option value=".leader">Leader</option>
<option value=".blah">Blah</option>
<option value=".whatever">Whatever</option>
</select>
<select name="" id="filter-location">
<option value="all">Filter by position</option>
<option value=".portland">Portland</option>
<option value=".missoula">Missoula</option>
<option value=".chicago">Chicago</option>
<option value=".newyork">New York</option>
</select>
The container where the filtered items live looks a little like this:
<ul id="filter-container">
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
</ul>
I can get each one to filter properly but not together (i.e., AND). The JS code I'm using goes like this:
$(function(){
var $positionSelect = $('#filter-position'),
$locationSelect = $('#filter-location'),
$container = $('#filter-container');
$container.mixItUp({});
$positionSelect.on('change', function(){
$container.mixItUp('filter', this.value);
});
$locationSelect.on('change', function(){
$container.mixItUp('filter', this.value);
});
});
If I filter by one select, things filter properly. If I then filter by the other, it overrides the first filter. What I want is for them to both be used.
I know there's a way to get this to work, but I'm not sure how to get that concatenated string to do the "and" logic as documented in the Advanced Filtering section of the docs using <select>
. Help?
I'm not advanced enough in JS to get this working fully.
I have two select fields with options:
<select name="" id="filter-position">
<option value="all">Filter by position</option>
<option value=".instructor">Instructor</option>
<option value=".leader">Leader</option>
<option value=".blah">Blah</option>
<option value=".whatever">Whatever</option>
</select>
<select name="" id="filter-location">
<option value="all">Filter by position</option>
<option value=".portland">Portland</option>
<option value=".missoula">Missoula</option>
<option value=".chicago">Chicago</option>
<option value=".newyork">New York</option>
</select>
The container where the filtered items live looks a little like this:
<ul id="filter-container">
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
<li class="mix">...</li>
</ul>
I can get each one to filter properly but not together (i.e., AND). The JS code I'm using goes like this:
$(function(){
var $positionSelect = $('#filter-position'),
$locationSelect = $('#filter-location'),
$container = $('#filter-container');
$container.mixItUp({});
$positionSelect.on('change', function(){
$container.mixItUp('filter', this.value);
});
$locationSelect.on('change', function(){
$container.mixItUp('filter', this.value);
});
});
If I filter by one select, things filter properly. If I then filter by the other, it overrides the first filter. What I want is for them to both be used.
I know there's a way to get this to work, but I'm not sure how to get that concatenated string to do the "and" logic as documented in the Advanced Filtering section of the docs using <select>
. Help?
3 Answers
Reset to default 5You're really close! Because of how the mixItUp API works, filters aren't additive (e.g. when you call mixItUp.filter
, it doesn't add a new filter to what's already active). So, you just need to construct a new filter string from the values of both dropdowns whenever one of them changes. You can acplish that like so:
$(function(){
var $positionSelect = $('#filter-position'),
$locationSelect = $('#filter-location'),
$container = $('#filter-container');
$container.mixItUp({});
$('#filter-position, #filter-location').on('change', function() {
var filterString = $positionSelect.val() + $locationSelect.val();
$container.mixItUp('filter', filterString);
});
});
I opted to use the jQuery .val()
over the native .value
in this case since you're already using jQuery, and val()
will probably be more reliable in this case. Let me know if you run into trouble.
You may also want to to change this:
<option value="all">Filter by position</option>
To this:
<option value="">Filter by position</option>
for both of your dropdown menus, otherwise you might get some weird behavior.
Use ma like.. $('#filter-location, #filter-container') .on('change', function(){ ... ;)
Something like this...
$(function(){
$container = $('#filter-container');
$container.mixItUp({});
$container.on('change','#filter-position, #filter-location', function(){
$container.mixItUp('filter', this.value);
});
});
本文标签: javascriptMixItUp with two selectdropdown fieldsStack Overflow
版权声明:本文标题:javascript - MixItUp with two selectdropdown fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155648a2645155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论