如何刷新数据表列数据

How to refresh datatable column data

本文关键字:数据表 数据 刷新 何刷新      更新时间:2023-09-26

我使用数据表插件,将表作为数据源。当用户在输入中输入一些数字时,我必须通过 javascript 更改一些列值(此输入也是表的一部分,我必须将此值导出到)。它运行良好,但是当我想导出表时,使用 javascript 更改的列不会显示在导出的文件中。

我认为问题是我没有刷新数据表插件。这是对的吗?如果是,如何刷新数据源?如果没有,问题是什么,我该如何解决?

我尝试了refresh()方法(var dataTableObject = $(this).parents('table').dataTable(); dataTableObject.refresh();)和$(this).parents('table').dataTable()但不起作用。

编辑:这是我更改单元格值的方法:

$item.find('input.quantity-coefficient').each(function (i, d) {
        $(d).off('change').on('change', function () {
            var multiplier = $(this).val();
            //var dataTableObject = $(this).parents('table').dataTable();
            $($(this).data("row-price-info-class")).each(function (i, d) {
                var priceContainer = $(d);
                var price = parseFloat(priceContainer.data('price-value'));
                var priceInfoRatioContainer = $(priceContainer.data("ratio-input-class"));
                if (multiplier * price == 0) {
                    priceInfoRatioContainer.closest("td").addClass("no-euro");
                    priceInfoRatioContainer.html(" ");
                } else {
                    priceInfoRatioContainer.closest("td").removeClass("no-euro");
                    priceInfoRatioContainer.html(localizeNumber(multiplier * price));
                }
            });
            //TODO redrow the data 
            $(this).parents('table').dataTable().fnDraw();;
        });
    });

您可以使用 fnDraw() 刷新数据表。在您的示例中:

$(this).parents('table').dataTable().fnDraw();

谢谢你们,但我想我在这篇文章中找到了解决方案: jquery 数据表 - 更改单元格的值而不仅仅是显示值

当我第一次尝试时,问题是我从 1 而不是从 0 开始行号。

因此,对于可能遇到此问题的其他人,我必须使用这样的东西:

(this).parents('table').dataTable().fnUpdate("new value", 0, 2)

更新单元格值。实际上这不是我要求的,因为这不是刷新所有表格,但现在这对我来说是一个解决方案。