Using Checkboxes in tables to set a flag in existing or other database

I was using the existing checkbox code on the site and playing around with it for a database app I am writing. I needed to set a flag to hide a table row once it had been updated. I also needed to set a flag within another knack database. I put this together as a test to see if I could achieve it with the API and Checkbox routine. It works for me and I am sure I will use many times for other purposes.

/*This code will read a table's check box and set a single or multiple fields to a given value*/ /*--------------------------------------------------------------------- Set the flag in single object ---------------------------------------------------------------------*/ function SetFieldFlag(id,data,KId,KKey,Kobject) { $.ajax({ url: 'https://api.knack.com/v1/objects/' + Kobject + '/records/' + id , type: 'PUT', headers: { 'X-Knack-Application-Id':KId, 'X-Knack-REST-API-Key': KKey }, data : data, success: function(data) { alert('Successful PUT'); //Turn this on when debugging location.reload(); //refresh the screen after the record is inserted. } }); } /*-----------------------------------------------------------------------------------------*/ // The second function is used as the “data” array does not work within the checkbox loop //routine. function CallSetFlag(id) { // Here you can set the flag or value of multiple fields if you like var Senddata = {} // This is where you set the field values Senddata = { field_20 : '1' } SetFieldFlag(id,Senddata,ApplicationID, APplicationKey','object_1X); // Use yourn own API and object_# } /*--------------------------------------------------------------------------------------- This code adds check boxes at the end of the table using append to the nominated view. -----------------------------------------------------------------------------------------*/ var addCheckboxes = function(view) { // add the checkbox to to the header to select/unselect all $('#' + view.key + '.kn-table thead tr').append('<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).append('<td><input type="checkbox"></td>'); }); //$('table tr>th').last().html('Delete'); // Remove the comments in front if you want the check all box removed at the header and replaced by text such as 'Delete' $('table tr>th').last().css('width','10px'); // Set the width of the table to a small size 10PX } /*------------------------------------------------------------------------------------------------------------------------------------------------------ /**** Example code to access above functions. The checkbox routine is useful to retrieve the record ID. Without it you will need to do this manually /------------------------------------------------------------------------------------------------------------------------------------------------------- */ $(document).on('knack-view-render.view_X', function (event, view) { addCheckboxes(view); // Add an update button $('<button id="update"">Update</button>').insertAfter('.view-header'); // Click event for the update button $('#update').click(function () { Knack.showSpinner();//Indicate something is happening var record_ids = []; // This array stores the record ID of the items that are checked. // 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')); // push the record id into the array CallSetFlag($(this).closest('tr').attr('id')); }); Knack.hideSpinner(); //location.reload(); //refresh the screen after the record is inserted. }); });