Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo Grid calls 'create' operation instead of 'update' after adding new record

I've setup a basic Kendo Grid and I'm using the DataSourceResult class from the PHP Wrapper library on the sever side.

I've come across a strange issue... if I create a new record and then edit it (without refreshing the page), the create operation is called again, rather than the update operation.

If the page is refreshed after adding the new record, the update operation is called correctly after making changes to the record.

I can confirm that the DataSourceResult class is returning the correct data after the create operation, including the id of the new record.

Any ideas why this is happening (and how to stop it)? Thanks

Update: Here's the datasource code. The query string in the url is just to easily distinguish the requests in Chrome's console. The additional data passed with each request is used by ajax.php to distinguish the different actions requested.

data = new kendo.data.DataSource({
    transport: {
        create:  {
            url: '/ajax.php?r=gridCreate',
            dataType: 'json',
            type: 'post',
            data: { request: 'grid', type: 'create' }
        },
        read:  {
            url: '/ajax.php?request=gridRead',
            dataType: 'json',
            type: 'post',
            data: { request: 'grid', type: 'read' }
        },
        update: {
            url: '/ajax.php?r=gridUpdate',
            dataType: 'json',
            type: 'post',
            data: { request: 'grid', type: 'update' }
        },
        destroy: {
            url: '/ajax.php?r=gridDestroy',
            dataType: 'json',
            type: 'post',
            data: { request: 'grid', type: 'destroy' }
        },
        parameterMap: function(data, operation) {
            if (operation != "read"){
              data.expires = moment(data.expires).format('YYYY-MM-DD HH:mm');
            }
            return data;
        }
    },
    schema: {
        data: 'data',
        total: 'total',
        model: {
            id: 'id',
            fields: {
                id: { editable: false, nullable: true },
                code: { type: 'string' },
                expires: { type: 'date' },
                enabled: { type: 'boolean', defaultValue: true }
            }
        }
    },
    pageSize: 30,
    serverPaging: true,
    serverSorting: true,
    serverFiltering: true 
});
like image 825
Mat Avatar asked Dec 04 '13 16:12

Mat


2 Answers

Best solution

Set to update, create or delete different Call Action

From Telerik Support :

I already replied to your question in the support thread that you submitted on the same subject. For convenience I will paste my reply on the forum as well.

This problem occurs because your model does not have an ID. The model ID is essential for editing functionality and should not be ommited - each dataSource records should have unique ID, records with empty ID field are considered as new and are submitted through the "create" transport.

 schema: {
     model: {
         //id? model must have an unique ID field
         fields: {
             FirstName: { editable: false},
             DOB: { type: "date"},
             Created: {type: "date" },
             Updated: {type: "date" },
         }
     } },

For more information on the subject, please check the following resources:

http://docs.kendoui.com/api/framework/model#methods-Model.define http://www.kendoui.com/forums/ui/grid/request-for-support-on-editable-grid.aspx#228oGIheFkGD4v0SkV8Fzw

MasterLink

I hope this information will help

like image 80
Ahmed Galal Avatar answered Jan 04 '23 04:01

Ahmed Galal


I have also the same problem & I have tried this & it will work.

.Events(events => events.RequestEnd("onRequestEnd"))

And in this function use belowe:

function onRequestEnd(e) {
    var tmp = e.type;
    if (tmp == "create") {
        //RequestEnd event handler code
          alert("Created succesully");
        var dataSource = this;
        dataSource.read();
    }
    else if (tmp == "update") {
          alert("Updated succesully");
    }
}

Try to Use this code in onRequestEnd event of grid

var dataSource = this;
    dataSource.read();

Hope that it will help you.

like image 30
Herin Avatar answered Jan 04 '23 05:01

Herin