I have not posted for a while, but I thought this code may be good for the newbies who want to have check boxes in their tables and a button that actions each row checked. I use it to update a status flag but I am sure it could be used for a number of purposes
//---- Select the table you want to add the check boxes-----
$(document).on(‘knack-view-render.view_XX’, function (page) {
$(‘.kn-table .control.has-addons’).after(‘
$(‘.kn-table .kn-records-nav’).append(‘’)
$(‘.filtercolumnbtn’).click(function() {
$(“#columnButtons”).slideToggle(500)
});
$(‘.kn-table th’).each(function(index, e) {
var columnName = $(this).text();
var $button = $(‘’).text(columnName);
$button.click(function() {
$(this).toggleClass(‘hiddentab’);
var colIndex = index + 1;
$(‘.kn-table td:nth-child(’ + colIndex + ‘)’).slideToggle(500)
$(‘.kn-table th:nth-child(’ + colIndex + ‘)’).slideToggle(500)
});
$(‘#columnButtons’).append($button);
});
});
//------------------------ Add Check Boxes to Header ----------------------------------------------
var addCheckboxes = function(view) {
// add the checkbox to the header to select/unselect all
$(‘#’ + view.key + ‘.kn-table thead tr’).prepend(‘’);
$(‘#’ + 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(‘’);
});
}
// This is called when button is clicked.
var updateRecords = function(id, data, OK) {
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_X/records/'+id,
type: ‘PUT’,
/***** CHANGE TO YOUR OWN APPID AND API KEY HERE *****/
headers: {‘X-Knack-Application-ID’: ‘X’, ‘X-Knack-REST-API-Key’: ‘X’},
// Define the fields you want to update
data: data,
success: function(response) {
Knack.hideSpinner();
location.reload();
},
error: function() {
alert('Error');
}
});
Knack.hideSpinner();
// window.location.href = ‘Knack’ //Take user to their applications
//location.reload();
}
//------------- Add the button--------------------------------------------------------
$(document).on(‘knack-view-render.view_xx’, function (event, view) {
// Add an update button
$(‘<button id=“apply”">Send to Multiple Applicants’).insertBefore(‘.table-keyword-search’);
// Add checkboxes to our table
addCheckboxes(view);
// Click event for the update button which will then call updateRecords.
$(‘#apply’).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
var this_row = $(this).closest('tr').children('td.field_573').text();
var job_id =$(this).closest('tr').children('td.field_606').text();
//------ Define the fields you want to update-----
var MyVariable = 'X';
var data = {
field_XX: MyVariable
};
// Use the first ID in the array, then pass in the rest of the array
value = $("#txt_name").val();
// This is called when the button is clicked
updateRecords(record_ids.shift(),data); // loops through the checked records
});
Knack.hideSpinner();
})
});
//======================================================================