Checkboxes set (JQUERY UI) from nested JSON using LocalStorage - Update
Checkboxes set from nested JSON using LocalStorage - Update
HTML
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.19/themes/base/jquery-ui.css">
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.14/jquery-ui.min.js"></script>
<div id="calsubscribe"></div>
<button class="set">SAVE SETTINGS</button>
<br><code id="code1"><p></p></code>
<div id="emailsubscribe"></div>
<button class="set1">SAVE SETTINGS</button>
<br>
JavaScript
// SET LOCAL STORAGE BASED IN THIS IF NON EXISTS, OTHERISE USE LOCALSTORAGE UNTIL RESET ON LOGIN.
if (localStorage.getItem("fdsettings") === null) {
var JSONsettings = {
"feeds": [{
"name": "Full Feed",
"id": "13",
"checked": true
}, {
"name": "Trip Feed",
"id": "53",
"checked": false
}, {
"name": "Flight Feed",
"id": "27",
"checked": true
}]
};
localStorage.setItem("fdsettings", JSON.stringify(JSONsettings));
var myjson = JSON.parse(localStorage.getItem("fdsettings"));
} else {
var myjson = JSON.parse(localStorage.getItem("fdsettings"));
}
if (myjson === null){
} else{ //If there is no data, initialize an empty array
myjson = [];
}
/*
0) $.getJson FROM SERVER DB
1) SET INCOMING JSON TO LOCAL STORAGE
2) SET UP THE CHECK BOX LIST FROM THE JSON IN LOCAL STORAGE
3) GATHER NEW JSON SETTINGS ON PRESS AND SAVE TO LOCAL STORAGE
*/
// PROCESS FEEDS TO SCREEN
$.each(myjson.feeds, function (i, v) {
var feedname = this.name;
$("#calsubscribe").append($("<label>").text(this.name).prepend(
$("<input>").attr({
'type': 'checkbox',
'class': 'inp-checkbox',
'name': 'subscribecal',
"data-feed": feedname
}).val(this.id)
.prop('checked', this.checked)));
});
// EMAIL ALERTS SAVE
// ON CLICK FEEDS SAVE THE SETTINGS TO LS OR USE TO UPDATE DB
$(document).on("click", "button.set1", function (e) {
var EcheckValues = $('input[name=subscribe]').map(function () {
return {
name: $(this).data('feed'),
id: $(this).val(),
checked: this.checked
};
}).get();
// set to LocalStorage
$("code#code1 p").html(JSON.stringify(EcheckValues));
// Save the amended table to LocalStorage - overwrite
localStorage.setItem("fdsettings", JSON.stringify(EcheckValues));
}); //end...