如何在更改ControlWrapper的状态后从表中正确选择行数据?

时间:2014-04-22 09:57:42

标签: javascript google-visualization

如何在更改ControlWrapper的状态后从表中正确获取selectedRow数据?这里在我的表中选择第一行(使用controlWrapper下拉后的更改状态)返回0,但我需要3,然后才得到正确的行数据。

var myControlWrapper = new google.visualization.ControlWrapper({
    'controlType': 'CategoryFilter',
    'view': {
        'columns': [1, 2, 3, 4, 7, 8]
    },
    'containerId': 'details_category_filter_container',
    'options': {
        'filterColumnLabel': 'P',
        'ui': {
            'matchType': 'any',
            'label': '',
            'caption': 'All',
            'labelStacking': 'horizontal',
            'allowTyping': false,
            'allowMultiple': false,
        }
    },
    'state': {
        'selectedValues': ['A']
    }
});

var table = new google.visualization.ChartWrapper({
    'chartType': 'Table',
    'containerId': 'my_details_table',
    'options': {
        'allowHtml': true,
        'alternatingRowStyle': true,
        'width': '100%',
        'max-height': '600px'
    }
});


// Create the dashboard.
var details_Table_DashBoard = new google.visualization.Dashboard(document.getElementById('my_details_table')).bind(myControlWrapper, table).draw(view);

google.visualization.events.addListener(table, 'ready', function () {

    google.visualization.events.addListener(table.getChart(), 'select', function () {
        var visualizationTable = table.getChart();

        if (visualizationTable.getSelection()[0] !== undefined) {
            var currentRow = visualizationTable.getSelection()[0].row;

            // Here currentRow returns 0,if I select first row after filtered,but it is third row in view(before filtered)  
            var name = dataTable.getFormattedValue(currentRow, 2);
            var time = dataTable.getFormattedValue(currentRow, 3);


        }
    });
});

1 个答案:

答案 0 :(得分:1)

您可以通过调用ChartWrapper来引用Table使用的数据 s getDataTable方法:

[编辑 - 向基础DataTable添加行转换,假设没有使用其他DataView]

google.visualization.events.addListener(table, 'ready', function () {
    google.visualization.events.addListener(table.getChart(), 'select', function () {
        var visualizationTable = table.getChart();

        if (visualizationTable.getSelection()[0] !== undefined) {
            var currentRow = visualizationTable.getSelection()[0].row;
            var dt = table.getDataTable();
            // get the row index in the underlying DataTable/DataView
            // you will need to do this for each DataView that sits between your ChartWrapper and the base DataTable
            rowIndex = dt.getTableRowIndex(currentRow);

            var name = dataTable.getFormattedValue(rowIndex, 2);
            var time = dataTable.getFormattedValue(rowIndex, 3);
        }
    });
});