What is the best way to locally store user-entered

2019-09-08 04:02发布

问题:

I'm working on web app that basically allows the user to enter data about a product and then add it to the web page in a table. I would like to save this information in a file of some kind (just locally on my computer, for now) so that the user can open his or her previously-created entries.

This is a stripped-down version of the html page:

<!doctype html>
<html lang="en">
<head>
        <meta charset="utf-8">


</head>
<body>
    <div>
        <table id="itemsTable">
            <tr>
                <th>Date Added</th>
                <th>Item Description</th>
            </tr>
            </table>

    </div>
     <div id="itemInput">

         <h3>Add Item</h3>
             Date Added:<input type="date" id="dateAdded">
        Description:<input type="text" id="description">
<input type="button" value="Add Row" onclick="addRowFunction();">
<input type="button" value="Delete Selected" onclick="deleteRowFunction();">        
     </div>
</html>

I then have some javascript to evaluate and interpret the data, client-side. I'm looking for ideas to store the recorded entries (locally). Not only that, but I'm looking for suggestions as to how to delete information that's been stored as well.

EDIT:

As per your request, here is a snippet of my JS code:

//Striped-down object
function Item (dateListed, description) {
    this.dateListed = dateListed;
    this.description = description;

}

//Simple function to take data from form, create object, add to table.
function addItem() {
    var dateAdded = document.getElementById('dateAdded').value;
    var description = document.getElementById('description').value;

    // I realize that using an object in this striped-down version is kind of 
    // unnecessary, but in the full code it makes more sense.    
    var newItem = new Item(dateAdded, description);

    table = document.getElementById('itemsTable');
    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);

    cell1.innerHTML = newItem.dateListed;
    cell2.innerHTML = newItem.description;
}

回答1:

localStorage could work if you are not storing too much data, but IndexedDB might be a better choice, it has a higher default storage limit and allows you to store your data in a more structured way. IndexedDB allows you to store objects and anything else that can be cloned by the structured cloning algorithm without you having to manually serialize/deserialize it (localStorage can only store strings).

IndexedDB is kind of complicated and can be a pain to work with, it might be a good idea to use an abstraction layer. PouchDB is a fairly popular library that takes care of the details for you. Under the hood it uses IndexedDB (or other similar technologies depending on what the current browser supports) and presents you with a much nicer API to work with. One benefit of working with PouchDB is that if you end up using CouchDB on the server PouchDB can actually sync up with it.



回答2:

I would suggest localStorage or indexedDb.

The localStorage will be useful if you have very limited data manipulation involved. (CRUD operations can be done in this easily), but if you have more complicated manipulation involved, use indexedDb.



回答3:

Have you looked into window.localStorage? This is in modern browsers and allows you to store string representations of objects. See the spec on MDN.