设置google.visualization.DataTable的列宽

时间:2015-10-08 14:55:14

标签: javascript jquery css google-visualization

我想更改google.visualization.DataTable中列的宽度,但是,我不知道该怎么做。

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%'});
  }

期望

  • &#39;名称&#39;列宽应为300px;
  • &#39;全职员工&#39;列宽应为100px;

感谢任何建议/帮助。

修改

根据建议,以下是应用的解决方案:https://jsfiddle.net/dgbh6sL0/2/

2 个答案:

答案 0 :(得分:4)

您可以这样做:

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

另外,你需要在“draw”中“allowHTML”,如:

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

答案 1 :(得分:1)

在相关的说明中,您可以通过覆盖标题单元格的white-space css来覆盖极端冗长的疯狂单行标题:

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

然后在你的CSS中添加:

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

例如 https://jsfiddle.net/3j10g8n9/