Page 1 of 1

how can I get all data from database and store in the local storage?

Posted: Wed May 13, 2015 2:10 pm
by AC

Sorry I am not familiar with the database stuff. But I have a list with 70 rows and 10 cols in my database as a collection and each cell has some texts. I want to download all those texts to the local storage when user click the start button. Then later I already use the math.random() to generate a number between 1 -70.I want to use this number to find the matching row in the list and draw the corresponding 10 cols texts to a popup. Is this possible to do with appery code or I have to do the text array input manually?

please help!


how can I get all data from database and store in the local storage?

Posted: Wed May 13, 2015 3:55 pm
by AC

(update): I use the list REST when I load page. I made a local variable and named it keys. I was trying to map the list to keys but it gave me [object Object] in the end.

How can I adjust it?


how can I get all data from database and store in the local storage?

Posted: Thu May 14, 2015 1:18 am
by Yurii Orishchuk

Hi Ac,

Yes it's possible.

Here is a solution:

  1. add list service datasource on the page.

  2. invoke this service datasource(1st step) on some event(button click, page show, etc) you need..

  3. open page in "Data" mode.

  4. add "success" event handler with action "Run javascript".

  5. Populate it with following JS code:

    pre

    var stringData = JSON.stringify(data);
    localStorage.setItem("localStorageName", stringData);

    /pre

    This will save all data from list/query service response to "localStorageName" lsv.

    Then you can get this stored data and get random item from it with following js code:

    pre

    var stringData = localStorage.getItem("localStorageName");
    var data = JSON.parse(stringData);

    var random = Math.random() * data.length;
    var limitedRandom = Math.round(random);

    //that's your random item. You can use it like you need.
    var randomItem = data[limitedRandom];

    /pre

    Regards.