Completing my code! - Copying a record between two objects
Objective:
I have a table which has a column of check boxes in the first column. Then I have a button which applies an effect to the selected rows in that table. The button
should copy the selected row, to another object.
What the Code Below Currently Does:
The code below applies the check boxes to the table, and places the button on the page.
What is Missing from the Code Below?
The action of the button, which copies the selected records to another table (object).
The Code:
- // ------ Function to Add to Checkboxes ------
-
- 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>');
- });
- }
-
-
- // ------ Function to Add to Cart on Quotes 1 Interface ------
-
-
- $(document).on('knack-view-render.view_54', function (event, view) {
- // Adds button and calls checkbox function to view
- $('<button id="update"">Add to Cart</button>').insertAfter('.kn-filters-nav');
- addCheckboxes(view);
-
- $('#update').click(function () {
- console.log(view.key);
- var checked = $('#' + view.key + ' tbody input[type=checkbox]:checked');
-
- calls = checked.length;
- success = 0;
-
- Knack.showSpinner();
-
- checked.each(function() {
-
- var id = $(this).closest('tr').attr('id'); // record id
-
- var data = {
- field_246: Knack.getUserAttributes().id
- };
-
- $.ajax({
- url: 'https://api.knackhq.com/v1/objects/object_1/records/' + id,
- type: 'PUT',
- headers: {'X-Knack-Application-ID': 'XXXXXXXXXXXXXX': 'XXXXXXXXXXXXXXX'},
- data: data,
- success: function (response) { // each time an update completes, our success variable increases by 1
- success+=1;
- if(success==calls) { // if the number of success calls = the number of checked boxes...
- Knack.hideSpinner();
- alert('Quotes added to your cart.');
- }
- }
- });
- });
- })
- });
Database is built on a platform called Knack. It's 100% online. Appreciate any help!! I'm not an expert here, and am working off examples.
Thanks,
Robert