inline editor - need to create, not update
inline editor - need to create, not update
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
here is my Editor
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
what I ended up doing was just doing an insert in the controller instead of the client side
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.