在我的表(Handsontable)我有四辆汽车,一,乙和Ç。对于数据汽车和一列从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列的类型设置为{type: numeric}
除了AJAX之外的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
为该单元格设置a ,并通过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;
}}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句