jquery 错误Datable id=example无法重新初始化数据表?

taor4pac  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(107)

我在剃刀页面asp.net核心工作。我面临错误Datable id=example无法重新初始化数据表?
那么如何解决这个错误呢?
更改选择选项打印机服务器时发生错误
我需要在更改时选择选择选项打印机服务器,然后隐藏或删除表与分页选项first ,next,previous,last etc

<script type="text/javascript">
        $(document).ready(function () {
     
            
           
        
            $('#PrintServer-select').change(function () {
                var printserverId = $(this).val();
               
                if (printserverId) {
                    console.log(printserverId);
                    $.ajax({
         
                        url: '?handler=RelatedBranches',
                        type: "GET",
                        dataType: "json",

                        data: { printserverId: printserverId },
                        success: function (data) {
                            console.log(data);
                            $('#branch-select').empty();
                            $.each(data, function (i, item) {
                                //console.log(item);
                                $('#branch-select').append($('<option>', {
                                    
                                    value: item.branchId,
                                    text: item.vBranchDesc
                                    
                                    
                                }));
                            });
                   
                            var tableShows = $('#example').DataTable({
                                paging: false
                            });

                            // Hide the table using jQuery
                            $('#example').hide();
                  
                            $('#example').DataTable();
                            if ($.fn.dataTable.isDataTable('#example')) {
                                $('#example').destory();
                                // The DataTables instance is already initialized
                            } else {
                                // Initialize the DataTables plugin
                                $('#example').DataTable();
                            }
                        }
                    });
                }
            });
            
           
            var table = $("#example").dataTable({
                "bFilter": false,
                "bSort": false,
                "paging": true,
                "bPaginate": true,
                "pagingType": "full_numbers",
                
                "columnDefs": [
                    { className: "pad-md-left-p-10 pad-top-bottom-p-10", "targets": [0, 1, 2, 3, 4, 5, 6, 7] }
                ],
                "lengthMenu": [[5,10, 15,20, 25, 35, 50, 100, -1], [5,10, 15,20, 25, 35, 50, 100, "All"]],
                "pageLength": 10 // Set the default page length to 10
            });
        
            

           
           
            table.destroy();

            
          
         
         

          

        });
    </script>

字符串
错误发生在块下面

var tableShows = $('#example').DataTable({
                                    paging: false
                                });
    
                                // Hide the table using jQuery
                                $('#example').hide();
                     
                                $('#example').DataTable();
                                if ($.fn.dataTable.isDataTable('#example')) {
                                    $('#example').destory();
                                    // The DataTables instance is already initialized
                                } else {
                                    // Initialize the DataTables plugin
                                    $('#example').DataTable();
                                }


我需要什么图片

kg7wmglp

kg7wmglp1#

我试着在下面,它成功地解决了我的问题
试试这个

$(document).ready(function () {
    // Create a variable to store the DataTable instance
    var table;

    $('#PrintServer-select').change(function () {
        var printserverId = $(this).val();
        if (printserverId) {
            console.log(printserverId);
            $.ajax({
                url: '',
                type: "GET",
                dataType: "json",
                data: { printserverId: printserverId },
                success: function (data) {
                    console.log(data);
                    $('#branch-select').empty();
                    $.each(data, function (i, item) {
                        $('#branch-select').append($('<option>', {
                            value: item.branchId,
                            text: item.vBranchDesc
                        }));
                    });

                    // Destroy the existing DataTable instance if it exists
                    if (table && $.fn.DataTable.isDataTable('#example')) {
                        table.destroy();
                    }

                    // Hide the table using jQuery
                    $('#example').hide();

                    // Reinitialize the DataTable
                    table = $('#example').DataTable({
                        paging: false
                    });
                }
            });
        }
    });

    // Initialize the DataTable outside of the change event
    table = $("#example").DataTable({
        "bFilter": false,
        "bSort": false,
        "paging": true,
        "bPaginate": true,
        "pagingType": "full_numbers",
        "columnDefs": [
            { className: "pad-md-left-p-10 pad-top-bottom-p-10", "targets": [0, 1, 2, 3, 4, 5, 6, 7] }
        ],
        "lengthMenu": [[5, 10, 15, 20, 25, 35, 50, 100, -1], [5, 10, 15, 20, 25, 35, 50, 100, "All"]],
        "pageLength": 10 // Set the default page length to 10
    });

    // Optional: Destroy the DataTable instance on document unload to prevent memory leaks
    $(window).on('beforeunload', function () {
        if (table && $.fn.DataTable.isDataTable('#example')) {
            table.destroy();
        }
    });
});

字符串
除了不能在两个不同的调用之间一致地拼写“destroy”这一事实,以及试图直接在jQuery对象上调用该方法而不是正确地调用它(如图所示
即使用调试器在代码中走上五分钟,也会向您展示:
1-创建表
2-立即尝试销毁该表,但由于没有正确调用该方法而失败
3-创建表
4-重新创建表
5-立即尝试销毁该表,但失败了,因为您没有正确调用该方法,这次您甚至不能正确拼写它
在所有这一切之间,您根本没有对表进行任何更改,因此重复调用创建和销毁它完全没有意义。

相关问题