Page 1 of 2

Count results from JSON request

Posted: Fri May 11, 2012 3:47 pm
by Chris2656882

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?


Count results from JSON request

Posted: Fri May 11, 2012 6:58 pm
by maxkatz

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...


Count results from JSON request

Posted: Tue Mar 26, 2013 7:04 pm
by Sean Kelley

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?


Count results from JSON request

Posted: Tue Mar 26, 2013 7:52 pm
by Kateryna Grynko

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?


Count results from JSON request

Posted: Tue Mar 26, 2013 8:43 pm
by Sean Kelley

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


Count results from JSON request

Posted: Tue Mar 26, 2013 8:57 pm
by Sean Kelley

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.


Count results from JSON request

Posted: Sun Aug 31, 2014 2:32 pm
by EJLD

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


Count results from JSON request

Posted: Sun Aug 31, 2014 10:14 pm
by Alena Prykhodko

Hello EJLD,

Custom code is outside the scope of our support, please show us what you have tried and what doesn't work.


Count results from JSON request

Posted: Mon Sep 01, 2014 8:23 am
by EJLD

thks for your prompt feedback.
I managed to find the way to map the count.
best


Count results from JSON request

Posted: Mon Oct 06, 2014 4:27 pm
by RobertJay

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.