3

I am using jQuery DataTables and here is what my table currently looks like:

enter image description here

Now, as of right now when I click Deactivate I am using ajax to call my WebApi method to set that record to active = false.. so the checkbox in the table should be unchecked.

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

This all works as expected.. but the table doesn't reload, unless if I refresh the page to see the check box actually unchecked.

Here is how my DataTable is set up.

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

My question/goal is.. how do I get the table to refresh/reload itself without an actual page refresh?

Any help is appreciated.

UPDATE

Tried using autoMakeTable.ajax.reload();

Received this error:

enter image description here

Followed by this one:

enter image description here

2 Answers 2

2

Suppose your datatable is assigned like this,

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

Then inside your success method for deactivate, you just need to reload your datatable like this,

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

You forgot to add ajax. Please check this official link to learn more.

UPDATED:

If your partial view is strongly typed, then call your action method in success method and render it, like, In your above success method,

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

Note: Here myDiv is the id of div, where your table located.

Hope it helps :)

Sign up to request clarification or add additional context in comments.

8 Comments

I believe, you are binding your datatable by using Ajax. And you need to write your datatable name instead myDataTable. I think your datatable variable name is autoMakeTable. So u need to write autoMakeTable.ajax.reload(); inside success method.
Sorry, I did do that.. I just copy and pasted your code.. but I did use my personal table name autoMakeTable.. and received those errors
Is your datatable get action method returning data in json format or you simply returning a view ?
I'm confused.. why does my Get method matter, if the page doesn't refresh?
Yes! If you dont want to refresh a page and want to load data then you need to return data in json or xml format. The error itself saying that your json data is invalid.
|
0

Create in your php file, or which file it may be, a table with the same html structure as your current one, but than with new data that you have received by doing the ajax call.

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

1 Comment

You can view the Example here: datatables.net/forums/discussion/38969/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.