Page 1 of 4

saving an array in localstorage

Posted: Mon Nov 19, 2012 12:23 pm
by Michael4771079

Hi,
i need to save every address a user enters into a text input in local storage as an array, Have you any details on this.

thank you


saving an array in localstorage

Posted: Mon Nov 19, 2012 12:48 pm
by Maryna Brodina

Here is the code which saves element item to varName variable

function addItemToArray(item, varName) { var arr = JSON.parse(localStorage.getItem(varName)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}


saving an array in localstorage

Posted: Mon Nov 19, 2012 1:55 pm
by Michael4771079

thanks marina,
do i run the script on click "save to favourites"

Image


saving an array in localstorage

Posted: Mon Nov 19, 2012 2:03 pm
by Maryna Brodina

Yes


saving an array in localstorage

Posted: Mon Nov 19, 2012 4:16 pm
by Michael4771079

Hi Marina,
I've done something wrong,
on textinput component I set action on input, set local storage, "favourites, bind to favourites, and ran the following script onclick, save to favourites button

function addItemToArray(item, favourites) { var arr = JSON.parse(localStorage.getItem(favourites)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}

it only saves the last entry, can tell where I went wrong

thank you for you help.


saving an array in localstorage

Posted: Tue Nov 20, 2012 11:56 am
by Maryna Brodina

Hello! You should add this code as Custom JS without changes. It's a static code

function save(item, varName) {
var arr = JSON.parse(localStorage.getItem(varName));
if(arr == null) {
arr = new Array();
}
arr.push(item);
localStorage.setItem(varName, JSON.stringify(arr))
}

function myload(varName) {
var arr = JSON.parse(localStorage.getItem(varName));
if(arr == null) {
arr = new Array();
}
return arr;
}

Then to save an array on button click run next code

save(Tiggzi('component_name').text(), 'variable_name')


saving an array in localstorage

Posted: Tue Nov 20, 2012 1:38 pm
by Michael4771079

Thanks Marina,

I have included the static code in create new java,

as yesterday, the text input id is "favourites", the click button id is "favourites button".

On input event I set local storage variable, and bound favourites to favourites (text)

on favourites button I set event onclick run custom java and used the code you gave me yesterday

function addItemToArray(item, favourites) { var arr = JSON.parse(localStorage.getItem(favourites)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}

I have this other code you gave me today

save(Tiggzi('component_name').text(), 'variable_name')

do I run only one code onclick or both


saving an array in localstorage

Posted: Tue Nov 20, 2012 2:24 pm
by Maryna Brodina

Try to use only this code save(Tiggzi('component_name').text(), 'variable_name')

Change component names and variable names


saving an array in localstorage

Posted: Tue Nov 20, 2012 3:09 pm
by Michael4771079

super cool Marina,
this is the result from localstorage,

variable_favourites "["","","","","","",""]"

is there any docs on how to populate a selectmenu with this array

kind regards


saving an array in localstorage

Posted: Wed Nov 21, 2012 11:15 am
by Michael4771079

Hi Marina,
this is the result in local storage in firebug, each time I save to favourites I get ,"" added to localstorage variables_favourites, but no text
I thought that the text would be there, is this correct?

variable_favourites "["","","","","","",""]"