Hüs*_*LIM 6 javascript jquery hook handsontable
我正在使用handontable js插件.我想在afterChange钩子中使用getCellMeta函数但不能正常工作.我在使用函数outChange hook后,函数正常工作.但是没有在afterChange挂钩工作.
var container = document.getElementById('t1'),
options = document.querySelectorAll('.options input'),
table,
hot;
hot = new Handsontable(container, {
autoWrapRow: true,
startRows: 81,
startCols: 206,
autoColumnSize : true,
stretchH: 'all',
afterChange : function(change,source) {
if (source === 'loadData') {
return;
}
var test = this.getCellMeta(change[0],change[1]); // not working, not return "id" meta
console.log(test);
}
});
$.ajax({
url: 'path',
type: 'GET',
dataType: 'json',
success: function (res) {
var data = [], row, pc = 0;
for (var i = 0, ilen = hot.countRows(); i < ilen; i++)
{
row = [];
for (var ii = 0; ii<hot.countCols(); ii++)
{
hot.setCellMeta(i,ii,'id',res[pc].id);
row[ii] = res[pc].price;
if(pc < (res.length-1)) {
pc++;
}
}
data[i] = row;
}
hot.loadData(data);
}
});
var test = this.getCellMeta(0,0); // is working, return "id" meta
console.log(test);
Run Code Online (Sandbox Code Playgroud)
变化后如何获得细胞元?
谢谢.
你就快到了,回调中只有一个小错误:文档afterChangechanges指定回调的第一个参数 ( ) 是:
包含有关每个已编辑单元格的信息的二维数组
[[row, prop, oldVal, newVal], ...]。
所以,有两个重要的细节:
hot.getCellMeta(change[0][0],change[0][1])例如hot和 notthis因为afterChange回调函数是从不同的上下文(即在不同的对象上)调用的,所以this不是正确的调用目标,请参阅“this”关键字如何工作?读取整个更改数组的示例:
var hot = new Handsontable(container, {
/* rest of init... */
afterChange : function(changes,source) {
console.log("Changes:", changes, source);
if (changes) {
changes.forEach(function(change) {
var test = hot.getCellMeta(change[0],change[1]);
console.log(test.id, test); // 'id' is the property you've added earlier with setMeta
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
请参阅演示小提琴,打开 JS 控制台,在表中进行任何更改。
| 归档时间: |
|
| 查看次数: |
874 次 |
| 最近记录: |