Kendo网格层次结构添加新行 - 将父ID传递给新的子记录

时间:2015-07-13 20:24:01

标签: javascript kendo-ui parent-child hierarchy

我是kendo和javascript的新手,所以请原谅任何知识失误...下面我有一个基本的kendo分层网格。当我尝试将新记录添加到子网格时,记录将通过我的控制器中的post方法添加,但父引用ID未正确传递,而是0作为外键引用传递给子表。

这是我的父网格

        //-----------------KENDO GRID DATA-----------------
    var datasource = new kendo.data.DataSource({
        type: "odata",
        transport: {
            read: {
                //url: sBaseUrl,
                url: baseUrl + "StationLogPaperTicketOdata",
                type: "get",
                dataType: "json",
                contentType: "application/json"
            },
            update: {
                type: "PUT",
                url: function (data) {
                    return baseUrl + "StationLogPaperTicketOdata(" + data.log_id + ")";
                },
                dataType: "json",
            },
            //parameterMap: function(options, operation) {
            //    if (operation !== "read" && options.models) {
            //    return {models: kendo.stringify(options.models)};
            //    }
            //}

        },

        schema: {
            data: "value",
            total: function (data) {
                return data['odata.count'];
            },
            model: {
                id: "log_id",
                fields: {
                    log_id: { type: "number" },
                    log_station: { type: "string" },
                    log_sourceCode: { type: "string", nullable: true },
                    log_carrierName: { type: "string", nullable: true },
                    log_ticketNumber: { type: "string", nullable: true },
                    log_deliveryTS: { type: "date" },
                    log_leaseNumber: { type: "string", nullable: true },
                    log_leaseName: { type: "string", nullable: true },
                    log_type: { type: "string" },
                    log_rackNumber: { type: "number", nullable: true },
                    log_logNumber: { type: "number", nullable: true },
                    log_driverFirstName: { type: "string", nullable: true },
                    log_driverLastName: { type: "string", nullable: true },
                    log_linkedTicketNumber: { type: "string", nullable: true },
                    log_volumeGross: { type: "number" },
                    log_volumeNet: { type: "number", nullable: true },
                    log_originalDestination: { type: "string", nullable: true },
                    log_lastModifiedTS: { type: "date", editable: false },
                    log_lastModifiedBy: { type: "string", editable: false },
                    log_meterReadOpen: { type: "number", nullable: true },
                    log_volumeNetCalculated: { type: "number", nullable: true },
                    log_isDeleted: { type: "string", nullable: true },
                    log_detailIsDeleted: { type: "string", nullable: true },
                    log_paperTicketIsApproved: { type: "string", nullable: true, editable: false },
                    log_accountingDate: { type: "date", nullable: true },
                    log_stationNumber: { type: "string", nullable: true },
                    log_isPickup: { type: "string", nullable: true },
                    log_operatorNumber: { type: "string", nullable: true },
                    log_controlNumber: { type: "string", nullable: true },
                    log_dispatchConfirmationNumber: { type: "string", nullable: true },
                    log_groupId: { type: "number", nullable: true },
                    log_groupSource: { type: "string", nullable: true },
                    log_isTank: { type: "string", nullable: true },
                    log_meterReadClose: { type: "number", nullable: true },
                    log_obsTemperature: { type: "number", nullable: true },
                    log_tankTemperature: { type: "number", nullable: true },
                    log_obsGravity: { type: "number", nullable: true },
                    log_bswPercent: { type: "number", nullable: true },
                    log_gaugeBeforeUnloadFeet: { type: "number", nullable: true },
                    log_gaugeBeforeUnloadInches: { type: "number", nullable: true },
                    log_gaugeBeforeUnloadQuarter: { type: "number", nullable: true },
                    log_comments: { type: "string", nullable: true }
                }
            }
        },

        pageSize: 50,
        serverPaging: true,
        serverfilering: true,
        serverSorting: true,
        sort: { field: "log_id", dir: "asc" },
    });

    //-----------------KENDO GRID-----------------
