jQuery的数据表刷新不刷新页面

问题描述:

我使用jQuery DataTables这里是我的表目前是这样的:现在jQuery的数据表刷新不刷新页面

enter image description here

,截至目前,当我点击Deactivate我使用AJAX调用我的WebAPI方法将该记录设置为active = false ..因此表格中的复选框应该取消选中。

public IHttpActionResult Deletecode_AutoMake(int id) 
{ 
    code_AutoMake code_AutoMake = db.code_AutoMake.Find(id); 
    if (code_AutoMake == null) 
    { 
     return NotFound(); 
    } 

    code_AutoMake.Active = false; 
    db.Entry(code_AutoMake).State = EntityState.Modified; 
    db.SaveChanges(); 

    return Ok(code_AutoMake); 
} 

这一切都按预期工作..但表不重装,除非如果我刷新页面看到的复选框取消选中实际。

这是我的DataTable如何设置。

$("#Auto-Make-Table").on("click", 
    ".js-automake-delete", 
    function() { 

     var link = $(this); 
     var autoMakeName = $(this).data("automake-name"); 
     var autoMakeId = $(this).data("automake-id"); 

     bootbox.confirm("Are you sure you want to delete this auto make?", 
      function (result) { 
       if (result) { 
        $.ajax({ 
         url: infoGetUrl + autoMakeId, 
         method: "DELETE", 
         success: function() { 
          autoMakeTable.reload(); 
          toastr.success(autoMakeName + " successfully deleted"); 
         }, 
         error: function (jqXHR, textStatus, errorThrown) { 
          var status = capitalizeFirstLetter(textStatus); 
          var error = $.parseJSON(jqXHR.responseText); 
          console.log(error); 
          toastr.error(status + " - " + error.exceptionMessage); 
         } 
        }); 
       } 
      }); 
    }); 

我的问题/目标是..我如何让表刷新/重新加载本身,而不实际页面刷新?

任何帮助表示赞赏。

enter image description here

跟此之一::

UPDATE

autoMakeTable.ajax.reload();使用

接收此错误尝试

enter image description here

假设你的数据表上这样分配,

var myDataTable = $('#myTableId').DataTable({ 
    //Rest code 
}) 

然后你success method为停用里面,您只需要重新加载数据表这样,

success: function() 
{ 
    myDataTable.ajax.reload(); 
    //rest code 
} 

你忘了加ajax。请查看official link以了解更多信息。

更新:

如果你的部分看法是strongly typed,然后调用在成功的方法,你的操作方法,并使其一样,在你上面的成功方法,

success: function() 
{ 
    $.ajax({ 
     url: "@Url.Action("Index", "code_AutoMake")", //As you have mentioned in the chat 
     method: "GET", 
     success: function (data) 
     { 
     //Here just render that partial view like 
     $("#myDiv").html('').html(data); //This will empty first then render the new data 
     } 
     }) 

注:这里myDivdivid,你的表位于哪里。

希望它能帮助:)

+0

请参阅我的更新请 –

+0

我相信,您通过使用** Ajax **来绑定您的数据表。而你需要编写你的数据表名而不是** myDataTable **。我认为你的数据表变量名是** autoMakeTable **。所以你需要在成功方法里写'autoMakeTable.ajax.reload();'。 –

+0

对不起,我做的是..我只是复制并粘贴你的代码..但我确实使用了我的个人表名autoMakeTable ..并且收到了这些错误 –

在您的php文件或其中可能包含的文件中创建一个与您当前的html结构相同的表格,而不是通过执行ajax调用而收到的新数据。

$.post("newTable.php", function(data) { 
    $(".currentTable").html(data); 
}); 
+0

你可以在这里查看示例:https://datatables.net/forums/discussion/38969/reload-refresh-table-after-event –

+0

我相信OP的后端是ASP。 net ... – davidkonrad

+0

是代码以任何后端语言运行 –