-1

Google ビジュアライゼーション API を使用しています。すべての Google ビジュアライゼーション コンストラクトは、2 つの方法のいずれかでインスタンス化できる Javascript オブジェクト google.visualization.DataTable のインスタンスを受け入れることを知っています。

したがって、特定の構造の JSON と特定の属性を使用してデータ テーブルを構築する 2 番目の方法を使用します。問題は、ご覧のとおり、サイクルを使用して特定の数の行を追加したいことです。これを実行しようとすると、上記のエラーが表示されます

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>
          Google Visualization API Sample
        </title>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
          google.load('visualization', '1', {packages: ['table']});
        </script>
        <script>

        function drawVisualization()
        {
         var data = new google.visualization.DataTable
         (
           {
           cols: [
           {id: 'task',  label: 'Task',          type: 'string'},
           {id: 'hours', label: 'Hours per Day', type: 'number'},
            {id: 'E',   label: 'Action',          type:  'string'}
                  ],
                  for (i=0;i<3;i++) 
                  {
                    rows: [ 
                    {c:[{v: 'Work'},     {v: 5},     {v: '<input type="button" value="my button" onclick="alert(\'I was clicked!\');"/>'}]},
                    {c:[{v: 'Eat'},      {v: 2}]}
                           ]                     
                   }
             }         

         );



        visualization = new google.visualization.Table(document.getElementById('table'));
        visualization.draw(data, null);
        }
      google.setOnLoadCallback(drawVisualization);
        </script>
        </head>
        <body>
        <div id="table"></div>
        </body>
        </html>
4

1 に答える 1

1

構文が無効です。オブジェクトを段階的に構築する必要があります。最初に静的部分を作成し、残りを for ループで追加します。

var rawData = {
    cols: [
        {id: 'task',  label: 'Task',          type: 'string'},
        {id: 'hours', label: 'Hours per Day', type: 'number'},
        {id: 'E',     label: 'Action',        type:  'string'}
    ],
    rows: []
};

for (var i = 0; i < 3; i++)  {
    rawData.rows.push({
        c:[{v: 'Work'}, {v: 5}, {v: '<input type="button" value="my button" onclick="alert(\'I was clicked!\');"/>'}]
    });
    rawData.rows.push({
        c:[{v: 'Eat'}, {v: 2}, {v: ''}]
    });
}

var data = new google.visualization.DataTable(rawData);
于 2013-01-06T16:01:12.293 に答える