简体   繁体   中英

Kendo UI: Data Grid Update Rest Call Not working

I'm using KendoUI and I have a datagrid in which I am trying to update data in a row inline. When I click the edit button, the UPDATE and CANCEL buttons are presented. When I click the UPDATE button nothing occurs. I have debugged this issue using Chrome developer tools and I put a break point where I thought an error(s) would appear. However, there are no errors that show up in relation to the UPDATE functionality of the KendoUI Grid datasource. Here is a snippet of my code:

endorsementsTabGridDataSource = new kendo.data.DataSource({
    type: "json",
    transport: {
        read: { url:function (obj){
          return  "/${applicationScope.contextName}/admin/endorsements/api?templateVersionSysId=" + 201442 + "&formCode=" + '7610810' + "&state=" + "FL"
           // url: "/${applicationScope.contextName}/admin/endorsements/api?templateVersionSysId=" +  $("#templateVersionSysId").val() + "&formCode=" + formCode + "&state=" + stateCode
        }
    }},

    update: {
        url: function (obj) {
            return "/${applicationScope.contextName}/admin/endorsements/api"
        },
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        data: function(data){
            return kendo.stringify(data)
        }
    },

    parameterMap: function (options, operation) {
        /* if ((operation === "create" || operation === "update" ) && options) {
            options.state = states.value();
            options.templateVersionSysId = $("#templateVersionSysId").val();
            options.formCode = formCode;
        }

        if (operation !== "read" && options) {
            return  kendo.stringify(options);
        }*/

        return JSON.stringify(options)
    },

    batch: false,
    error: function (xhr, status, error) {
        notification.show('Error occured in Endorsements Tab: ' + xhr.responseText, "error");
    },
    schema: {
        model: {
            id: "customEndorSysId",
            fields: {
                fieldKey: {editable: false, nullable: false},
                name: { editable: true, nullable: false},
                description: {editable: true},
                state: {editable: false, nullable: true},
                templateFldSysId: {editable: false, nullable: false}
            }
        }
    }

});

In addition to the jquery mentioned above, the rest call makes a call to a Spring MVC Controller. The Controller class is preceded with

@RequestMapping("/admin/endorsements")

The method that is called is the following java code:

  @ResponseBody
@RequestMapping(value = {"/api"}, method = {RequestMethod.PUT, RequestMethod.POST}, produces = {"application/xml", "application/json"}, headers = {"application/json"})
public ResponseEntity updateEndorsement(@RequestBody String customEndorsementDescription) {

    try {
       // hibernateDao.initAuditFields(customEndorsementDescription, principal);
      //  hibernateDao.saveOrUpdate(customEndorsementDescription);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {

    }
    return new ResponseEntity(customEndorsementDescription, HttpStatus.OK);
}

I figured out what my problem was. The problem originates with bad structuring of the restful call on the client side. The update call was outside of the transport object. See corrected code below:

endorsementsTabGridDataSource = new kendo.data.DataSource({
    transport: {
        read: { url: "/${applicationScope.contextName}/admin/endorsements/api?templateVersionSysId=" + 201442 + "&formCode=" + '7610810' + "&state=" + "FL"
            // url: "/${applicationScope.contextName}/admin/endorsements/api?templateVersionSysId=" +  $("#templateVersionSysId").val() + "&formCode=" + formCode + "&state=" + stateCode
        },
        update: {
            url: "/${applicationScope.contextName}/admin/endorsements/api/test",
            type: "POST",
            dataType: "json",
            contentType: "application/json"
        },
        parameterMap: function (options, operation) {
            if (operation !== "read") {
                //console.log("Models: ", options.models);
                options.key = options.fieldKey;
                options.stateText = stateCode;
                return JSON.stringify(options);
            }
            return options;
        }},
    batch: false,
    schema: {
        model: {
            id: "customEndorSysId",
            fields: {
                fieldKey: {editable: false, nullable: false},
                name: { editable: true, nullable: false},
                description: {editable: true}
            }
        }
    }

});

On the backend controller side, I had to make the following changes:

 @ResponseBody
@RequestMapping(value = {"/api/test"}, method = {RequestMethod.POST}, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity updateEndorsement(@RequestBody EndorsementDescription customEndorsementDescription, Principal principal) {

    TrTemplateEndorDesc trTemplateEndorDesc = new TrTemplateEndorDesc();
    try {

        hibernateDao.initAuditFields(customEndorsementDescription, principal);
        trTemplateEndorDesc.setAuditRecCreateApplId(customEndorsementDescription.getAuditRecCreateApplId());
        trTemplateEndorDesc.setAuditRecCreateApplUserId(customEndorsementDescription.getAuditRecCreateApplUserId());
        trTemplateEndorDesc.setAuditRecCreateDbUserId(customEndorsementDescription.getAuditRecUpdtDbUserId());
        //trTemplateEndorDesc.setAuditRecCreateDts(customEndorsementDescription.getAuditRecCreateDts());
        hibernateDao.saveOrUpdate(trTemplateEndorDesc);

    } catch (Exception e) {
        //e.printStackTrace(e.printStackTrace() + e.getCause());
        throw new RuntimeException("Unknown Exception", e.getCause());
    } finally {

    }
    return new ResponseEntity(customEndorsementDescription, HttpStatus.OK);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM