i've written 3 little function that save, load or delete a single entry in an array saved with jstorage.
The two first functions works perfectly .
But the one that delete a single entry systematically delete the whole array or doesn't save it.
Could someone point me the problem, which is probably in my code ?
function local_single_delete(poi_id){
if ($.jStorage.storageAvailable()== true) { // if local storage is available
loctab = $.jStorage.get("poi_ids"); // load saved data
if (loctab == null) { // if no saved data => null array
loctab = new Array();
console.log('loctab is null');
}
for (local_i = 0; i < loctab.length; local_i++ ){ // for each saved data , looking for one one in particular
if (loctab[local_i]['id_poi'] === poi_id) { // if found
loctab[local_i]= loctab[loctab.length-1]; // the one becomes the last entry value,
loctab.length =loctab.length -1; // then we truncate the array for one position
break; // and we leave the for
}
}
$.jStorage.set("poi_ids",loctab); // write data
$.jStorage.flush(); // clear cache
}
else {
console.log('on a pas de stockage local');
}
}
You delete all data when you flush ... so just don't do it! You also had an non declared i
variable.
function local_single_delete(poi_id){
if ($.jStorage.storageAvailable() === true) {
loctab = $.jStorage.get("poi_ids");
if (loctab === null) {
loctab = [];
console.log('loctab is null');
}
for (local_i = 0; local_i < loctab.length; local_i++ ){
if (loctab[local_i].id_poi === poi_id) {
loctab[local_i]= loctab[loctab.length-1];
loctab.length =loctab.length - 1;
break;
}
}
$.jStorage.set("poi_ids",loctab);
}
else {
console.log('on a pas de stockage local');
}
}