AC
Posts: 0
Joined: Wed Oct 08, 2014 8:53 pm

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

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!

AC
Posts: 0
Joined: Wed Oct 08, 2014 8:53 pm

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

(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?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

Return to “Issues”