在我的表格(Handsontable)中,我有四列汽车,A,B和C。
Car和A列的数据是从MySQL数据库加载的。 (如PHP Demo)。
根据Cars的值,通过AJAX从MySQL数据库填充B列的数据。代码如下:
{type: { renderer : function (instance, TD, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
var cr,prc;
cr = instance.getDataAtCell(row, 0);
prc = instance.getDataAtCell(row, 1);
$.ajax({
url: "php/act-go-t.php",
data: {cars: cr, price: prc},
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result[0].tax === null) {
TD.innerHTML = '0.000';
}
else {
TD.innerHTML = res.result[0].tax;
}
},
error: function () {
TD.innerHTML = '0.000';
}
});
}}}
C列是A和B的总和,代码为:
{type : { renderer : function (instance, TD, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
var a,b;
a = instance.getDataAtCell(row, 1);
b = instance.getDataAtCell(row, 2);
TD.innerHTML = +a + +b;
}}}
问题是,尽管该值已加载到B中,但是加法不起作用。如果我将B列的类型设置为除了AJAX以外的numeric(
{type: numeric}
),则添加工作正常。使用AJAX的结果:
+----------+------+-------+--------------+
| Cars | A | B | C |
+----------+------+-------+--------------+
| Nissan | 20 | 10 | 20 |
| Honda | 5 | 6 | 5 |
+----------+------+-------+--------------+
没有AJAX的结果:
+----------+------+-------+-------------+
| Cars | A | B | C |
+----------+------+-------+-------------+
| Nissan | 20 | 10 | 30 |
| Honda | 5 | 6 | 11 | -
+----------+------+-------+-------------+
有人可以告诉我我是否想念东西吗?
最佳答案
在您的情况下,TD.innerHTML = res.result[0].tax;
仅用于显示数据,但是不会将数据插入到datamap中。
您可以尝试为该单元格设置一个id
,并通过jquery求和并求和。所以代码看起来像这样:
{type: { renderer : function (instance, TD, row, col, prop, value, cellProperties) {
var cr,prc;
cr = instance.getDataAtCell(row, 0);
prc = instance.getDataAtCell(row, 1);
$.ajax({
url: "php/act-go-t.php",
data: {cars: cr, price: prc},
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result[0].tax === null) {
TD.innerHTML = '0.000';
}
else {
TD.innerHTML = res.result[0].tax;
}
},
error: function () {
TD.innerHTML = '0.000';
}
});
arguments[5] = TD.innerHTML;
TD.id = row+'_'+col;
Handsontable.TextCell.renderer.apply(this, arguments);
}}}
和
{type : { renderer : function (instance, TD, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
var a,b;
a = $('#'+row+'_1').text();
b = $('#'+row+'_2').text();
TD.innerHTML = +a + +b;
}}}
关于javascript - 使用Handsontable中的AJAX进行自定义单元格渲染时,计算无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40439773/