This is a migrated thread and some comments may be shown as answers.

Kendo UI DataSource Destroy function calls more than one time for multiple records

1 Answer 583 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ajeesh
Top achievements
Rank 1
Ajeesh asked on 11 Apr 2013, 12:22 PM
Hi friends,

I have a strange problem in Kendo UI - which I dont understand.

This is my code.

$(document).ready(function() {

        var kendo_dataSource = new kendo.data.DataSource({
            autoSync: true,
            batch: true,
            transport: {
                read: {
                    url: "<?php echo BASE_URL . 'kendo/kendo_grid_read' ?>",
                    dataType: "json"
                },
                destroy: {
                    url: "<?php echo BASE_URL . 'kendo/kendo_grid_destroy' ?>",
                    dataType: "json",
                    type: "POST"
                },
                parameterMap: function(data, type) {
                    if (type == "destroy") {
                        return {models: data.models}
                    }
                }
            },
            serverFiltering: true,
            serverGrouping: true,
            serverPaging: true,
            page: 1,
            pageSize: 5,
            schema: {
                data: "results",
                total: "total",
                model: {
                    id: "field1"
                }
            }
        });

        $("#kendo_grid2").kendoGrid({
            dataSource: kendo_dataSource,
            height: 300,
            filterable: true,
            sortable: true,
            pageable: true,
            selectable: "multiple row",
            columns: [
                {
                    field: "field1"
                },
                {
                    field: "field2"
                },
                {
                    field: "field3"
                }
            ]
        });

        $("#test_button").on("click", function() {
            var selectedRows = $("#kendo_grid2").data("kendoGrid").select();
            if (selectedRows.length > 0) {
                for (var i = 0; i < selectedRows.length; i++) {
                    var dataItem = $("#kendo_grid2").data("kendoGrid").dataItem(selectedRows[i]);
                    console.log(dataItem);
                    kendo_dataSource.remove(dataItem);
                }
            }
        });

    });

Here goes the situation.

When the $("#test_button").on("click", function() is fired, it checks for the selected rows in the grid - and delete the rows.

If I select 2 rows, it deletes 2 rows. And the 2 rows are disappeared from the Grid.

But, I see something strange -

whhen 2 rows are deleted, there is 2 POST request - which is fine.

But the first POST request's parameters are

    models[0][field1]    3
    models[0][field2]    poioioi
    models[0][field3]    oiuoiuuigbhkjh
    models[0][field4]    kjh kjhkjhyt

And the second POST request's parameters are

    models[0][field1]    3
    models[0][field2]    poioioi
    models[0][field3]    oiuoiuuigbhkjh
    models[0][field4]    kjh kjhkjhyt
    models[1][field1]    4
    models[1][field2]    kjhk hkiui
    models[1][field3]    khkj
    models[1][field4]    mkhkhkhkjhghgfgdf

And I understand that, I can access the data in the server like this

    foreach ($_POST['models'] as $model) {
                echo $model['field1'];           
    }

I was wondering if its possible to send only one request - possibly the second POST request only, as I can delete the 2 rows in one request.

Or sending 2 separate request but with only one model at a time?

Is it possible?

Any help would be greatly appreciated.

I understand that autoSync should be set to false and I should I call  to solve this situation.

But, I wonder if where should I call it? After the for loop ?

With kind regards,

Ajeesh

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 11 Apr 2013, 12:51 PM
Hello,

 I am pasting here my response to the same question asked on stackoverflow:

 This is caused by the autoSync setting. When you set it to true the data source calls the syncmethod after every change. Setting autoSync to false and manually calling the sync() method would cause the data source to make only one request with all deleted data items.

Greetings,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Ajeesh
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or