javascript - TypeError : oTable. fnReloadAjax 不是函数 - DataTables

标签 javascript php jquery codeigniter datatable

我认为数据表不会再有问题,因为它现在甚至刷新都可以工作。

事实证明,有时它在某些地方可以工作,但在某些地方它只是给出这个愚蠢的错误。

TypeError: oTable.fnReloadAjax is not a function

我有一个函数可以在表中添加新数据,成功后我希望刷新数据表。

它确实在其他 View 上工作,但现在当我创建了这个 View 时,它不起作用。

我正在使用 Ignited Datatables。

我有一个 common.js 文件,其中包含数据表脚本。这个脚本运行良好。

function commonDataTables(selector,url,aoColumns){
    var responsiveHelper;
    var breakpointDefinition = {
        tablet: 1024,
        phone : 480
    };
      oTable = selector.dataTable({
        sPaginationType: 'bootstrap',
        oLanguage      : {
            sLengthMenu: '_MENU_ records per page'
        },
        "autoWidth" : false,
        "aoColumns":aoColumns,
        "bServerSide":true,
        "bProcessing":true,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "sAjaxSource": url,
        "iDisplayLength": 25,
        "aLengthMenu": [[2, 25, 50, -1], [2, 25, 50, "All"]],
        'fnServerData'   : function(sSource, aoData, fnCallback){
            $.ajax ({
                'dataType': 'json',
                'type'    : 'POST',
                'url'     : sSource,
                'data'    : aoData,
                'success' : fnCallback
            }); //end of ajax
        },
        'fnRowCallback': function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            $(nRow).attr("data-id",aData[0]);
            responsiveHelper.createExpandIcon(nRow);
            return nRow;
        },
        fnPreDrawCallback: function () {
            // Initialize the responsive datatables helper once.
            if (!responsiveHelper) {
                responsiveHelper = new ResponsiveDatatablesHelper(selector, breakpointDefinition);
            }
        },
        fnDrawCallback : function (oSettings) {
            // Respond to windows resize.
            responsiveHelper.respond();
        }
    });
}

然后在我的 Codeigniter 查看文件中。

    oTable = '';
    //Data Tables Script Here.
    var selector = $('#ManageTabs');
    var url = "{{base_url()}}admin/configurations/listTabs_DT/";
    var aoColumns =  [
        /* ID */   {
            "bVisible":    false,
            "bSortable":   false,
            "bSearchable": false
        },
        /* Tab Name */  null,
        /* Tab Order */  null,
        /* Tab Desc */  null,
        /* Actions */  null
    ];
    commonDataTables(selector,url,aoColumns);
    //End Of dataTables Script..

我使用oTable作为全局变量,因此在document.ready函数之外定义了oTable

var oTable;

我知道使用全局变量是一种不好的做法,但我想找到一种解决方法来使数据表刷新所有数据。

这个全局变量方法确实适用于 1 个 View ,但在下一个 View 中我收到了我已经告诉过你的错误。

这里,下面是按钮的代码,它应该成功地履行其职责,但却给出了错误..

            $('#createTabBtn').on('click', function(e){
                //e.stopImmediatePropagation();
                e.preventDefault();
                var selector = $('#createTabModelForm');
                HRS.formValidation(selector);
                if(selector.valid()){
                    var formData = {
                        TabName : $("#cTabName").val(),
                        TabOrder : $("#cTabOrder").val(),
                        TabDesc :   $("#cTabDesc").val()
                    };
                    $.ajax({
                        type:"post",
                        url:"{{base_url()}}admin/configurations/addNewTab/",
                        dataType:"json",
                        data: formData,
                        success: function(output){
                            if (output == true){
                                oTable.fnReloadAjax();
                            }
                        }
                    });

                    //Do Stuff After pressing the Create Button.
//                    Close the Modal
                    $('#addNewTabModal_ManageTabs').modal('hide');
//                    Reset All the TextBoxes and CheckBoxes
                    $("#createTabModelForm")[0].reset();
//                    Reset/Empty All the Select2 Dropdowns
                    //jQuery('.select2-offscreen').select2('val', '');
                }
                else{
                    //The Else Portion if you want Something else to Happen if not validated Form
                }

            });

请问谁知道刷新数据表的最佳方法..

最佳答案

天啊,我忘了添加 fnReloadAjax.js 文件。我在第一个 View 中添加了该内容,但忘记在此 View 中添加..

所以如果有人遇到这样的问题,只需查看 js 文件是否已附加即可。

粗心浪费了我的时间,但最终问题得到了解决。

但是仍然存在全局变量的问题,我的意思是我使用全局变量来刷新网格。

如果有人有更好的选择来使用这个fnreloadajax函数。请分享。

关于javascript - TypeError : oTable. fnReloadAjax 不是函数 - DataTables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25520959/

相关文章:

php - 我的 MySQL 查询、while 循环和 SESSIONS 无法正常工作 PHP

javascript - 卡片翻转动画 Internet Explorer 11

javascript - 将 php 作为 "return value"回显到 ajax 调用

java - Java应用服务器利用304 HTTP响应将数据缓存到JavaScript中是否正常

javascript - 关闭模态时位置标记消失

javascript - 使用 jQuery 在页眉和页脚之间滚动时如何使侧边栏变粘(不滚动它们)?

javascript - 仅在页面刷新后加载 Google Maps API v3

javascript - javascript原型(prototype)的意义到底是什么

php - PDO:使用 foreach 和 $_POST 定义参数

php - 截断长字符串,不打断带点插值的单词