inline editor - need to create, not update

inline editor - need to create, not update

montoyammontoyam Posts: 568Questions: 136Answers: 5

I have an odd situation. I have a parent table (SDPlus_Worklogs) and a child table (SDPlus_Approvals). I am trying to do an inline editor with a checkbox. when the user clicks the checkbox I need to create a record in the child table. The parent table is read only and will never be edited. A child record will not exist until the user clicks the checkbox (eventually I will not allow the checkbox to be un-checked once it is checked).

So, I have the following code from the inline checkbox examples on this site, but instead of editing, i tried to change it to create.

    $('#SDPlus_BillableItems').on('change', 'input.editor-active', function () {
        d = new Date($.now());
        var tr = $(this).closest('tr');
        var row = BillableItemsTable.row(tr);
        selectedpkID = row.data().SDPlus_Worklogs.PK_ID;
        BillableItemsEditor
            //.create(false)
            .create({
                title: 'Add new record',
                buttons: 'Save'
            })
            .set('SDPlus_Approvals.WorkLog_PK_ID', $(this).prop('checked') ? selectedpkID : '')
            .set('SDPlus_Approvals.ApprovedBy', $(this).prop('checked') ? 'UserNameHere' : '')
            .set('SDPlus_Approvals.ApprovedDate', $(this).prop('checked') ? d : '')
            //.submit();
            ;
    });

I added a debug in my controller and see that an update is being triggered, not an insert:

    public class SDPlus_BillableItemsController : ApiController
    {
        [Route("api/SDPlus_BillableItems")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult SDPlus_BillableItems()
        {
            var request = HttpContext.Current.Request;
            var settings = System.Configuration.ConfigurationManager.ConnectionStrings["msSql"];
            using (var db = new Database("sqlserver", settings.ConnectionString))
            {
                {
                    DateTime thisDay = DateTime.Today;
                    var response = new Editor(db, "udf_SDPlusWorkLogItems ('" + thisDay + "',3) as SDPlus_Worklogs", "PK_ID")
                .Model<SDPlus_BillableItemsModel>("SDPlus_Worklogs")
                .Field(new Field("SDPlus_Worklogs.DateCreated")
                    //.SetFormatter(Format.NullEmpty())
                    .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                )
                .Field(new Field("SDPlus_Worklogs.LogStartTime")
                    //.SetFormatter(Format.NullEmpty())
                    .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                )
                .Field(new Field("SDPlus_Worklogs.LogEndTime")
                    //.SetFormatter(Format.NullEmpty())
                    .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                )
                .LeftJoin("SDPlus_Approvals", "SDPlus_Approvals.WorkLog_PK_ID", "=", "SDPlus_Worklogs.PK_ID")
                    .Field(new Field("SDPlus_Approvals.ApprovedBy"))
                    .Field(new Field("SDPlus_Approvals.ApprovedDate")
                        .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                    )
                .Debug(true)
                .Process(request)
                .Data();
                    return Json(response);
                }
            }
        }
    }
[
  {
    "Query": "UPDATE  [SDPlus_Approvals] SET  [ApprovedBy] = @ApprovedBy, [ApprovedDate] = @ApprovedDate WHERE [WorkLog_PK_ID] IS NULL ",
    "Bindings": [
      {
        "Name": "@ApprovedBy",
        "Value": "UserNameHere",
        "Type": null
      },
      {
        "Name": "@ApprovedDate",
        "Value": "Fri Jul 02 2021 15:32:44 GMT-0700 (Pacific Daylight Time)",
        "Type": null
      }
    ]
  }
]

I can't have the primary table be the approval table with a left join to the worklogs table because there are no records for approval until the user will click the approval checkbox.

But if I am using .create() instead of .edit(), why is the query being sent an 'update' query?

Answers

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    here is my Editor

        var BillableItemsEditor = new $.fn.dataTable.Editor({
            ajax: 'api/SDPlus_BillableItems',
            table: '#SDPlus_BillableItems',
            fields: [
                { label: "PK_ID", name: "SDPlus_Approvals.WorkLog_PK_ID" }
                , { label: "ApprovedBy", name: "SDPlus_Approvals.ApprovedBy" }
                , { label: "ApprovedDate", name: "SDPlus_Approvals.ApprovedDate" }
            ]
        });
    
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Do you have another checkbox with the class editor-active perhaps? I'm not seeing anything wrong with the code above.

    If you have a look at the data submitted to the server in the browser's network inspector, what does it show the parameters being sent as? I'm particularly interested in the action parameter.

    Thanks,
    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    what I ended up doing was just doing an insert in the controller instead of the client side

                    response.PreEdit += (sender, e) => _CreateApproval(db, "create", e.Id, e.Values);
    
                    return Json(
                        response.Process(request).Data()
                    );
    
                }
            }
    
            private void _CreateApproval(Database db, string action, object id, Dictionary<string, object> values)
            {
                var data = JsonConvert.SerializeObject(values);
                JObject json = JObject.Parse(data);
                DateTime localDate = DateTime.Now;
                var approvedBy = "";
                db.Insert("SDPlus_Approvals", new Dictionary<string, object>{
                    { "WorkLog_PK_ID",id },
                    { "ApprovedBy",  approvedBy }
                    //{ "ApprovedDate", localDate.ToString() }
                });
            }
    

    then just did the standard code as shown in the example: https://editor.datatables.net/examples/api/checkbox.html

    and let the Editor send the update to the newly created record. odd work-around, but it worked.

Sign In or Register to comment.