Saving dates back to database with Editor form and Inline editing
Saving dates back to database with Editor form and Inline editing
Hi,
I am having an issue with saving dates back to my database in the correct format, i am using both inline and the editor form to edit the dates. They are loaded into the view with Json and then rendered as DD/MM/YYYY. I need to be able to edit them and then submit the form back to the controller. Dates that have been edited submit back correctly, however if a date is not edited then it is passed back as LatestDate: #1/1/0001 12:00:00 AM#.
data: "LatestDate",
title: "Latest Date",
type: 'date',
className: "dt-head-center dt-body-center editable",
render: DataTable.render.datetime('DD/MM/YYYY'),
width: "80px"
Edtior Form below
function convertJsonDateToShortDate(data) {
const dateValue = new Date(parseInt(data.substr(6)));
return dateValue.toLocaleDateString();
}
function isShortDateFormat(dateString) {
const regex = /^\d{2}\/\d{2}\/\d{4}$/;
return regex.test(dateString);
}
// build editor form
editor = new $.fn.dataTable.Editor({
table: "#dtPurchaseOrderExpedites",
idSrc: 'OrderNo',
formOptions: {
main: {
focus: 4
}
},
template: '#customForm',
fields: [
{
label: 'Latest date:',
name: 'LatestDate',
type: 'datetime',
format: "DD/MM/YYYY",
data: function (data) {
if (!isShortDateFormat(data.LatestDate)) {
data.LatestDate = convertJsonDateToShortDate(data.LatestDate);
}
return data.LatestDate;
}
}
],
}); //End Editor
Thanks in advance for any ideas.
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
What is the "wire format" - i.e. when you load the data into the DataTable, what is it read as? ISO8601?
I would strongly suggest you remove the
data
function from theEditor
initialisation. It can be used as a getter and setter, butfields.getFormatter
andfields.setFormatter
are much easier to work with.Allan
Hi Alan, the date is loaded in as a timestamp. I have attached a screenshot of when i remove the rendering completely. i had tried using the Get and Set formatters to deal with the dates but wasnt able to get it to display correctly.
I have included the code below, i may have made a mistake in it somewhere as the field was still displaying the timestamp.
Ah good old .NET. I presume you are Ajax loading the data for the DataTable? What script are you using to do that? Are you using our .NET libraries for Editor, or something else? Can the conversion to ISO8601 be done there?
Allan
Yeah i am using an ajax post to the load the Json into the datatable.
I have not been using the .NET libraries, i am not aware of how this works. Our controller returns a Json result with a collection of lines in an object. The object is made up of the properties in the model in which mine is a Date field.
We are quite new to this so may not be making the best use of all the tools available to us.