2

我正在使用 Google Charts 来显示分页表。

当我加载页面时,表格的宽度非常小,并且仅在第一次分页后才调整为适当的宽度。

我尝试设置显式宽度并检查表格元素,宽度属性设置为适当的大小,但表格在加载时仍然很小。

代码并没有什么特别之处,和操场上的例子一样:

https://code.google.com/apis/ajax/playground/?type=visualization#table_paging

有没有办法强制表格调整到设定的尺寸?

谢谢

4

4 回答 4

2

这对我来说很好:

$(window).on("resize", function () {
    draw();
}).resize();

你必须设置表格的宽度......

table.draw(data, {
 width:'100%',
 ...
});
于 2013-06-10T14:09:05.517 回答
0

脚本:

<script type="text/javascript">
var visualization;
var data;

var options = {'showRowNumber': true};
function drawVisualization() {
  // Create and populate the data table.
  var dataAsJson =
  {cols:[
    {id:'A',label:'Name',type:'string'},
    {id:'B',label:'Height',type:'number'},
    {id:'C',label:'Coming',type:'boolean'},
    {id:'D',label:'Time',type:'timeofday'}],
  rows:[
    {c:[{v:'Dave'},{v:159.0,f:'159'},{v:true,f:'TRUE'},{v:[12,25,0,0],f:'12:25:00'}]},
  {c:[{v:'Peter'},{v:185.0,f:'185'},{v:false,f:'FALSE'},{v:[13,15,0,0],f:'13:15:00'}]},
  {c:[{v:'John'},{v:145.0,f:'145'},{v:true,f:'TRUE'},{v:[15,45,0,0],f:'15:45:00'}]},
  {c:[{v:'Moshes'},{v:198.0,f:'198'},{v:true,f:'TRUE'},{v:[16,32,0,0],f:'16:32:00'}]},
  {c:[{v:'Karen'},{v:169.0,f:'169'},{v:true,f:'TRUE'},{v:[19,50,0,0],f:'19:50:00'}]},
  {c:[{v:'Phil'},{v:169.0,f:'169'},{v:false,f:'FALSE'},{v:[18,10,0,0],f:'18:10:00'}]},
  {c:[{v:'Gori'},{v:159.0,f:'159'},{v:true,f:'TRUE'},{v:[13,15,0,0],f:'13:15:00'}]},
  {c:[{v:'Bill'},{v:155.0,f:'155'},{v:false,f:'FALSE'},{v:[7,40,48,0],f:'7:40:48'}]},
  {c:[{v:'Valery'},{v:199.0,f:'199'},{v:true,f:'TRUE'},{v:[6,0,0,0],f:'6:00:00'}]},
  {c:[{v:'Joey'},{v:187.0,f:'187'},{v:true,f:'TRUE'},{v:[5,2,24,0],f:'5:02:24'}]},
  {c:[{v:'Karen'},{v:190.0,f:'190'},{v:true,f:'TRUE'},{v:[6,14,24,0],f:'6:14:24'}]},
  {c:[{v:'Jin'},{v:169.0,f:'169'},{v:false,f:'FALSE'},{v:[5,45,36,0],f:'5:45:36'}]},
  {c:[{v:'Gili'},{v:178.0,f:'178'},{v:true,f:'TRUE'},{v:[6,43,12,0],f:'6:43:12'}]},
  {c:[{v:'Harry'},{v:172.0,f:'172'},{v:false,f:'FALSE'},{v:[6,14,24,0],f:'6:14:24'}]},
  {c:[{v:'Handerson'},{v:175.0,f:'175'},{v:true,f:'TRUE'},{v:[6,57,36,0],f:'6:57:36'}]},
  {c:[{v:'Vornoy'},{v:170.0,f:'170'},{v:true,f:'TRUE'},{v:[13,12,0,0],f:'13:12:00'}]}]};
  data = new google.visualization.DataTable(dataAsJson);

  // Set paging configuration options
  // Note: these options are changed by the UI controls in the example.
  options['page'] = 'enable';
  options['pageSize'] = 10;
  options['pagingSymbols'] = {prev: 'prev', next: 'next'};
  options['pagingButtonsConfiguration'] = 'auto';

  // Create and draw the visualization.
  visualization = new google.visualization.Table(document.getElementById('table'));
  draw();
}

function draw() {
  visualization.draw(data, options);
}


google.setOnLoadCallback(drawVisualization);

// sets the number of pages according to the user selection.
function setNumberOfPages(value) {
  if (value) {
    options['pageSize'] = parseInt(value, 10);
    options['page'] = 'enable';
  } else {
    options['pageSize'] = null;
    options['page'] = null;  
  }
  draw();
}

// Sets custom paging symbols "Prev"/"Next"
function setCustomPagingButtons(toSet) {
  options['pagingSymbols'] = toSet ? {next: 'next', prev: 'prev'} : null;
  draw();  
}

function setPagingButtonsConfiguration(value) {
  options['pagingButtonsConfiguration'] = value;
  draw();
}

</script>

html:

<div id="table" style="width:100%;"></div>

我说的是上面的 div 容器。如果在您的 css 文件中,有表格元素的 css 代码,它会覆盖 google 生成的表格 css。

您的自定义表格 css 代码也会影响 google 表格。

于 2012-12-06T09:18:51.190 回答
0

现在我只是用 $.width() 再次设置宽度,

但这似乎不是最佳解决方案

于 2012-12-06T08:44:04.187 回答
0

The only solution that worked was giving the 100% width to the table via css:

.google-visualization-table-table{width: 100%}

Givin the width to the table in the option parameter at initalisation did only work with pixel values. And the other proposed solutions seemed not very elegant.

于 2014-12-04T07:56:05.167 回答