1

我有以下代码:

 //creates a DataTable. don't worry about this. this works
var projectData = createDataView(json);

// Create and draw the visualization.
var table = new google.visualization.ChartWrapper({
    chartType: "Table",
    containerId: "projectChart"
});

// add user filter
var userFilter = new google.visualization.ControlWrapper({
    'controlType': 'StringFilter',
    'containerId': 'userFilter',
    'options': {
        'filterColumnLabel': 'User'
    }
});

var dashboard = new google.visualization.Dashboard(
            document.getElementById('projectChartContainer'))
        .bind(userFilter, table);

function drawTable() 
{
    dashboard.draw(projectData, {
        'allowHtml': true,
        showRowNumber: true,
        title: "Project Data",
        width: middleWidth
    });


    // google visualization styles
    $(".google-visualization-table-td-number").addClass("small");
    $(".google-visualization-table-td-center").addClass("eighty");
}

// set width 
middleWidth = $("#contentMiddle").width();
drawTable();

// if user resized the browser, change the width and redraw.  
//this is ugly, but the only way i could find. 100% was buggy
$(window).resize( function() {
    middleWidth = $(window).width()-142;
    drawTable();
});

// add a change listener to table
google.visualization.events.addListener(table, 'ready', function() {                    
    // get hour totals
    var workedHours = 0;
    var billedHours = 0;

    // THIS IS WHERE I NEED HELP!!!!!       
    /*var dataLength = table.getNumberOfRows();
    for (var x = 0; x < dataLength; x++)
    {
        workedHours += table.getValue(x, 7);
        billedHours += table.getValue(x, 9);
    }
    $("#totalWorked").text(workedHours);
    $("#totalBilled").text(billedHours);
    $("#totalsContainer").show();*/

    console.log(projectData.getValue(1,1));
});

就表格显示和过滤而言,一切正常。我需要的是一种获取新过滤表的列总数的方法。我可以使用 projectData.getValue() 第一次获取值,但无论过滤掉什么,它都会返回相同的值。我可以在新绘制的表格中没有行,它将显示与最初相同的值。我并不感到惊讶,因为我实际上只是在更改表格并听取它的重绘。但我无法从表中获取任何值。我上面的表函数(即...table.getNumberOfRows())不存在。这正是我希望发生的事情。关于如何获取过滤表的新列值的任何想法?

4

1 回答 1

1

我的建议是先将表数据转换为数据表var tableData = table.getDataTable();

该数据表将被过滤和排序,如表中所示。然后,您可以在 tableData 上运行 for() 循环:

for (var x = 0; x < tableData.getNumberOfRows(); x++)
{
    workedHours += tableData.getValue(x, 7);
    billedHours += tableData.getValue(x, 9);
}
$("#totalWorked").text(workedHours);
$("#totalBilled").text(billedHours);
$("#totalsContainer").show();
于 2013-02-06T01:22:45.737 回答