DotDragnet
May 24, 2012, 08:51:29 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: jquery loops  (Read 364 times)
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« on: December 06, 2011, 03:23:51 PM »

I don't know much about the jquery loops (each?) so wondered if someone could point me in the right direction for the following, which I think is possible...

I have say 10 divs on a page, all divs have a class of post. Then within the class list, they'll also have a number of other classes, some of which denote which category or categories that post is in eg. category-x, category-y and category-z.

I want to be able to say, loop through all posts on the page and hide posts in category-y and category-z but not if they're in category-x.

Eg.
post 1 is in x and y (so stays showing)
post 2 is in y and z (so hides)
post 3 is in x (so stays)
post 4 is in z (so hides)
post 5 is in x, y and z (so stays)
etc.

Thing is, I only know that a post should be in category-x to stay, not that it shouldn't be in y or z. So I need to sort of say 'if there isn't a class matching category-x, then hide'. To (possible) make it more complicated, there won't just be one category to match, there could be x1, x2 and x3, of which a post in any of those categories should stay showing.

Thoughts? Is it possible via jQuery?

TIA smile
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #1 on: December 06, 2011, 03:36:41 PM »

 biggrin

Love giving you a challenge!
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #2 on: December 06, 2011, 03:44:34 PM »

Tongue  Think I've sussed it but need to test it and see if there's a better way.
Logged

SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« Reply #3 on: December 06, 2011, 06:59:33 PM »

I used a script on my portfolio have dig in my site smile ill try and find the original script for you.

EDIT: it is called filterable you can grab it from my site by viewing the code as i can't find an original site. there are a few links if you search for it.

http://net.tutsplus.com/tutorials/javascript-ajax/creating-a-filterable-portfolio-with-jquery/
« Last Edit: December 06, 2011, 07:05:15 PM by SLEE » Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #4 on: December 06, 2011, 08:01:12 PM »

Cheers Lee. Only issue with that is that it uses hasClass(), which I could have more than one class, so I'd have to loop through all classes for every post (eg. 10 posts on a page), which probably takes up more resources than necessary.

I've got (untested)

Code:
cats = cats.toString();  // cats is an array of class names
cats = cats.replace(',', '|');
var chk = new RegExp(cats, "gi");

$('#div.post').each(function(i,row){
var found = false;
var classList = $(this).attr('class').split(/\s+/);
$.each( classList, function(index, item){
    if (chk.test(item)) {
        found = true;
    }
});

if (!found) {
$(this).fadeOut();
}
});
Logged

samhs
Administrator
Hero Member
*****
Posts: 1713



View Profile WWW Awards
« Reply #5 on: December 06, 2011, 10:01:22 PM »

all fine from a cursory glance, but you don't need to use attr("class") - use hasClass() instead:

Code:
var ismatch;
$("#div.post").each(function(){
  ismatch = false;
  for(var x=0;x<cat.length;x++) {
    if($(this).hasClass(cat[x])) ismatch=true;
  }
  if (!ismatch) $(this).fadeOut();
});
(warning: untested code)


That will hide anything that doesn't match at least one of the classes specified in the cats array. If the cats array contains many classes, and you're only testing for a match based on whether the classname contains the string "gi", then you'll have to do something more like you have.

HTH
Logged

Loose adj a not held together; not fastened or firmly fixed in place
Lose verb to misplace something. To fail to keep or obtain something, especially because of a mistake, carelessness, etc.
---
Blog: www.ohwrite.co.uk
Twitter: www.twitter.com/samhs
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #6 on: December 06, 2011, 10:33:12 PM »

Thanks Sam smile  Just out of interest, how much sort of resource use / time could looping through the cat array produce for 10 posts? For example, someone may have 5 or 6 categories in the array, so that would mean looping through up to 60 times (can I break out of the for loop like you can in PHP once a match is found, to save loop through the rest when it's unnecessary?)

My code (in theory) just took the array and put it into a regex to avoid looping through the array multiple times (the 'gi' are the modifiers global and case insensitive, in case that wasn't obvious).

if there's not much difference in efficiency then your code looks a lot cleaner and easier to read! I'll try both out and see if there's much difference in speed smile

Thanks again, much appreciated
Logged

samhs
Administrator
Hero Member
*****
Posts: 1713



View Profile WWW Awards
« Reply #7 on: December 07, 2011, 01:00:53 PM »

yeah, you're quite right in that it's less efficient to loop through the array multiple times, but I suspect if you're only talking a total of 10 posts, and 6 elements in the Array, you'll notice no degradation in performance. And yes, you can indeed break out using break;

Even after using them for years, I still find regular expressions hard to get my head around. Every time I use one I feel like I'm learning from complete noob status Sad The principle is fine, but decoding is another story altogether!
Logged

Loose adj a not held together; not fastened or firmly fixed in place
Lose verb to misplace something. To fail to keep or obtain something, especially because of a mistake, carelessness, etc.
---
Blog: www.ohwrite.co.uk
Twitter: www.twitter.com/samhs
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #8 on: December 07, 2011, 08:42:39 PM »

Cheers Sam. Yeah I'm still trying to get my head round regex. I'm getting there slowly with PHP, but there are a few differences in JS and that's the first one I tried in the above script. However converted it to your script as I think it's cleaner smile

Thanks again. Appreciated smile
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!