This project has moved and is read-only. For the latest updates, please go here.

Parsing multi-choice fields with jQuery

Nov 9, 2010 at 6:58 PM

I'm using SPServices.GetListItems.  One field is a multi-choice field that returns a value like:   ;#Men;#Women;#Pregnant Women;#Children;#.  What I would like to do is turn that into a table representation so if the list like:


Men      Women        Pregnant Women      Children


               x                       x                       

               x                                                   x

x             x                       x                           x


So - for each row in the list, I look at the choice field and for each choice selected, I want to put an X in the table field.  Any suggestions on how to do this?  I have it written in C# but I am just learning jQuery.

If this is the wrong forum, then please disregard.

Nov 9, 2010 at 9:59 PM

I figured out something - but I'm sure someone with more jQuery experience can show me how accomplish this easier with jQuery.  This is the function I wrote - which is called as my completefunc.


	function WriteDataTable(xData, Status)
	    var columns = ["Men","Women","Pregnant Women","Children"];
		var tableOut = "<table id='resultsgrid'><tr><th rowspan='2'>Name</th><th colspan='4'>Gender</th><th rowspan='2'>Frequency</th></tr>";
		tableOut+="<tr><th>Men</th><th>Women</th><th>Pregnant Women</th><th>Children</th></tr>";
		var alt = true;
		    alt = !alt;
			var rowHtml = "<tr" + ((alt) ? " class='alt'" : "") + "><td>" + $(this).attr("ows_Title") + "</td>";
			var gender = $(this).attr("ows_Gender");
			for(i=0; i<columns.length; i++)
				rowHtml += "<td class='gendergrid'>" + ((gender.indexOf("#"+columns[i]) > 0) ? "&#149;" : "&nbsp;") +  "</td>";
			rowHtml += "<td>"+ $(this).attr("ows_Frequency") +"&nbsp;</td></tr>";
		tableOut +="</table>";

Nov 10, 2010 at 3:22 AM

What you've come up with is perfectly fine! (Assuming it does what you want.) You could, of course, spin through the data to determine the unique values for your multi-select and save them in an array. That would make this continue to work if there are additional values added.