【问题标题】:Google Visualization dataview.setRows(): invalid row index errorGoogle Visualization dataview.setRows():无效的行索引错误
【发布时间】:2015-06-30 01:36:09
【问题描述】:

在我的代码中,我使用 Google Visualization 创建了一个数据视图,其中包含 300 多行。当我尝试通过指定确切的行索引来过滤行时,控制台会产生错误:

SCRIPT5022:行索引 4 无效。应在 [0-380] 范围内。

这里是 JavaScript:

function filter(t) {
    var view = new google.visualization.DataView(data);

    if (t.colQuery.value) {
        view.setColumns(t.colQuery.value.split('/'));
    }
    if (t.rowQuery.value) {
        view.setRows(t.rowQuery.value.split('/'));
    }
    chart.draw(view, {
        allowHtml: true
    });
}

这是 HTML:

<div id='form'>
    <form id='serverForm'>Enter server name(s):
        <!-- textbox and button for server name(s) -->
        <textarea id='text' onkeypress='checkKey(event, this.form)'></textarea>
        <button onclick='drawChart(this.form); return false;'>Table/Refresh</button>
    </form>
    <form id='queryForm'>Enter column query:
        <!-- textbox and button for queries -->
        <input type='text' id='colQuery'></input>Enter row query:
        <input type='text' id='rowQuery'></input>
        <button onclick='filter(this.form); return false;'>Filter</button>
    </form>
</div>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    当你接受输入并用正斜杠分割时,你会得到一个字符串数组。但是,setRows()setColumns() 需要整数数组(索引)。

    您需要先将它们映射成整数。只需稍加修改,您就可以这样做:

    更改自:

    view.setRows(t.rowQuery.value.split('/'));
    

    至此:

    view.setRows(t.rowQuery.value.split('/').map(function(v){ return parseInt(v) }));
    

    【讨论】:

    • 不起作用。它给了我同样的错误,除了它说 NaN 而不是 4。
    • 你可以试试更新的代码吗?无论如何,只是想知道,您在输入的末尾是否有/
    • 没关系。我找到了解决办法:把map()里面的函数换成Number,所以view.setRows(t.rowQuery.value.split('/').map(Number);感谢帮助。
    • 很高兴它提示您如何修复它。
    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多