Set column width for google.visualization.DataTable

寵の児 提交于 2020-01-02 08:33:25

问题


I want to change the width of the columns in google.visualization.DataTable but, I am not sure how to do it.

Fiddler: https://jsfiddle.net/dgbh6sL0/1/

HTML

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['table']}]}"></script>
<div id="table_div"></div>

JS

google.setOnLoadCallback(drawTable);

  function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('boolean', 'Full Time Employee');
    data.addRows([
      ['Mike',  {v: 10000, f: '$10,000'}, true],
      ['Jim',   {v:8000,   f: '$8,000'},  false],
      ['Alice', {v: 12500, f: '$12,500'}, true],
      ['Bob',   {v: 7000,  f: '$7,000'},  true]
    ]);

    var table = new google.visualization.Table(document.getElementById('table_div'));

    table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
  }

Expectation

  • 'Name' column width should be 300px;
  • 'Full Time Employee' column width should be 100px;

Any suggestion / help is appreciated.

EDIT

Based on the suggestion, here is the applied solution: https://jsfiddle.net/dgbh6sL0/2/


回答1:


You can do this like:

data.setProperty(0, 0, 'style', 'width:300px');

Also, you need to "allowHTML" in "draw" like:

table.draw(data, {showRowNumber: true, width: '100%', height: '100%',allowHtml: true});



回答2:


On a related note, you can additionally override the crazy one-line-header that ends up extremely long by overriding the white-space css for header cells:

table.draw(data, {showRowNumber: true, width: '100%', height: '100%',
    allowHtml: true, cssClassNames: {headerCell: 'normal-whitespace'}});

Then in your CSS add:

.normal-whitespace {
  white-space: normal;
}

For example https://jsfiddle.net/3j10g8n9/



来源:https://stackoverflow.com/questions/33019300/set-column-width-for-google-visualization-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!