Hello I am trying to update several records marked with a checkbox in a table the data I am trying to update is a connection field, the code that I currently have in addition to putting the checkbox in the table is supposed to update the records but only update a record and that does it if the field that I update is not a connected field.left the attached code
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_143/records/' + id,
type: 'PUT',
/***** CHANGE TO YOUR OWN APPID AND API KEY HERE *****/
headers: {'X-Knack-Application-ID': 'xxx', 'X-Knack-REST-API-Key': 'xxx'},
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_2465', function (event, view) {
// Add an update button
$('<button id="update"">Update</button>').insertAfter('.view_2465');
//$('<input type="text" id="update""></input>').insertAfter('.view_2465');
// 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_1875: "alv"
};
// Use the first ID in the array, then pass in the rest of the array
updateRecords(record_ids.shift(), record_ids, data);
})
});