# Using

**URL:** <https://forums.knack.com/t/using/15067>\
**Category:** Knacksters' Idea Lab\
**Created:** [October 30, 2023, 5:00am UTC](https://forums.knack.com/t/using/15067 "2023-10-30T05:00:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter2](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Peter2](https://forums.knack.com/u/Peter2)\
**Post date:** [October 30, 2023, 5:00am UTC](https://forums.knack.com/t/using/15067/1 "2023-10-30T05:00:41Z")

</div>

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(‘

Filter columns
’)  
$(‘.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](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](https://myapps.knack.com/peoplemanager#current-jobs2/)’ //Take user to their applications  
[//location.reload](https://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();  
})  
});

//======================================================================

---

<div class="post-metadata">

**Author:** ![KnackPros](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/knackpros/32/807_2.png) [@KnackPros](https://forums.knack.com/u/KnackPros)\
**Post date:** [October 30, 2023, 2:28pm UTC](https://forums.knack.com/t/using/15067/2 "2023-10-30T14:28:34Z")

</div>

To newbies reading this, it’s **not** advised to use [object-based requests](https://docs.knack.com/docs/object-based-requests) from client-side code, including custom JavaScript added directly to your Knack apps, as this will expose your API key.

This is because object-based requests give anyone unrestricted access to create/retrieve/update/delete records on all records in your database.

Instead, using a [view-based request](https://docs.knack.com/docs/view-based-requests) keeps your API key and database safe.

---

<div class="post-metadata">

**Author:** ![Peter2](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Peter2](https://forums.knack.com/u/Peter2)\
**Post date:** [October 30, 2023, 10:22pm UTC](https://forums.knack.com/t/using/15067/3 "2023-10-30T22:22:18Z")

</div>

Thanks for the reminder, I had changed to the view-based request but recently posted some old code with Object-Based.

Pete

![~WRD0001.jpg](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/5/52d5f1c08649212fe6768d2cacdcf8df711810d9.jpeg)

---

<div class="post-metadata">

**Author:** ![De\_Anna](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/de_anna/32/2383_2.png) [@De\_Anna](https://forums.knack.com/u/De_Anna)\
**Post date:** [November 2, 2023, 3:46pm UTC](https://forums.knack.com/t/using/15067/4 "2023-11-02T15:46:31Z")

</div>

@Peter2 Do you have a screenshot of what the table looks like?

---

<div class="post-metadata">

**Author:** ![Peter2](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Peter2](https://forums.knack.com/u/Peter2)\
**Post date:** [November 2, 2023, 9:44pm UTC](https://forums.knack.com/t/using/15067/5 "2023-11-02T21:44:49Z")

</div>

This is what it should look like

Pete

 ![IMG_0034.jpeg](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/5/52dc7ae15f37ff36eaae6edfe1d91c576e3f4d86.jpeg)
