问题
I'm several hours into building a simple sortable search results for an online merchant site. The way it works, is that a html template is fetched of the search page, while the search is carried out with ajax. the json data is retrieved, and the div elements are then created. I have already completed the sort,per, and direction sorts, but now i'm stuck on the filter sort and i really have no idea where to even begin on this last one.
the last sort function needs to iterate over each div, checking the selected attribute for a 1 or a 0. if the attribute is 0, the hide class needs to be toggled for the div.
My question is:
- how to access current selected div inside of
each() - how to check if the datatarget is equal to 0. hide it if it is 0, or
- how to continue to next iteration of
each()if it is not.
a brief pseudocode of what i'm trying to achieve:
$('div.item').each(function(){
datartarget = (datatarget == 0) ? currentdiv.hide() : datatarget;
});
Here's my full jQuery code:
$(function(){
var last_selector = null;
last_selector = $('.filter , .sort, .per, .direction').click(function(){
var clname = this.className;
var direction = $('#direction > dd.active > a').attr('data-target');
$('.sub-nav.'+clname+'').find('dd.active').removeClass('active');
$(this).parent().addClass('active');
var datatarget = $(this).attr('data-target');
if(clname == 'sort')
{
$('div.item').tsort({order:direction,attr: datatarget+'-data'});
last_selector = datatarget;
}
if(clname == 'filter')
{
last_selector = (last_selector == null) ? last_selector : $('#sort dd.active > a').attr('data-target');
$('div.item').each(function(){
//have no idea where to start )-:
})
}
if(clname == 'per')
{
last_selector = (last_selector == null) ? last_selector : $('#sort dd.active > a').attr('data-target');
var item_count = $('div.item').length;
if(item_count > datatarget)
{
while (item_count > datatarget) {
$('div.item:last-child').toggleClass('hide');
item_count = $('div.item').length;
}
}
if(item_count < datatarget)
{
$('div.item.hide').toggleClass('hide');
while (item_count > datatarget) {
$('div.item:last-child').toggleClass('hide');
item_count = $('div.item').length;
}
}
}
if(clname == 'direction')
{
direction = $(this).attr('data-target');
datatarget = (last_selector == null) ? last_selector : 'price';
$('div.item').tsort({order:direction,attr: datatarget+'-data'});
}
return last_selector;
});
});
and the html
the dropdown selection divs(provides the datatarget for the click functions):
<a data-dropdown="direction" class="button dropdown">Direction Order</a>
<dl id="direction" data-dropdown-content class="sub-nav direction f-dropdown content">
<dt><strong>Direction:</strong></dt>
<dd class="active"><a class="direction" data-target="desc">Down</a></dd>
<dd><a class="direction" data-target="asc">Up</a></dd>
</dl>
<a data-dropdown="filter-by" class="button dropdown">Filter Results</a>
<dl id="filter-by" data-dropdown-content class="sub-nav filter f-dropdown content">
<dt><strong>Filter Results:</strong></dt>
<dd class="active"><a class="filter" data-target="new">Newest</a></dd>
<dd><a class="filter" data-target="endsoon">Ending Soon</a></dd>
<dd><a class="filter" data-target="reserve">No Reserve</a></dd>
<dd><a class="filter" data-target="buyitnow">Buy It Now</a></dd>
<dd><a class="filter" data-target="all">All</a></dd>
</dl>
<a data-dropdown="sort-by" class="button dropdown">Sort Results</a>
<dl id="sort-by" data-dropdown-content class="sub-nav sort f-dropdown content">
<dt><strong>Sort By:</strong></dt>
<dd class="active"><a class="sort" data-target="end">End Time</a></dd>
<dd><a class="sort" data-target="price">Price</a></dd>
<dd><a class="sort" data-target="seller">Seller Reputation</a></dd>
</dl>
<a data-dropdown="results-per" class="button dropdown">Results Per Page</a>
<dl id="results-per" data-dropdown-content class="sub-nav per f-dropdown content">
<dt><strong>Per Page:</strong></dt>
<dd class="active"><a class="per" data-target="100">100</a></dd>
<dd><a class="per" data-target="75">75</a></dd>
<dd><a class="per" data-target="50">50</a></dd>
<dd><a class="per" data-target="25">25</a></dd>
</dl>
回答1:
Thanks to the help of barmar, i answered my own question.
i was confused because i didn't realize that this would be rebound inside of each().
the following accomplishes my goal, of changing the current filter, by first unhiding any hidden elements, then applying the new filter.
if(clname == 'filter')
{
$('div.item.hide').toggleClass('hide');
last_selector = (last_selector == null) ? last_selector : $('#sort dd.active > a').attr('data-target');
$('div.item').each(function(){
($(this).attr(datatarget+'-data') == 0) ? $(this).hide() : false;
});
}
Of course, this only addresses the basic functionality. i still have to modify it to maintain the results per page rules.
来源:https://stackoverflow.com/questions/24652337/filtering-divs-in-jquery-and-hiding-them-based-on-a-custom-data-attribute-tag