Jump to content

Recommended Posts

if I have an array like this:

 

thisarray = new Array("this", "that", "theotherthing");

 

how could I go about building a conditional like so:

 

if(thisarray[0] == thisvar && thisarray[1] == thisvar && thisarray[2] == thisvar) {

    //do something

}

 

the caveat is that I do not know how many items could be in thisarray. I'm a bit stumped as to how to accomplish this. Any help would be appreciated.

 

I run a mysql query and get data returned which I dump into a grid layout on my page. I take that returned data and store it in a javascript object...basically I grab each row as an assoc array

 

rowitems[fieldname] = fieldvalue

 

(rowitems[id] = "1", rowitems['department'] = "photography", etc.)

 

then store the array in my object

 

gridObject[thisrow] = rowitems

 

I have a filter form that the user can submit to filter the returned data. In essence I am trying to avoid making another AJAX call back to the database and appending the original query to get filtered results. I'm trying to see if I can do it on the client end. I capture the filter items (department, pubcode, etc.) and also how they want the results returned (AND or OR)...or is easy, match up the filter field with the rowitems array fields, then see if their values match and reset the array item rowitems[meetsquery] from false to true...the problem comes from when someone wants more than one criteria for a match...department AND pubcode...

 

If I knew exactly how many items they would need to match it would be easy but in one case it could be department AND pubcode while in another it could be department AND pubcode AND affiliate...etc. etc.

 

Here's the full code I am working with. (This will get simplified somewhat as I am just testing stuff now but realized that instead of pulling the original data from the html and breaking it down and putting it into my object I return it from AJAX already in a broken down state, by row and col.)

 

$(".btnFilter").live('click', function() { 

		//get items to filter by
		var filteritems = new Array();
		var fieldnames = new Array();

		//get connector
		var connector = $("input[@name='connector']:checked").val();

		//get department 
		var deptIndex = $('#department').val();
		if (deptIndex != 0 ) { 
			var department = $("#department option[value='" + deptIndex + "']").text();
			filteritems['department'] = department;
		}

		var bucketIndex = $('#bucket').val();
		if (bucketIndex != 0 ) { 
			var bucket = $("#bucket option[value='" + bucketIndex + "']").text();
			filteritems['bucket'] = bucket;
		}

		//make object from results
		gridObject = new Object();

		//get all the rows
		var rowlist = $('li[row]');

		for(var r=0; r<rowlist.length; r++) {

			//get the row content
			var thisrow = $(rowlist[r]).html();
			//get all the p tags
			var rowitems = $(thisrow + 'p.[rowitem]');

			//get field name
			var temparray = new Array();
			for(var ri=0; ri<rowitems.length; ri++) {
				var fieldname = $(rowitems[ri]).attr('rowitem');
				var fieldvalue = $(rowitems[ri]).html();
				temparray[fieldname] = fieldvalue;

				//make array of field names
				if (r==1) { 
					fieldnames.push(fieldname) 
				}
			}


			temparray['meetsquery'] = false;

			//make gridObject row element here
			var thisrow = "row_" + r;
			//add temparray as value to object element
			gridObject[thisrow] = temparray;


		}

		//add the meetsquery key
		fieldnames.push("meetsquery");

		//loop through the search items
		for (thisfilter in filteritems) { 

			//here's the point where I am getting stuck - check if it is AND or OR, or is easy enough as I said, I am stuck on the AND part...			
		}
         $.unblockUI();
});

 

NOTE: Some of the field names are weird sounding, I am working with an accountant on this project, so just go with it.  :D

I solved this last night, posting the code for others. If it could be streamlined I'd love to see the comments. Thanks.

 


function checkstuff() {

	var connector = "and";

	//stuff to filter by
	var filteritems = new Array;
	filteritems["A"] = "one";
	filteritems["B"] = "two";
	filteritems["C"] = "three";

	//fieldnames
	var fieldnames = new Array("A", "B", "C", "D");

	//data
	var dataarray = new Array;
	var temparray = new Array;
	temparray['id'] = 1;
	temparray["A"] = "one";
	temparray["B"] = "c";
	temparray["C"] = "three";
	temparray["D"] = "unused object"
	temparray["usethis"] = false;
	dataarray.push(temparray); 

	temparray = new Array;
	temparray['id'] = 2;
	temparray["A"] = "one";
	temparray["B"] = "two";
	temparray["C"] = "y";
	temparray["D"] = "unused object"
	temparray["usethis"] = false;
	dataarray.push(temparray);

	temparray = new Array;
	temparray['id'] = 3;
	temparray["A"] = "one";
	temparray["B"] = "two";
	temparray["C"] = "three";
	temparray["D"] = "unused object"
	temparray["usethis"] = false;
	dataarray.push(temparray);

	temparray = new Array;
	temparray['id'] = 4;
	temparray["A"] = "x";
	temparray["B"] = "y";
	temparray["C"] = "z";
	temparray["D"] = "unused object"
	temparray["usethis"] = false;
	dataarray.push(temparray);

	temparray = new Array;
	temparray['id'] = 5;
	temparray["A"] = "a";
	temparray["B"] = "b";
	temparray["C"] = "c";
	temparray["D"] = "unused object"
	temparray["usethis"] = false;
	dataarray.push(temparray);

	//get count of filteritems
	var filtercount = 0;
	for(a in filteritems) { 
		filtercount++;
	}

	//set a matchcount
	var matchcount = 0;

	//loop through the data items
	for(var d=0; d<dataarray.length; d++) {

		//set a match boolean
		var hasmatch = false;

		//define the row
		var thisrow = dataarray[d]; 


			//loop through the fieldnames array
			for(var f=0; f<fieldnames.length; f++) {
				//loop through the filteritem object
				for(filteritem in filteritems) {
					//set the matchfield - could skip this and just use filteritem but the name makes more sense 
					var matchfield = filteritem;
					//check for a match
					if (matchfield == fieldnames[f]) {
						//if match of field, check for a match of value 
						var matchvalue = filteritems[filteritem];

						if(matchvalue == thisrow[fieldnames[f]]) {

							hasmatch = true;
							matchcount++;

							if (connector != "and") {
								thisrow["usethis"] = true; 
								hasmatch = false;
								break;
							}

						} else { 
							hasmatch = false; 
						}
					}
				}
			}

			//check to see if we have matched all the requested items
			if (matchcount == filtercount) { 
				thisrow["usethis"] = true;
			}

			matchcount = 0;
	}

	for(d=0; d<dataarray.length; d++) { 
		thisrow = dataarray[d];
		if(thisrow["usethis"] == true) { 
			console.log("Item " + thisrow['id'] + " matches the filter criteria.");
		}
	}
}


This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.