从另一个js文件中的另一个函数重新加载dataTable ajax源

时间:2016-08-12 23:53:41

标签: javascript jquery ajax datatable

我应该在使用函数删除项目后重新加载我的数据表中的数据 我把函数放在数据表的不同文件中: 这是我的删除功能:

function removeFunction(table,id) { 
    var txt= $('.titleSup').text();
    var txt2= $('.textSup').text();
    swal({   
        title:"Delete",   
        text: txt,  
        type: "warning",   
        showCancelButton: true,   
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "Yes !",   
        cancelButtonText: "No, cancel",   
        closeOnConfirm: false,   closeOnCancel: false 
    }, function(isConfirm){
        if (isConfirm) {
            $.ajax({
                url: "ajax/delete.php?for="+table+"&id="+id,
                success : function(data){ 
                    if(data == 'ok'){
                        swal({   
                            title:"Delete",   
                            text: txt2,  
                            type: "success",    
                            confirmButtonColor: "#AEDEF4",
                            confirmButtonText: "Ok",   
                            closeOnConfirm: true,   
                        }, function(isConfirm){
                            //reload here 
                        });
                    }else{
                        swal("Error, try again", "", "error");  
                    }  
                   }
            }); // end ajax call 

        } else {     
            swal("Cancel", "", "error");   
        } 
    }); 
}

这是我的数据表的文件:

var TableData = function () {
    //function to initiate DataTable
    //DataTable is a highly flexible tool, based upon the foundations of progressive enhancement, 
    //which will add advanced interaction controls to any HTML table
    //For more information, please visit https://datatables.net/
    var runDataTable = function () {
        var oTable = $('#sample_1').dataTable({
            "sAjaxSource": 'ajax/get_companys.php',
            "aoColumns": [
                { "mData": "register_no" },
                { "mData": "name" },
                { "mData": "email" },
                { "mData": "phone" },
                { "mData": "fax" },
                { "mData": "nbr_customer" },
                { "mData": "nbr_user" },
                { "mData": "created" }

            ],
            "aoColumnDefs": [{
                "aTargets": [0]
            }],
            "oLanguage": {
                "sLengthMenu": "Show _MENU_ Rows",
                "sSearch": "",
                "oPaginate": {
                    "sPrevious": "",
                    "sNext": ""
                }
            },
            "aaSorting": [
                [2, 'desc']
            ],
            "aLengthMenu": [
                [5, 10, 15, 20, -1],
                [5, 10, 15, 20, "All"] // change per page values here
            ],
            // set the initial value
            "iDisplayLength": 10,
        });
    };
    return {
        //main function to initiate template pages
        init: function () {
            runDataTable();
        }
    };
}();

如何在RemoveFunction的ajax调用的成功函数中访问datatable元素并重新加载他的数据源,谢谢。

2 个答案:

答案 0 :(得分:2)

简单,请注意使用带有大写字母D的DataTable来访问API,或者也可以使用$("#sample_1").DataTable().ajax.reload();

答案 1 :(得分:0)

您只需使用$("#sample_1").dataTable().draw();