执行 ajax 调用时丢失 mCustomScrollbar 效果

Losing mCustomScrollbar effect when performing an ajax call

我正在使用 mCustomScrollbar 替换 div 标签中的默认滚动条,其中包含我使用 javascript 绘制的 table 以帮助我在执行时重新加载它ajax 来电,这是我的 HTML 代码:

<!-- the div that will contain the table-->
<div id="countriesTable" class="customScroll" data-mcs-theme="dark">
</div>

这是在 table 中加载数据并在 div

中绘制数据的函数代码
function reloadTable(data, id) {
        var str = '<table class="table"><thead>' +                    
                '<tr><th> Column1 </th>' +
                '<th> Column2 </th>' +
                '<th> Column3 </th>' +
                '<th> Column4 </th></tr></thead><tbody>';
        for (var property in data) {
            if (data.hasOwnProperty(property)) {                    
                str += '<tr>'
                str += '<td>' + data[property][0] + '</td>' +
                '<td>' + data[property][1] + '</td>' +
                '<td>' + data[property][2] + '</td>' +
                '<td>' + data[property][3] + '</td></tr>';
            }
        }
        str += '</tbody></table>';
        $(id).html(str);
    }

当然还有调用加载数据的函数以及应用自定义滚动条效果的函数:

reloadTable(myData, '#countriesTable');
$(".customScroll").mCustomScrollbar();

当页面加载时,div 成功获得了自定义滚动条,但是当我执行 ajax 调用以将数据重新加载到我的 table 时,我再次绘制了它使用 reloadTable 函数我失去了滚动条效果。 我试图回忆起 ajax success 函数中的 mCustomScrollbar 但没有成功。

我认为您需要像这样删除当前的 mCustomScrollbar:

$('.customScroll').mCustomScrollbar("destroy")
$('#countriesTable').html("")
reloadTable(myData, '#countriesTable');
$(".customScroll").mCustomScrollbar();