Skip to content

Instantly share code, notes, and snippets.

@bgriggs1
Created October 13, 2014 15:45
Show Gist options
  • Save bgriggs1/6107ed1238405415c254 to your computer and use it in GitHub Desktop.
Save bgriggs1/6107ed1238405415c254 to your computer and use it in GitHub Desktop.
Update a date/time field with an inline table edit
// 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) {}
});
});
@wylie39
Copy link

wylie39 commented Aug 13, 2020

This is awesome, thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment