Created
October 13, 2014 15:45
-
-
Save bgriggs1/6107ed1238405415c254 to your computer and use it in GitHub Desktop.
Update a date/time field with an inline table edit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this event handler will update a date/time field when a value is updated with inline editing | |
// change view_1 to the view key of the table with inline editing | |
$(document).on('knack-cell-update.view_1', function(event, view, data) { | |
// create a JS date object | |
var todayDate = new Date(); | |
todayDate.setHours(todayDate.getHours() - 7); // make any timezone changes | |
// create an object for the data to update | |
var update_vars = { | |
'field_1': todayDate.toJSON() // change field_1 to your date/time field key | |
}; | |
// call the Knack API | |
$.ajax({ | |
url: 'https://api.knackhq.com/v1/objects/object_1/records/' + data.id, // change object_1 to your object_key that the table is displaying | |
type: 'PUT', | |
contentType: 'application/json', | |
headers: { | |
'X-Knack-Application-Id': 'xxx', // change to your app's API info | |
'X-Knack-REST-API-Key': 'xxx' | |
}, | |
data: JSON.stringify( update_vars ), | |
success: function(response) {}, // this is happening behind the scenes so success and error not necessary | |
error: function(xhr, ajaxOptions, thownError) {} | |
}); | |
}); |
Many thanks for your example. This was the first time I'd tried to add a customisation to a Knack app and I do not classify myself as a developer. Worked a treat!
This is awesome, thanks so much.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome, thanks. Any idea how to manage the timezone changes automatically. We have two daylight saving changes a year?