0

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;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • What are you using on the server? – Paul Jan 16 '15 at 05:31
  • As far as what goes? For now I'm just test-driving it as I develop is on my computer. It's really a tool that I'm making for myself. It's possible I may eventually make it publicly available, but for now it's really just for me. – Chris Lambrecht Jan 16 '15 at 05:39
  • 1
    A little more info would be helpful. What does your existing JavaScript look like? – Mike Loffland Jan 16 '15 at 06:02
  • 1
    The "server" could be your computer. Having something server-side does not mean it's publicly available. The thing is, though, if you're looking at storing the data in a file then you're not going to be able to do that directly from the browser in JavaScript. If you want to store it in the browser (as others have suggested), it's limited to that browser and whatever size restrictions the browser has for you. Understanding what you're trying to do would help. – Paul Jan 16 '15 at 17:21
  • @MikeLoffland I'm adding (part of) my javascript code to my original question. – Chris Lambrecht Jan 16 '15 at 18:26
  • @Paul One of the ways I earn income is by selling items for a client online. So, I'm designing a web app that stores information about this process (the days I list items, if/when the sell, what they sell for, when they sell, etc.). Ideally, after I figure out a way to store all this information, I'd like to generate reports based on the data (ie: commission due, gross revenue for a period, etc.) I need to be able to store this information for an indefinite period of time. I can't afford to lose it. For this reason, I'm wary of localStorage. I'm not opposed to learning another language... – Chris Lambrecht Jan 17 '15 at 18:52
  • @Paul ...like PHP or something, if it provides me a means to store the data "permanently." For now, I'm only worried about being able to access the data from one machine. That being said, it would be nice if the data was stored in file that I could retrieve, back up, etc. I know nothing about PHP (or any other web-based language besides HTML, JavaScript, and CSS), so I'm really not sure if that would be something I should look in to or what my options even look like. – Chris Lambrecht Jan 17 '15 at 18:55
  • 1
    Yes, grab a book on a server-side language, that's exactly what you need. I don't personally like PHP, but there's plenty of options there. If you're comfortable in JS, then Dart or Node might be an option for you, though there's learning curve no matter how you slice it. – Paul Jan 18 '15 at 17:59

3 Answers3

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.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
1

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.

Vitor Canova
  • 3,918
  • 5
  • 31
  • 55
user1496463
  • 410
  • 3
  • 14
  • If I use localStorage, could I use it to store objects, or arrays of objects? What are the limitations to localStorage? As you can kind of tell from the code, I need to be able to save each individual entry, which I can go back later and make changes to (ie: mark it as sold, lost, whatever). – Chris Lambrecht Jan 16 '15 at 18:33
0

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.

unrared
  • 93
  • 1
  • 4