Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove row from Kendo UI Grid with jQuery

I am using a Kendo UI grid and for deleting a row I am using a custom button with bootstrap that when I click on it, with ajax I call a web api method to remove that row and if it is successfully deleted that row removes it from the DOM. (I'm not using the command destroy of kendo)

The problem I have is that if I try to filter that row that was removed, it appears again in the grid and it seems that it was not removed at all.

This is my Kendo UI grid:

var table = $("#grid").kendoGrid({                
    dataSource: {
        transport: {
            read: {
                url: "/api/customers",
                dataType: "json"
            }
        },
        pageSize: 10
    },               
    height: 550,
    filterable: true,
    sortable: true,
    pageable: {
        refresh: true,
        pageSizes: true,
        buttonCount: 5
    },
    columns: [{
        template: "<a href='' class='btn-link glyphicon glyphicon-remove js-delete' title='Delete' data-customer-id= #: Id #></a>",
        field: "Id",
        title: "&nbsp;",
        filterable: false,
        sortable: false,
        width: 50,
        attributes: {
            style: "text-align: center" 
        }
    }, {
        field: "Name",
        title: "Name",
        width: 100,
    }, {
        field: "LastName",
        title: "LastName",
        width: 100,
    }, {
        field: "Email",
        title: "Email",
        width: 150
    }]
});

And this is my jQuery code for deleting a row:

$("#grid").on("click", ".js-delete", function () {
     var button = $(this);
     if (confirm("Are you sure you want to delete this customer?")) {
         $.ajax({
             url: "/api/customers/" + button.attr("data-customer-id"),
             method: "DELETE",
             success: function () {
                 button.parents("tr").remove();  //This part is removing the row but when i filtered it still there.
             }
         });
     }
 });

I know that in jQuery DataTables when can do something like this:

 table.row(button.parents("tr")).remove().draw();

How can i do something like this with Kendo UI using jQuery?

like image 506
Juan José Avatar asked Sep 25 '17 13:09

Juan José


People also ask

How do I delete multiple rows in kendo grid?

Why don't you just make your grid select multiple rows then each time a row is selected, you keep each row/record (uid) in an Array. Then onClick of your Delete button, loop through the gridDataSource and use the "remove" [gridDataSource. remove(selectedRow);] method then "sync" [gridDataSource. sync();] it.


2 Answers

Don't ever play with a Kendo's widget DOM. Always use it's methods instead.

Using Grid's removeRow():

$("#grid").on("click", "button.remove", function() {
    var $tr = $(this).closest("tr"),
        grid = $("#grid").data("kendoGrid");

    grid.removeRow($tr);
});

Demo


Using DataSource's remove():

$("#grid").on("click", "button.remove", function() {
    var $tr = $(this).closest("tr"),
        grid = $("#grid").data("kendoGrid"),
        dataItem = grid.dataItem($tr);

    grid.dataSource.remove(dataItem);
});

Demo

like image 61
DontVoteMeDown Avatar answered Sep 27 '22 20:09

DontVoteMeDown


The removed row will be present in the kendo ui till you push changes to server. To remove the row entirely you need to use grid.saveChanges()

So the code below will remove row from server as well from ui

const row = $(e.target).closest('tr')[0];
const grid = $(e.target).closest('#grid').data("kendoGrid");
grid.removeRow(row);
grid.saveChanges() //comment out if you need to remove only from ui
like image 40
Stadub Dima Avatar answered Sep 27 '22 21:09

Stadub Dima