2013-10-21 165 views
1

我正在使用Handsontable插件,当用户更改单元格中的值时,它应该变成黄色,以便他们可以跟踪已更改的内容。在这种情况下,黄色是类.changeInput。棘手的部分是当你双击单元格来改变它,这成为一个textarea,不再是一个td。有任何想法吗?提前致谢。如何更改Handsontable中更改的单元格的颜色?

http://jsfiddle.net/PAH5J/

jQuery的

$("textarea.handsontableInput").change(function(){ 
    //$(this).find(td).addClass('changeInput'); 
    $('.htNumeric .current').addClass('changeInput'); 
}); 
+0

你需要使用事件委派,也许“KEYUP”,而不是改变[HTTP://的jsfiddle。 net/PAH5J/4 /](http://jsfiddle.net/PAH5J/4/) –

+0

@AbrahamUribe我需要更改的TD保持黄色此外,这似乎只影响textarea。 – triplethreat77

+0

只适用于最后编辑的单元格[http://jsfiddle.net/PAH5J/6/](http://jsfiddle.net/PAH5J/6/) –

回答

1

到有变化,你可以创建自定义渲染器和适用于仅当数据( “变”)存在这样

//Custom renderer add class if the element have the data "change" 
var myRenderer = function (instance, td, row, col, prop, value, cellProperties) { 
    Handsontable.TextCell.renderer.apply(this, arguments); 
    if($(td).data("change")){ 
     $(td).addClass('changeInput'); 
    } 
};  
$('#example').handsontable({ 
data: data, 
minSpareRows: 1, 
colHeaders: true, 
contextMenu: true, 
    cells: function (row, col, prop) {//set the new renderer for every cell 
    return {type: {renderer: myRenderer}}; 
    } 
}); 
//afterChange get every cell and add class and data 
$('#example').handsontable('getInstance').addHook('afterChange', function(changes) { 
    var ele=this; 
    $.each(changes, function (index, element) { 
      $(ele.getCell(element[0],element[1])).addClass('changeInput').data("change",true); 
}); 

$("#example").on("keyup","textarea.handsontableInput",function(){ 
$(this).addClass('changeInput'); 
}).on("blur","textarea.handsontableInput",function(){ 
$(this).removeClass('changeInput'); 
});  
每一个细胞标记

http://jsfiddle.net/PAH5J/8/
编辑
将突出显示的区域,您可以使用cellProperties代替。数据()这样的

var myRenderer = function (instance, td, row, col, prop, value, cellProperties) { 
    Handsontable.TextCell.renderer.apply(this, arguments); 
    if (cellProperties.change) {//check for new property change in the cell 
     $(td).addClass('changeInput'); //add the changeInput class to the actual td 
    } 
};  
$('#example1').handsontable('getInstance').addHook('afterChange', function(changes) { 
    var ele=this; 
    $.each(changes, function (index, element) { 
     //add the changeInput class to the actual td 
     $(ele.getCell(element[0],ele.propToCol(element[1]))).addClass('changeInput') 
     //get the cell properties and create a new one "change"  
     //to check in the renderer 
     ele.getCellMeta(element[0],ele.propToCol(element[1])).change=true; 
    });  
}); 
+0

我犯了一个错误,没有给出我正在使用的完整示例。而不仅仅是数据:数据,我从一个函数中获取数据... function getCarData()http://jsfiddle.net/D4Kx3/并且不能得到这个工作 – triplethreat77

+1

[http://jsfiddle.net/D4Kx3 /1/](http://jsfiddle.net/D4Kx3/1/)但你不应该有两个同名的“价格”列 –

+0

工作很好,谢谢。最后一件事,我的复选框不再被正确读取。他们表现为假。 http://jsfiddle.net/D4Kx3/2/ – triplethreat77