$("#grid").kendoGrid({
        dataSource: datasource,
        pageable:
            {
                refresh: true,
                pageSizes: [10,25,50,100],
                buttonCount: 5
            },
        height: 600,
        width: 500,
        sortable: true,
        scrollable: true,
        reorderable: true,
        toolbar: ["save", "cancel"],
        editable: true,
        filterable: {
            mode: "row"
        },
        selectable: "row",
        resizable: true,
        detailInit: detailInit,
        dataBound: onDataBound,
        columns: [
            //{ field: "log_id", title: "ID", width: 130 }, //THIS IS FOR TESTING PURPOSES
            {
                field: "log_paperTicketIsApproved",
                title: "Approved",
                width: 130,
                template:
                    "# if(log_paperTicketIsApproved == 'Y') { #" +
                        "# approved = true #" +
                    "# } else { #" +
                        "# approved = false #" +
                    "# } #" +
                    "<input name='paperTicketIsApproved' class='check_row' type='checkbox' data-bind='checked: approved' #= approved ? checked='checked' : '' #/>"
            },
            { field: "log_type", title: "Source", width: 130 },
            { field: "log_rackNumber", title: "Connect Point (LACT)", width: 150 },
            { field: "log_sourceCode", title: "Carrier Source Code", width: 140 },
            { field: "log_carrierName", title: "Carrier Name", width: 130 },
            { field: "log_ticketNumber", title: "Ticket Number", width: 130 },
            { field: "log_deliveryTS", title: "Date", width: 160, format: "{0:MM-dd-yyyy}" },
            { field: "log_deliveryTS", title: "Time", width: 140, format: "{0:hh:mm tt}" },
            { field: "log_volumeGross", title: "Gross BBLs", width: 140 },
            { field: "log_volumeNet", title: "Net BBLs", width: 140 },
            { field: "log_leaseName", title: "Lease Name", width: 200 },
            { field: "log_driverFirstName", title: "Driver First Name", width: 160 }, // template: "#= log_driverFirstName # #=log_driverLastName #" },
            { field: "log_driverLastName", title: "Driver Last Name", width: 160 },
            { field: "log_bswPercent", title: "Obs. BS&W %", width: 140 },
            { field: "log_obsGravity", title: "Obs. Gravity", width: 130 },
            { field: "log_obsTemperature", title: "Obs. Temperature", width: 150 },
            { field: "log_linkedTicketNumber", title: "Linked Ticket Number", width: 150 },
            { field: "log_originalDestination", title: "Original Destination", width: 150 },
            { field: "log_lastModifiedBy", title: "Last Modified By", width: 170 },
            { field: "log_lastModifiedTS", title: "Last Modified Date", width: 170, format: "{0:MM-dd-yyyy hh:mm tt}" },
            //{ field: "log_station", title: "Station", width: 140 } //THIS IS FOR TESTING PURPOSES ONLY
        ]
    });

    var grid = $("#grid").data("kendoGrid");
    //grid.bind("dataBound", onDataBound);
    grid.table.on("click", ".check_row", selectRow)
}

这是子网格

//-----------------KENDO CHILD-GRID DATA-----------------
function detailInit(e) {
    var datasource = new kendo.data.DataSource({
        type: "odata",
        transport: {
            read: {
                url: baseUrl + "StationLogComments",
                type: "get",
                dataType: "json"
            },
            update: {
                type: "put",
                url: function (data) {
                    return baseUrl + "StationLogComments(" + data.log_commentID + ")";
                },
                dataType: "json",
                contentType: "application/json"
            },
            create: {
                url: baseUrl + "StationLogComments",
                type: "post",
                dataType: "json"
            },
        },

        schema: {
            data: "value",
            total: function (data) {
                return data['odata.count'];
            },
            model: {
                id: "log_commentID",
                fields: {
                    log_commentID: { type: "number" },
                    log_id: { type: "number" },
                    log_comment: { type: "string" },
                    log_commentCreatedBy: { type: "string" },
                    log_commentCreatedDate: { type: "date", editable: false }
                }
            }
        },
        serverPaging: true,
        serverSorting: true,
        sort: { field: "log_commentCreatedDate", dir: "asc" },
        serverFiltering: true,
        width: 100,
        pageSize: 10,
        filter: { field: "log_id", operator: "eq", value: e.data.log_id }
    });


    //-----------------KENDO CHILD-GRID-----------------
    $("<div/>").appendTo(e.detailCell).kendoGrid({
        dataSource: datasource,
        scrollable: false,
        toolbar: ["create", "save", "cancel"],
        saveChanges: function (e) {
            debugger;

        },
        editable: true,
        resizable: true,
        width: 600,
        //sortable: true,
        //pageable: true,
        columns: [
            { field: "log_id", title: "log_id", width: "100px" },
            { field: "log_comment", title: "Comment", width: "500px" },
            { field: "log_commentCreatedBy", title: "Created By", width: "100px" },
            { field: "log_commentCreatedDate", title: "Created Date", format: "{0:MM-dd-yyyy hh:mm tt}" }
        ]
    });
}

我可以在saveChanges中添加什么:获得我想要的结果?感谢

1 个答案:

答案 0 :(得分:0)

RESOLVED!!

I found my own answer and am posting it here in the hopes that it may help someone else..

In the model of my Child Grid I add this line of code to the foreign key reference:

defaultValue: e.data.log_id

so now my child grid model looks like this

            model: {
                id: "log_commentID",
                fields: {
                    log_commentID: { type: "number" },
                    log_id: { type: "number", defaultValue: e.data.log_id },
                    log_comment: { type: "string" },
                    log_commentCreatedBy: { type: "string" },
                    log_commentCreatedDate: { type: "date", editable: false }
                }