最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort Column in Table - Google Chart Tools - Stack Overflow

programmeradmin3浏览0评论

The following is a table for the Google Charts API. I'm trying to sort the "Numbers" Column descending. Anyone know how to do this?

<script type="text/javascript" src=""></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">
    function drawVisualization() {
      // Create and populate the data table.
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Names');
      data.addColumn('number', 'Numbers');
      data.addRows(3);
      data.setCell(0, 0, 'Name 1');
      data.setCell(1, 0, 'Name 2');
      data.setCell(2, 0, 'Name 3');
      data.setCell(0, 1, 1);
      data.setCell(1, 1, 2);
      data.setCell(2, 1, 3);



      visualization = new google.visualization.Table(document.getElementById('table'));
      visualization.draw(data, null);
    }


    google.setOnLoadCallback(drawVisualization);
    </script>

    <div id="table"></div>

The following is a table for the Google Charts API. I'm trying to sort the "Numbers" Column descending. Anyone know how to do this?

<script type="text/javascript" src="http://www.google./jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">
    function drawVisualization() {
      // Create and populate the data table.
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Names');
      data.addColumn('number', 'Numbers');
      data.addRows(3);
      data.setCell(0, 0, 'Name 1');
      data.setCell(1, 0, 'Name 2');
      data.setCell(2, 0, 'Name 3');
      data.setCell(0, 1, 1);
      data.setCell(1, 1, 2);
      data.setCell(2, 1, 3);



      visualization = new google.visualization.Table(document.getElementById('table'));
      visualization.draw(data, null);
    }


    google.setOnLoadCallback(drawVisualization);
    </script>

    <div id="table"></div>
Share Improve this question asked Mar 2, 2012 at 5:11 GeorgGeorg 6482 gold badges9 silver badges24 bronze badges 1
  • 1 developers.google./chart/interactive/docs/gallery/table Search for event sort Look here for the playground code.google./apis/ajax/playground/… – Ron van der Heijden Commented May 22, 2012 at 11:12
Add a ment  | 

1 Answer 1

Reset to default 6

Yes. just add the following line below your data def., it will sort descending on number, and then ascendsing on name.

data.sort([{column: 1, desc:true}, {column: 0}]);

oh, you could also use this:

data.addRow(['Name 1',1]);
data.addRow(['Name 2',2]);
data.addRow(['Name 3',3]);

regards, Halma

发布评论

评论列表(0)

  1. 暂无评论