首页 > 技术文章 > handsontable 问题

wang-jing 2015-08-20 09:58 原文

碰到问题了,去官网上找community:http://docs.handsontable.com/0.16.1/tutorial-quick-start.html

 

1、

描述:把handson table放在bootstrap tab中,首次没有激活,table没有正常显示。

原因:这种情况下,table invisible,所以不会调用render(),不会正常显示

解决办法:

$('a[href="#data"]').on('shown.bs.tab', function (e) {
	hsd.hot.render();
});

另一个问题

描述:在上面的基础上,fixedRowsTop无效

解决办法:添加这一句,重新更新。

hsd.hot.updateSettings({fixedRowsTop : 1});

注:直接更新就可,不用render()也行。

 

2、

频繁切换表格内容是,使用updateSetting,这样就不会出现数据显示异常。

 

3、

minSpareRows:1,
minSpareCols:1

在最后一行输入内容,enter后,会自动增加一行。  

 

4、

给某行设置背景颜色:这一类问题都这样解决

cells:function(row, col, prop){
    var cellProperties = {};
    if(row === 0){
        cellProperties.renderer = firstRowRenderer;
    }
    return cellProperties;
} 

var firstRenderer = function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.style.background = '#eee';
} 

 

5、

建空表是data:[[]]; 

 

6、

滚动到指定行

hsd.hot.selectCell(1, 0);
hsd.hot.deselectCell();

找了好久,原来确实没有默认的方法,只能自己解决了,hsd.hot是我自己定义的表 

 

7、copy,paste,cut

demo--contextMenu中有例子,但是jsfiddle中,copy和paste显示有点不正常;cut,没有提到

 

8、

firefox下鼠标滚动缓慢:
github上回复,是使用其他三种方式:滚动条,键盘,右键后能快一点。https://github.com/handsontable/handsontable/issues/2520
或者是修改firefox的配置:选项--高级--常规--去掉(使用平滑滚动)。
 
9、
updateSettings:一个很奇怪的问题
场景:为了区分是哪种数据结构,而进行的更新。用A建一表,有cells属性,用B更新此表,cells中使用的是A的数据
//----------初始化开始----------------------
//获取数据
$.get('json/handsontable.json', function(data){
	// hsd.data = data;
	hsd.init(data);
});

// 更新
$('#update').on('click', function(){
	$.get('json/handsontable1.json', function(data){
		// hsd.data = data;
		// hsd.init();
		hsd.hot.updateSettings({
			data:data
		})
	});
})

//----------初始化结束----------------------

hsd.con = $('#con');
hsd.init = function(data){
     // updateSettings 时不会执行这一句,
     console.log('sfp')  
hsd.hot = new Handsontable(hsd.con[0], { data: data, rowHeaders: true, colHeaders: ['SLE', 'Control'], contextMenu: true, columnSorting: true, manualColumnResize: true, fixedRowsTop: 1, fixedColumnsLeft: 1, sortIndicator: true, cells: function(row, col, prop){
              // updateSetting时 会执行这一句 if(row === 0 && col === 0){ console.log(data[0][0]); } } }); };

解决办法:init不传参数,获取到B之后,执行hsd.data = B, init中得数据用hsd.data

10、如何区分自动创建的row和通过contextmenu创建的row:http://jsfiddle.net/2oc2hb3e/18/

var flag = false;
afterRender: flag = true;
afterCreateRows: if(flag){ // }
如果有mincols,removeRows,会自动CreateRows,所以需要在removeRows:flag=false
注意:update和createtable时,都先要flag=false

  

  

 

 

 

  

 

推荐阅读