Selecting within a selection

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Selecting within a selection

Post by IGGt »

I have the following javascript:

Code: Select all

var b = 1;
function showRows2()
	{
		var $rows2 = $('#dbTable2 tr');
			
		$rows2.css('display','none');
			
		$rows2.eq(0).css('display','table-row'); //Display Header
		$rows2.eq(b).css('display','table-row'); //Show Row
   		b = (b == $rows2.length-1) ? 1 : b + 1; //increment by one
   	    	
   	};
   	
function hideShow2(time)
	{	
		timer2 = setInterval('showRows2()',time);	
	};
This loops through every row in a table, and displays them one after the other.

However, within that table each row has a class of either 'group1', 'group2', 'group3'.

I need to modify the above javascript so that:

[text]If there are rows with 'Group 1' it loops through only those, and all others remain hidden,

else

If there are rows with 'Group 2' it loops through only those, and all others remain hidden,

else

it loops through all those rows with 'group 3' (which would be all of them as that is the default).[/text]

But I can't figure how to do it. in my head it sounds like it should be really easy, but I just can't figure out how to do it in my script. Can any one advise?
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Selecting within a selection

Post by IGGt »

After a good sleep, I realise the answer is as simple as I thought it was:

Code: Select all

var b = 1;
function showRows2()
	{
		var $rows2 = $('#dbTable2 tr'); //Get ALL Rows
		
		var $rowsST2 = $('#dbTable2 tr.Group1'); //Get Group1 Rows
		var $rowsBE2 = $('#dbTable2 tr.Group2'); //Get Group2 Rows
		var $rowsOK2 = $('#dbTable2 tr.Group3'); //Get Group3 Rows
			
		if ($rowsST2.length >= 1){$rows = $rowsST2;}else if($rowsBE2.length >= 1){$rows = $rowsBE2;}else{$rows = $rowsOK2;};
	
		$rows2.css('display','none'); //Hide ALL Rows	
	
		$rows2.eq(0).css('display','table-row'); //Display Table Header

		$rows.eq(b).css('display','table-row'); //Show Row
		if (b == $rows.length-1){b = 0;}else{b++;};	//Increment to Next Row		
	};

function hideShow2(time)
	{	
		timer2 = setInterval('showRows2()',time);	
	};
Post Reply