0

I tried the solution posted on this [adding objects to array in localStorage but it doesnt seem to work for me.Mine has a small change,I am getting the values as a list from python flask app:

    var myValue = {{ List|safe }};
    onload=function appendRow() {
    var names = [];
    names = myValue;
    var existingEntries = JSON.parse(localStorage.getItem("allEntries")) || [];
    localStorage.setItem("oltbl", JSON.stringify(names));
    existingEntries.push(oltbl);
    localStorage.setItem("allEntries", JSON.stringify(existingEntries))
    var storedNames = JSON.parse(localStorage.getItem("allEntries"));
    var tbl = document.getElementById('mixed'), 
    row = tbl.insertRow(tbl.rows.length),     
    i;
    for (i = 0; i < tbl.rows[0].cells.length; i++) {
    createCell(row.insertCell(i), storedNames[i], 'row');
     }
     }

    function createCell(cell, text, style) {
    var div = document.createElement('div'), 
    txt = document.createTextNode(text); 
    div.appendChild(txt);                    
    cell.appendChild(div);                  
    }
    </script>
    <body>
    <table id="mixed">
    <tr>
    <th>Title</th>
    <th>Text</th>
    <th>STATUS</th>
    </tr>

Any pointers?

6
  • what happens? do you get any errors? Commented Sep 27, 2017 at 9:42
  • I dont get any output in the table,the table is empty Commented Sep 27, 2017 at 9:46
  • when I have just this: localStorage.setItem("oltbl", JSON.stringify(names)); var storedNames = JSON.parse(localStorage.getItem("oltbl")); It rewrites evrytime the function gets called. I want to maintain a table everytime this page is rendered. Commented Sep 27, 2017 at 9:49
  • it should be var storedNames = JSON.parse(localStorage.getItem("allEntries")); instead of var storedNames = JSON.parse(localStorage.getItem("oltbl")); and oltbl is not defined, so you push nothing into your array Commented Sep 27, 2017 at 9:56
  • @oliv37 I have it like "JSON.parse(localStorage.getItem("allEntries"))" in my code,this was done just for testing purpose. Commented Sep 27, 2017 at 10:02

1 Answer 1

0

You're always setting the allEntries the result of itself, but since you never set a thing to it, it's blank

You should rethink what exactly what you want to achieve.

also

existingEntries.push(oltbl); /*oltbl IS NOT ASSIGNED*/

and causes js error

Sign up to request clarification or add additional context in comments.

7 Comments

Ok,Can I have it set as an array var oltbl=[]; localStorage.setItem("oltbl", JSON.stringify(names)); This prints out [object Object] in the html page. Basically I need to add entries of some values returned by flask app in the table without overwriting.
So you need to update your allEntries adding some new values?
yes each time new values get added to the array Ive removed allEntries and tried printing the existingEntries array and this is what i got: localStorage.setItem("entry", JSON.stringify(entry)); existingEntries.push(entry); document.getElementById('whereToPrint').innerHTML = JSON.stringify(existingEntries); var retrievedData = JSON.parse(localStorage.getItem("existingEntries")); document.getElementById('whereToPrint1').innerHTML = JSON.stringify(retrievedData); existingEntries array gets printed,but retrievedData prints null.any idea why?
check your local storage using the chrome inspector window for example. open it (F12) > Application > Local Storage and see if you have what you expect
Hi Daniel, I am able to see the entries getting populated in LocalStorage->allEntries but when printing on html page it just shows up like this []
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.