简体   繁体   English

使用Handsontable中的AJAX进行自定义单元格渲染时,计算无法正常工作

[英]Calculation is not working with custom Cell Rendering with AJAX in Handsontable

In my table(Handsontable) I have four columns Cars , A , B and C . 在我的表(Handsontable)中,我有四列CarsABC。 Data for Cars and A columns are loaded from MySQL database. CarA列的数据是从MySQL数据库加载的。 (like PHP Demo ). (如PHP Demo )。

Data of Column B is populated from MySQL database via AJAX depending on the value of Cars . 根据Cars的值,通过AJAX从MySQL数据库填充B列的数据。 The code is as follows: 代码如下:

{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';
                        }
                    });                 
                }}}

The C column is the SUM of A and B and the code is: C列是AB总和 ,代码为:

{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;
                }}}

The problem is although the value is loaded in B but the addition is not working. 问题是,尽管该值已加载到B中,但是加法不起作用。 If I set the type of B column to numeric( {type: numeric} ) except the AJAX, the addition is working fine. 如果我将B列的类型设置为除了AJAX以外的numeric( {type: numeric} ),那么添加工作正常。

Result with AJAX: 使用AJAX的结果:

 +----------+------+-------+--------------+ | Cars | A | B | C | +----------+------+-------+--------------+ | Nissan | 20 | 10 | 20 | | Honda | 5 | 6 | 5 | +----------+------+-------+--------------+ 

Result without AJAX: 没有AJAX的结果:

 +----------+------+-------+-------------+ | Cars | A | B | C | +----------+------+-------+-------------+ | Nissan | 20 | 10 | 30 | | Honda | 5 | 6 | 11 | - +----------+------+-------+-------------+ 

Can anybody please tell me if I am missing something? 有人可以告诉我我是否想念东西吗?

In your case TD.innerHTML = res.result[0].tax; 在您的情况下TD.innerHTML = res.result[0].tax; is only for displaying data, however it not inserts data into datamap. 仅用于显示数据,但不将数据插入到datamap中。

You may try to set a id for that cell and get the value of that by jquery and summing them up. 您可以尝试为该单元格设置一个id ,并通过jquery求和并求和。 So the codes will look something like that: 所以代码看起来像这样:

{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);

                }}}

And

{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;
                }}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM