Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh Jquery datatable after deleting a row

I have a Datatable that display data from a table (role) and i use an ajax call to delete a row :

 function OnDeleteClick(el) 
    {
        //var url = ($(this).attr('href'));
        //alert(url);
        //var employeeId = event.getAttribute(id); //e.target.id
        //var url = ($(this).attr('id'));      
        var id = $(el).attr('id');       
        var roleid = getURLParameter(id, 'id');        
        var username = getURLParameter(id, 'name');        
        var flag = confirm('You are about to delete Role ' + username + ' permanently.Are you sure you want to delete this record?');

        if (flag) { 
            $.ajax({ 
                url: '/Role/DeleteAJAX', 
                type: 'POST', 
                data: { roleId: roleid },
                dataType: 'json', 
                //success: function (result) { alert(result); $("#" + Name).parent().parent().remove(); },
                success: function (result) {
                    alert(result); 
                },
                error: function () { alert('Error!'); } 
            }); 
        } 
        return false; 
    }

My question is how to refresh the jquery datatable once the delete has succeed, so the deleted row will be also deleted from the datatable?

Below is the code that i use to build my JQuery datatable :

<div class="row" >
    <fieldset class="col-md-10 col-md-offset-1" >
        <legend>Liste des rôles </legend>
        <div class="table-responsive" style="overflow-x: hidden;">
            <table width="98%" class="table table-bordered table-hover" id="dataTables-example">
                <thead>
                    <tr>
                        <th>Designation Role</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @item.Name
                            </td>
                            <td width="110">                                
                                <a class="btn btn-custom btn-xs" href="" onclick="return getbyID('@item.Id',false)" title="consulter">
                                    <i class="fa fa-folder-open-o"></i>
                                </a>
                                <a class="btn btn-custom btn-xs" href="" onclick="return getbyID('@item.Id',true)" title="Editer">
                                    <i class="fa fa-edit"></i>
                                </a>
                                <a class="btn btn-custom btn-xs" onclick="OnDeleteClick(this);" id="#[email protected]&[email protected]" title=" supprimer">
                                    <i class="fa fa-trash-o"></i>
                                </a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
        <div class="b-right">
            <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create", "Role")'">
                <i class="fa fa-plus"></i> Ajouter un role
            </button>
        </div>        
    </fieldset>
</div>

Thanks for your help.

like image 861
Zakaria.d Avatar asked Mar 09 '23 22:03

Zakaria.d


1 Answers

If you are using DataTables.net and you built your table like this with an AJAX data source:

var myTable = $("#myTable").DataTable({ ajax: "path/to/my/data" });

You can refresh the data in the table like this:

$.ajax({ 
    <your deleting code>
    success: function() {
        myTable.ajax.reload();
    })
});

If you've built your table with Razor using the view model from MVC, you will need to either 1) reload the page in the success callback:

$.ajax({ 
    <your deleting code>
    success: function() {
        window.location.reload();
    })
});

or 2) remove the row from the DOM, knowing it's already deleted from the database, to keep the DB and the UI in sync:

$.ajax({ 
    <your deleting code>
    success: function() {
        myTable.row($(el).parents("tr")).remove().draw();
    })
});
like image 192
Joe Wilson Avatar answered Mar 12 '23 10:03

Joe Wilson