This first requires you to add the checkbox snippet from this example: Add checkboxes to a table
This following snippet will add an "Update" button below your view header and update all checked records with the data you specify in the data variable.
Once all records are updated, an alert will display confirming the update.
Note: This still needs some work! There's no function to catch errors, etc.
var addCheckboxes = function(view) {// add the checkbox to to the header to select/unselect all
$(’#’ + view.key + ‘.kn-table thead tr’).prepend(’<th><input type=“checkbox”></th>’);$(’#’ + view.key + ‘.kn-table thead input’).change(function() {
$(’.’ + view.key + ‘.kn-table tbody tr input’).each(function() {
$(this).attr(‘checked’, $(’#’ + view.key + ‘.kn-table thead input’).attr(‘checked’) != undefined);
});
});// add a checkbox to each row in the table body
$(’#’ + view.key + ‘.kn-table tbody tr’).each(function() {
$(this).prepend(’<td><input type=“checkbox”></td>’);
});
}var updateRecords = function(id, records, data) {
$.ajax({
url: ‘https://api.knackhq.com/v1/objects/object_1/records/’ + id,
type: ‘PUT’,
/***** CHANGE TO YOUR OWN APPID AND API KEY HERE *****/
headers: {‘X-Knack-Application-ID’: ‘YOUR-APP-ID’, ‘X-Knack-REST-API-Key’: ‘YOUR-API-KEY’},
data: data,
success: function(response) {
if ( records.length ) {
// Every time a call is made, the array is shifted by 1.
// If the array still has a length, re-run updateRecords()updateRecords(records.shift(), records); } else { // We know all records are processed when length = 0 alert('Update complete!'); Knack.hideSpinner(); } }
})
}/**** CHANGE VIEW_ID TO YOUR OWN VIEW ID ****/
$(document).on(‘knack-view-render.VIEW_ID’, function (event, view) {// Add an update button
$(’<button id=“update”">Update</button>’).insertAfter(’.view-header’);// Add checkboxes to our table
addCheckboxes(view);// Click event for the update button
$(’#update’).click(function () {// We need an array of record IDs var record_ids = []; // Populate the record IDs using all checked rows $('#' + view.key + ' tbody input[type=checkbox]:checked').each(function() { record_ids.push($(this).closest('tr').attr('id')); // record id }); Knack.showSpinner(); // Define the fields you want to update var data = { field_1: 'Closed' }; // Use the first ID in the array, then pass in the rest of the array updateRecords(record_ids.shift(), record_ids, data);
})
});