通过id加载特定页面时,数据表无法正常工作

时间:2016-10-21 11:29:14

标签: javascript jquery ajax datatable sweetalert

我的页面已完成数据表,在此图片中enter image description here

我有一个问题,一旦我激活或停用我的员工,数据表搜索和排序将消失。我认为我的错误是在加载部分。使用div id加载特定页面的正确方法是什么?

这是激活或停用我的员工enter image description here

时的输出

因为我所做的是在我的base_url +“adminpage / userpage”中,它是用户和员工的一页。我刚刚做的是当我按下查看人员时它将隐​​藏用户并将显示在同一链接中的员工页面。

我的代码在这里

$(document).ready(function(){
$("#user-page").show();
$("#viewstaff").on("click",function(){
$("#user-page").hide();
$('#staff-page').show();
});
$("#viewuser").on("click",function(){
$("#staff-page").hide();
$("#user-page").show();
});})

我的激活代码在这里

function activate_staff(id){

        swal({
  title: 'Are you sure?',
  text: "You want to activate this user?",
  type: 'info',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, Deactivate it!'
}).then(function() {
    $.ajax({
        url : base_url+"adminpage/staff_deactivate/"+id,
        type: "POST",
        success: function(data){
           $('#dataTables-example1').DataTable();

            var result = JSON.parse(data);

            if(result===1){

              swal({
                title: 'Deactivate Success',
                // text: "will remove within 1 sec",
                type: 'success',
                timer: 1500,
                showConfirmButton: false
              }).done()
                window.setTimeout(function(){ 
                 $("#wrapper-staff").load(base_url+"adminpage/useraccount #wrapper-staff > *");

                 } ,1500)
            }
        },
        error: function (jqXHR, textStatus, errorThrown){
            swal("Oops...", "Something went wrong :(", "error");
        }
    });
})}

和停用是

function deactivate_staff(id){
    swal({
  title: 'Are you sure?',
  text: "You want to deactivate this user?",
  type: 'info',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, Activate it!'
}).then(function() {

    $.ajax({
        url : base_url+"adminpage/staff_activate/"+id,
        data: $(this).serialize(),
        type: "POST",


        success: function(data){

            var result = JSON.parse(data);

            if(result===1){

              swal({
                title: 'Activated Success',
                // text: "will remove within 1 sec",
                type: 'success',
                timer: 1500,
                showConfirmButton: false
              }).done()
                window.setTimeout(function(){


                  $("#wrapper-staff").load(base_url+"adminpage/useraccount #wrapper-staff > *");

                 } ,1500)


            }
        },
        error: function (jqXHR, textStatus, errorThrown){
            swal("Oops...", "Something went wrong :(", "error");
        }
    });
})    }

1 个答案:

答案 0 :(得分:2)

您必须将此DataTable更改为服务器端,如example。之后,您可以在每行上添加按钮,如下所示:

table = $('#dataTables-example').DataTable({ 
    "serverSide": true, //Feature control DataTables' server-side processing mode. 

    // Load data for the table's content from an Ajax source 
    "ajax": { 
        "url": "<?php echo site_url('serversidedt/ajax_dt')?>", 
        "type": "POST" 
    }, 

    "columns": [ 
        {data: "fname"}, 
        {data: "lname"}, 
        {data: "email"}, 
        {data: "status"}, 
        {data: myButton, searchable: false, orderable: false} 
    ] 

}); 

function myButton(data, type, dataToSet) { 
    if(dataToSet.status == 1){ 
       return "<button class='btn btn-primary'>Active</button>"; 
    }else{ 
       return "<button class='btn btn-primary'>Desactive</button>"; 
    } 
};

你必须像这样刷新你的DataTable:

table.ajax.reload( null, false )

希望有所帮助。