Code to retrieve and display all records from an object

The below code retrieves all records from an object (here: object_1) through the API. The result of the Ajax GET is stored in object "data". The code then determines how many records the object contains (data.records.length) and uses this to write the records' fields.

(object_1 contains 2 fields, field_1 and field_2, that in my case contain first name and last name.)

Cheers

Bart


/ Retrieve all name records when clicking a button and show the results 
$(document).on('knack-view-render.view_4', function (event, view, data) {
// Add a button to execute the action
$("#view_4").prepend("<button id='showallnamerecords'>Show All Name Records</button>");

// Add the record function to the button
document.getElementById('showallnamerecords').addEventListener('click', function () {
	Knack.showSpinner();
	$.ajax({
 		url: 		"https://api.knackhq.com/v1/objects/object_1/records",
      	cache:		false,
	    type: 		"GET",
	    headers: 	{"X-Knack-Application-Id"	: "YOUR APP ID HERE", 
                  	"X-Knack-REST-API-Key"		: "YOUR API KEY HERE"
                    },
		success: 	function(data) 
      	{ 
          	Knack.hideSpinner();     
          
              	document.write("<p># of records in database is " + data.total_records + "</p>");
                                 
          	for (x=0; x<data.records.length ; x++) 
            {
              document.write("<p>Record " + x + ": " + data.records[x].field_1 + " " + data.records[x].field_2 + "</p>");
            };

        },

      	error: 		function(xhr)
      	{
          	Knack.hideSpinner();     
          	sweetAlert("An error occured! Status: " + xhr.status + " " + xhr.statusText);
    	}
	  }); // ajax 
  
});

});

Thanks for the code! I do seem to have a problem though. I have 128 records, but it's only showing 24, which is just enough to show on one page. Any thoughts as to why this would be?