Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1175
Update Primary Key from Web API Response
posted

I've successfully used the igGrid to hook into our Web API.  Create, Remove and Delete operations are firing nicely and playing nicely together.  The only scenario that is currently failing is this:

  1. User creates a new record.
  2. igGrid creates a new ID - specifically -1.
  3. An API request is sent to create the new record with an ID of -1.
    POST:  /api/carrier
  4. API creates the new record and returns a request with the actual ID of 100.
  5. I need to update the primary key of record in the grid to 100.
  6. User deletes the new record.
    DELETE:  /api/carrier/-1        (WRONG)
    DELETE:  /api/carrier/100      (EXPECTED)

    Server won't be able to find -1.

In the "iggridupdatingrowadded" event I call "SaveChanges" to the grid to force the API call.

rowAdded: function (e, ui) {
    var original = ui;

    ui.owner.grid.saveChanges(function (record, status, response) {
        var row = this.findRecordByKey(original.rowID);

        row[original.owner.grid.options.primaryKey] = record[original.owner.grid.options.primaryKey];

        this.updateRow(original.rowID, row);
    });
}

When the transaction commits, it is failing to map the transaction with an -1 ID when it should be 100. 

Technical Details:

v14.2

Method: _commitTransaction

this.origDs has a record value of -1.

Unhandled exception at line 244, column 17855 in http://localhost:51580/admin/Scripts/IG/js/infragistics.core.js

0x800a138f - JavaScript runtime error: Unable to set property 'Id' of undefined or null reference

How can I correctly programmatically change the primary key value so the entire grid will function as normal?

Thanks,

Parents Reply
  • 2745
    Offline posted in reply to Karthik Thiagarajan

    Sorry I misunderstood what you meant by data binding. I modified the event and now it is correctly seeding the value. We have another event handler for deleting rows, here's the entire igGrid definition:

    $.ced.ig.grid({
                    id: "igGridCarriers",
                    allowPaging: false,
                    allowHiding: false,
                    allowUndoRedo: false,
                    emptyDataText: "THERE ARE NO CARRIERS AVAILABLE. CLICK TO ADD A CARRIER.",
                    primaryKey: "Id",
                    columns: [
                        { key: "Id", dataType: "number", hidden: true },
                        { key: "CarrierDescription", dataType: "string", headerText: "Description" },
                        { key: "StandardCarrierAlphaCode", dataType: "string", headerText: "AlphaCode" },
                        { key: "Display", dataType: "string", headerText: "Display", unbound: true, template: "${StandardCarrierAlphaCode} - ${CarrierDescription}" },
                        { key: "SortOrder", dataType: "number", hidden: true },
                        { key: "CreatedBy", dataType: "number", hidden: true },
                        { key: "CreatedDate", dataType: "date", hidden: true },
                        { key: "UpdatedBy", dataType: "number", hidden: true },
                        { key: "UpdatedDate", dataType: "date", hidden: true },
                        {
                             key: "Actions",
                             dataType: "string",
                             headerText: "",
                             width: "148px",
                             align: "center",
                             template: "×",
                             unbound: true
                        }
                    ],
                    features: [
                        {
                            name: "Filtering",
                            columnSettings: [
                                { columnKey: "Display", allowFiltering: false },
                                { columnKey: "Actions", allowFiltering: false },
                            ]
                        },
                        {
                            name: "Sorting",
                            columnSettings: [
                                { columnKey: "Display", allowSorting: false },
                                { columnKey: "Actions", allowSorting: false },
                            ]
                        },
                        {
                            name: "Updating",
                            editMode: "cell",
                            autoCommit: false,
                            enableAddRow: true,
                            enableDeleteRow: false,
                            columnSettings: [
                                { columnKey: "Display", readOnly: true },
                                { columnKey: "Actions", readOnly: true }
                            ],
                            rowAdding: function (e, ui) {
                                ui.values.CreatedDate = $.ig.formatter(ui.values.CreatedDate || new Date(), "date", "MM/dd/yyyy h:mm:ss tt", true, true, null);
                                ui.values.CreatedBy = ui.values.CreatedBy || 1;
    
                                ui.values.UpdatedDate = $.ig.formatter(new Date(), "date", "MM/dd/yyyy h:mm:ss tt", true, true, null);
                                ui.values.UpdatedBy = 1;
                            },
                            rowAdded: function (e, ui) {
                                var original = ui;
    
                                ui.owner.grid.saveChanges(function (record, status, response) {                                
                                    ui.owner.updateRow(original.rowID, record);
                                    original.owner.grid.dataBind();
                                });
                            },
                            editCellEnding: function (e, ui) {
                                if (ui.rowAdding) {
                                    return;
                                }
    
                                if (ui.update === false) {
                                    return false;
                                }
    
                                var row = ui.owner.grid.findRecordByKey(ui.rowID);
    
                                row.CreatedDate = $.ig.formatter(row.CreatedDate || new Date(), "date", "MM/dd/yyyy h:mm:ss tt", true, true, null);
                                row.CreatedBy = row.CreatedBy || 1;
    
                                row.UpdatedDate = $.ig.formatter(new Date(), "date", "MM/dd/yyyy h:mm:ss tt", true, true, null);
                                row.UpdatedBy = 1;
    
                                ui.owner.updateRow(ui.rowID, row);
                            },
                            editCellEnded: function (e, ui) {
                                if (ui.rowAdding) {
                                    return;
                                }
    
                                ui.owner.grid.dataSource.saveChanges();
                            }
                        }
                    ],
                    restSettings: {
                        create: {
                            batch: false,
                            template: $.ced.createUri("/api/carrier"),
                            url: $.ced.createUri("/api/carrier")
                        },
                        update: {
                            batch: false,
                            template: $.ced.createUri("/api/carrier/${Id}"),
                            url: $.ced.createUri("/api/carrier")
                        },
                        remove: {
                            batch: false,
                            template: $.ced.createUri("/api/carrier/${Id}"),
                            url: $.ced.createUri("/api/carrier")
                        }
                    },
                    dataSource: $.ced.createUri("/api/carrier")
                });

    Our remove template is setup for deleting items but this is what we see: DELETE /admin/api/carrier/$%7BId%7D HTTP/1.1 The template is escaping the values instead of seeding it with the ID. Updates work great! What gives?

Children