How can one count the number of results from a JSON request before it is rendered into a list or grid?
Also, how can one append to a list or grid rather than overwriting it with new data?
How can one count the number of results from a JSON request before it is rendered into a list or grid?
Also, how can one append to a list or grid rather than overwriting it with new data?
On service's success event run this JavaScript:
code
var count = data.results.length;
/code
'results' is the actual array name, the variable in your JSON object might be different.
As for appending, it has to be implemented by the developer. One option is to save all data into local storage, combine, and then run a Generic Service to do the mapping: https://getsatisfaction.com/tiggzi/to...
Max-I am trying to count my collection results as you describe:
I run this on service success:
//the following line does not get count and creates exception of data is not defined / cannot read property length of undefined.
//var count = data.results.length;
//this logs count correctly but creates exception that data is not defined.
var count = data.length;
console.log(count);
localStorage.setItem('total', count);
I am confused about how I could be getting the correct count and the error at the same time. How to I access the length correctly?
Hi Sean,
The variable "data" points to data got from Rest Service.
In Max's example array was stored in response parameter "result". If Rest service returns an array then your code for array length definition is correct:
codevar count = data.length;/code
The error "data is not defined" may be caused by JS code in another Action. Do you have an Action on "Complete" event?
the array makes sense. With regard to the error on data, I did have a complete action but deleted it. I now only have one action under Data on success.
When I watch the console, the exception occurs after the successful count is logged. I thought that I might have lest a test bit of code somewhere else but cannot find it. both the count result and the error occur in the same js file
Uncaught ReferenceError: data is not defined startScreen.js:299
$.die.live.click startScreen.js:299
jQuery.event.dispatch jquery-1.8.2.js:3063
elemData.handle.eventHandle
thanks- the problem appeared to be with execution order. I invoked service on button click, then page transitioned, but on data side I ran script to get count on service success. By moving page transition to data side after script ran, error went away. thanks.
hello! I am looking for a similar line of code on Success.
my count query works when tab-test the service.
however, may I ask you for the proper line of code to forward that count result to a localStorage or a Appery label ?
thk you in advance,
Eric
Hello EJLD,
Custom code is outside the scope of our support, please show us what you have tried and what doesn't work.
thks for your prompt feedback.
I managed to find the way to map the count.
best
This was fantastically helpful to me just now - not only for the count, but for getting the value of a specific record index (e.g. var t1 = data[3].lastname). Thank you Kateryna.