I have stored data in sessionStorage and this is an example of how I save the data:
$.ajax({
type: 'POST',
url: 'Components/Functions.cfc?method='+cfMethod,
data: formData,
dataType: 'json'
}).done(function(obj){
sessionStorage.setItem('Books',JSON.stringify(obj.DATA));
}).fail(function(jqXHR, textStatus, errorThrown){
alert('Error: '+errorThrown);
});
After saving records in sessionStorage, they are structured like this:
{
"14": {
"COMMENTS": "",
"RECORDID": 14,
"NAME": "Decorah Sector",
"AGENCY": "01",
"CODE": "011A"
},
"15": {
"COMMENTS": "",
"RECORDID": 15,
"NAME": "Waukon Field",
"AGENCY": "01",
"CODE": "011B"
},
"16": {
"COMMENTS": "Test 524",
"RECORDID": 16,
"NAME": "New Hampton",
"AGENCY": "01",
"CODE": "011C"
}
}
Each record has a unique key. When a user wants to edit a record, I retrieve the data from sessionStorage using the matching unique key. Here's an example of my edit function:
function editBook() {
var recordID = $(this).closest('tr').attr('id'),
bookRecord = JSON.parse(sessionStorage.Books[recordID]);
console.log(bookRecord); //For testing purpose.
if(bookRecord){
$.each(regionRecord, function(name, value){
var elementName = $('[name="'+'frmSave_'+name.toLowerCase()+'"]'),
elementType = elementName.prop('type'),
elementVal = $.trim(value);
switch(elementType){
case 'select-one':
elementName.val(elementVal);
break;
case 'textarea':
elementName.val(elementVal);
break;
default:
elementName.val(elementVal);
}
});
}
}
When I console.log()
bookRecord, I'm not receiving the data for the specific id
that I passed in sessionStorage. I'm unsure if there's an issue with my syntax or something else causing the problem. If anyone can identify where the issue might be in my code, please advise.