1

I'm having a problem in my app where i will store the data in the localStorage everytime the client enter a case. here's my html using modal.

           <div class="modal-body">
                <div class="form-group">
                    <input class="form-control date-picker" placeholder="Month and Year" id="c_round" name="c_round">
                </div>
                <div class="form-group">
                    <select class="form-control" id="prov" name="prov">
                        <option>Select Province</option>
                        <option value="01">Province1</option>
                        <option value="02">Province2</option>
                        <option value="03">Province3</option>
                    </select>
                </div>
                <div class="form-group">
                    <select class="form-control" id="mun" name="mun">
                        <option>Select Municipality</option>
                        <option value="01">Municipality1</option>
                        <option value="02">Municipality2</option>
                        <option value="03">Municipality3</option>
                    </select>
                </div>
                <div class="form-group">
                    <select class="form-control" id="brgy" name="brgy">
                        <option>Select Barangay</option>
                        <option value="001">Barangay1</option>
                        <option value=:002>Barangay2</option>
                        <option value="003">Barangay3</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="commodity" class="control-label text-left">Household Classification</label>
                    <div class="form-horizontal">
                        <input type="radio" name="h_classification" value="Farming"> Farming &emsp;&emsp;
                        <input type="radio" name="h_classification" value="Non-Farming"> Non-Farming
                    </div>
                </div>
            </div><!--End modal body-->
            <div class="modal-footer">
                <button type="submit" id="create" class="btn btn-primary pull-right" data-dismiss="modal" >Create</button>
            </div>

When the client click the Create button the data will store in the localStorage here's my jquery.

$(function() {

var round = $("#c_round");
var prov  = $("#prov");
var mun   = $("#mun");
var brgy  = $("#brgy");
var h_classification = $("[name='h_classification']");
var btn_create = $('#create');
var dataObject = [];

btn_create.on('click', function(){ 
    if(typeof(Storage) !== "undefined"){
        dataObj = [{
            'user_id': user_id,
            'prov'   : prov.val(),
            'mun'    : mun.val(),
            'brgy'   : brgy.val(),
            'r_code' : r_code,
            'h_class': h_classification.val()
        }];

        dataObject.push(dataObj);

        localStorage.setItem('dataObject', JSON.stringify(dataObject));

        retrivedOjects = localStorage.getItem('dataObject');

        console.log('retrivedOjects: ', JSON.parse(retrivedOjects));
    }else {
        alert("Error: Localstorage not supported");
    }
});

});

When i refresh the page the old data is still there but when i create new data the old data is replaced by new data it doesn't append.

I want to stack many arrays in the localStorage("dataObjects") first before sending it on to the server for my offline mode application,

(sorry for my english)

2 Answers 2

1

Set dataObject to whatever is currently in localStorage before pushing new data to it.

var dataObject = [];
btn_create.on('click', function() { 
    if(typeof(Storage) !== "undefined") {
        if (localStorage.getItem('dataObject') && localStorage.getItem('dataObject').length > 0)
            dataObject = JSON.parse(localStorage.getItem('dataObject'));
        dataObj = {
            'user_id': user_id,
            'prov'   : prov.val(),
            'mun'    : mun.val(),
            'brgy'   : brgy.val(),
            'r_code' : r_code,
            'h_class': h_classification.val()
        };
        dataObject.push(dataObj);
        localStorage.setItem('dataObject', JSON.stringify(dataObject));

        retrivedOjects = localStorage.getItem('dataObject');

        console.log('retrivedOjects: ', JSON.parse(retrivedOjects));
    } else {
        alert("Error: Localstorage not supported");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! @Villa7_
why when the dataObject is empty or localStorage is empty it returns error of dataObject is null cannot push
oh i figure it out anyway thnks hehe
1

you need to get the old data from local storage and then push the new object to the old data. Then you can save it back to local storage.

var oldObject = localStorage.getItem('dataObject');
oldObject.push(dataObject);
localStorage.setItem('dataObject', JSON.stringify(dataObject));

Comments

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.