0
votes

This is my table
Type Time Month
A Yes Jan
A Yes Feb
A No Mar
A Yes Apr

This is what I get in return:
Month YES NO
Jan 1 0
Feb 2 0
Mar 0 3
Apr 0 4

What I want is for my count of to look like this:

Month YES NO
Jan 1 0
Feb 1 0
Mar 0 1
Apr 1 0

Here is my script. I'm racking my head and any help is greatly appreciated. Thanks

<Script>
function processResult(xData, status) {


    var Month = [];
    var i = 0;  
var TableRow = "<table>"; 

    $(xData.responseXML).find("z\\:row").each(function() {
    var sType = $(this).attr("ows_Title");
    var sTime = $(this).attr("ows_Time");
    var sMonth = $(this).attr("ows_Month");
    i = i + 1;


if(Month[sMonth]==undefined)
{
Month[sMonth] = [0,0,0,0];
}
if(Month[sMonth]!=undefined)
{   
if (sTime == "Yes" && sType == "A"){Month[sMonth][0] = i}
if (sTime == "No" && sType == "A"){Month[sMonth][1] = i}
}


});
 var key = "";
    for(key in Month){
           TableRowHtml += "<TR><td style='text-align: Left'> " + key +" </td><td>" + Month[key][0] + "</td><td> "+ Month[key][1] + "</td><TR>";
            }

TableRowHtml +="</Table>";
        $("#tasksUL").append(TableRowHtml);
 }
</Script>

Thanks

2
SharePoint 2007 or 2010?Gabriel Mongeon

2 Answers

0
votes

I think you're looking for this:

if (sTime == "Yes" && sType == "A"){Month[sMonth][0] = 1}
if (sTime == "No" && sType == "A"){Month[sMonth][1] = 0}
0
votes

Try moving the var i; definition into the each function; right now you're using the same i for each row, thus it's cumulative. Beware the closure.

(Wait, what even is i? It doesn't seem to do anything useful, if you're not trying to keep it cumulative.)

Also not really sure how you're creating a well-formed table, seems like you're missing the first <table> tag. In any case, see below (untested).

<Script>
function processResult(xData, status) {
    var Month = [];

    $(xData.responseXML).find("z\\:row").each(function() {
        var sType = $(this).attr("ows_Title");
        var sTime = $(this).attr("ows_Time");
        var sMonth = $(this).attr("ows_Month");
        i = i + 1;

        if (Month[sMonth] == undefined) {
            Month[sMonth] = [0,0,0,0];
        } else if (sType == "A") {
            var which = sTime == "Yes" ? 0 : 1;
            Month[sMonth][which] = i;
        }
    });

    var table = "<table>"; 
    for (var key in Month) {
        table += "<TR><td style='text-align: Left'> " + key +" </td>"
               + "<td>" + Month[key][0] + "</td><td> "+ Month[key][1] + "</td><TR>";
    }
    table += "</table>";

    $("#tasksUL").append(table);
}
</Script>