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) {}
});
});
@ActuallyHonest
Copy link

Just wanted to thank you very much for documenting this code so clearly. I was looking for something similar to this, and have used your example here as a perfect working example.

@ActuallyHonest
Copy link

Would you be interested in helping me with my current goal of updating a Connecting field in Knack with the currently logged in users name? I am trying to do this for inline edits, and while your example above helped, and I am able to display the logged in user using Knack.getUserAttributes(), I cannot seem to get it to assign to a variable and connect to the record properly.

@Wasabiboy
Copy link

This is awesome, thanks. Any idea how to manage the timezone changes automatically. We have two daylight saving changes a year?

@SteveTomalin
Copy link

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!

@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