简体   繁体   中英

Reload dataTable ajax source from another function in another js file

i should reload the data in my datatable after using a function to delete item i put the function in different file of the datatable : this is my delete function:

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");   
        } 
    }); 
}

and this is the file of my datatable :

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();
        }
    };
}();

how can i access to the datatable element and relaoad his data source in the success function of the ajax call in the RemoveFunction, Thanks.

Simple, use $("#sample_1").DataTable().ajax.reload(); notice that it's DataTable with capital D to access the API or you can also use $("#sample_1").DataTable().draw();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM