jQuery DataTable如何在后台进程自动刷新表时重新绑定DataTable

时间:2019-06-24 13:47:03

标签: javascript jquery datatables

我有一个表,该表是使用PHP代码通过MySQL生成的数据构建的,并将其绑定到DataTable,该表和DataTable正常工作,但是当我单击该表行时,我为该表行添加了一个删除按钮后端PHP会删除记录,而不是我调用新的PHP来查询数据库并构建新表并将其推回到页面上,一切正常,除了在格式化新查询并将其推入dataTable的使用期限之外根本没有约束力,这是我的脚本。

谢谢

function aa_userDeleteSkill(in_id, in_uid) {
    $.ajax( {
        url: "user_delete_skill.php",
        type: "get", //send it through get method
        data: {
            id: in_id,
            uid: in_uid
        },
        success: function (response) {
            console.log( "success -- delete: " + response );

            // cleanup
            if ( $.fn.DataTable.isDataTable( '#SkillsUser' ) ) {
                $('#SkillsUser').DataTable().destroy();
            }
            $('#SkillsUser tbody').empty();

            // call the rendering function
            aa_userSkillRender( in_uid );

            jQuery('#SkillsUser').dataTable({
                "pageLength": 10,
                "ordering": false,
                "responsive": true,
                "destroy": true,
                drawCallback: function(dt) {
                    //console.log("draw() callback; initializing Select2's.");
                    //$('.experience-jquerySelect2-tag').select2({tags: true, width: "6em"});
                }
            });

        },
        error: function (xhr) {
            console.log( "failed -- delete: " + xhr );
        }
    } );
} // end of fucntion

function aa_userSkillRender( uid ) {
    // The following will prevent all future AJAX requests from being cached, 
    // regardless of which jQuery method you use ($.get, $.ajax, etc.)
    $.ajaxSetup({
        cache: false
    });

    // call the backend service 
    $("#IDuserListSkills").load("./user_render_skill.php?id=" + uid + " #IDuserListSkillsReturned",
        function (responseTxt, statusTxt, xhr) {
            console.log("responseTxt: " + responseTxt);
            if (statusTxt == "success") {
                console.log("user_render_skill.php: External content loaded successfully!");
            }
            if (statusTxt == "error") {
                console.log("user_render_skill.php -- Error: " + xhr.status + ": " + xhr.statusText);
            }
        }
    );

} // end of fucntion

0 个答案:

没有答案