2017-05-22 1 views
0

array内のオブジェクトを削除する必要があり、ローカルストレージから削除する必要があります。私はspliceメソッドを使用して削除しようとしていますが、実際にどのように使用するのかは分かりません。 Folloeingは私が「エントリを」と呼ばれるあなたがのlocalStorageに新しい項目を設定しているあなたのdeleteEntry機能でtried-localstorageを使って配列内のオブジェクトを削除するには?

var details = []; 



function addEntry() { 


    var existingEntries = JSON.parse(localStorage.getItem("allEntries")); 
    if (existingEntries == null) existingEntries = []; 


    var srno = document.getElementById("txtpid").value; 
    var name = document.getElementById("txtpname").value; 
    var dob = document.getElementById("txtpdob").value; 
    var email = document.getElementById("txtpemail").value; 
    var address = document.getElementById("txtpaddr").value; 
    var contact = document.getElementById("txtpmobile").value; 

    var obbbj = { 
    txtpid: srno, 
    txtpname: name, 
    txtpdob: dob, 
    txtpemail: email, 
    txtpaddr: address, 
    txtpmobile: contact 
    }; 
    localStorage.setItem("details", JSON.stringify(obbbj)); 

    existingEntries.push(obbbj); 
    localStorage.setItem("allEntries", JSON.stringify(existingEntries)); 
    showEntry(); 
    console.log(existingEntries); 
    //location.reload(); 
} 


function showEntry() { 
    var messageBox = document.getElementById("display"); 
    messageBox.value = ""; 
    document.getElementById("txtpid").value = ""; 
    document.getElementById("txtpname").value = ""; 
    document.getElementById("txtpdob").value = ""; 
    document.getElementById("txtpemail").value = ""; 
    document.getElementById("txtpaddr").value = ""; 
    document.getElementById("txtpmobile").value = ""; 


    var render = "<table border='1'>"; 
    render += "<tr><th>Srno</th><th>Name</th><th>Birthdate</th><th>Email</th><th>Address</th><th>Contact</th></tr>"; 


    var allEntriesoo = {}; 
    var detailsOOO = {}; 
    for (i = 0; i < localStorage.length; i++) { 
    var key = localStorage.key(i); 
    var person = localStorage.getItem(key); 
    if (key == 'allEntries') 
     allEntriesoo = JSON.parse(person); 
    if (key == 'details') 
     detailsOOO = JSON.parse(person); 
    var data = JSON.parse(person); 

    } 

    for (var key in allEntriesoo) { 
    console.error(allEntriesoo[key]) 
    render += "<tr><td>" + allEntriesoo[key].txtpid + "</td><td>" + allEntriesoo[key].txtpname + " </td>"; 
    render += "<td>" + allEntriesoo[key].txtpdob + "</td>"; 
    render += "<td>" + allEntriesoo[key].txtpemail + "</td>"; 
    render += "<td>" + allEntriesoo[key].txtpaddr + "</td>"; 
    render += "<td>" + allEntriesoo[key].txtpmobile + "</td>"; 
    render += "<td><input type='button' value='Delete' onClick='return deleteEntry(" + i + ")'></td>"; 
    render += "<td><input type='button' value='Edit' onClick='return editInfo(" + i + ")'></td></tr>"; 

    } 
    render += "</table>"; 
    display.innerHTML = render; 
} 

function nameVal() { 
    document.getElementById("txtpname").focus(); 
    var n = document.getElementById("txtpname").value; 
    var r; 
    var letters = /^[a-zA-Z]+$/; 
    if (n == null || n == "") { 
    alert("please enter user name"); 
    return null; 
    n.focus(); 
    } else { 
    if (n.match(letters) && n != "") { 
     r = ValidateEmail(); 
     return r; 
    } else { 
     alert("please enter alphabates"); 
     document.getElementById("txtpname").value = ""; 
     document.getElementById("txtpname").focus(); 
     return null; 
    } 
    } 
} 

function ValidateEmail() { 
    var uemail = document.getElementById("txtpemail").value; 
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
    if (uemail.match(mailformat)) { 
    return true; 
    } else { 
    alert("You have entered an invalid email address!"); 
    document.getElementById("txtpemail").value = ""; 
    document.getElementById("txtpemail").focus(); 
    return null; 
    } 
} 

function alphanumeric() { 
    var uadd = document.getElementById("txtpaddr").value; 
    var letters = /^[0-9a-zA-Z]+$/; 
    if (uadd == null || uadd == "") { 
    alert("plz enter address"); 

    uadd.focus(); 
    } else { 
    if (uadd.match(letters)) { 
     return true; 
    } else { 
     alert('User address must have alphanumeric characters only'); 
     document.getElementById("txtpaddr").value = ""; 
     document.getElementById("txtpaddr").focus(); 
     return false; 
    } 
    } 
} 

function cntVal() { 
    var n = document.getElementById("txtpmobile").value; 
    var r1; 
    var letters = /^\d{10}$/; 
    if (n !== null || n !== "") { 
    if (n.match(letters)) { 
     r1 = alphanumeric(); 
     return r1; 
    } else { 
     alert("please enter contact number"); 
     document.getElementById("txtpmobile").value = ""; 
     document.getElementById("txtpmobile").focus(); 
     return null; 
    } 
    } else { 
    alert("please enter contact Number"); 
    return null; 
    n.focus(); 
    } 

} 

function deleteEntry(id) { 
    console.log("aaaaaaaaaaaaaaAAAA"); 

    var entry = localStorage.getItem('allEntries') JSON.parse(localStorage.getItem('allEntries')): []; 
    var index; 
    for (var i = 0; i < entry.length; i++) { 
    if (entry[i].id === id) { 
     index = i; 
     break; 
    } 
    } 
    if (index === undefined) return 
    entry.splice(index, 1); 
    localStorage.setItem('entry', JSON.stringify(entry)); 
} 

function clearstorage() { 
    localStorage.clear(); 
    window.location.reload(); 
} 

window.onload = function() { 
    showEntry(); 
}; 
+1

あなたはそれをより読みやすいですので、よりきれいにあなたのコードをフォーマットし、コメントアウトされたコードのような不必要な余分なビットを、削除する場合は役に立つ答えを取得する可能性が高くなります。それを[最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)に減らすことができれば、あなた自身が答えを見つけられるかもしれません。 – DaveyDaveDave

+0

エラーが発生しましたか?問題は何ですか? 'splice()'メソッドが実行される限り、あなたはそれを正しく使用しているようです。 –

+0

問題はエントリがローカルストレージから削除されていないことです。あなたが私にそれを理解させるのを助けてくれますか? –

答えて

0

をしている私のコードです。だからあなたは 'allEntries'を設定しておらず、おそらくそのように表示されている理由は、 'allEntries'は更新されていないのです。したがって、すべてのエントリを再度設定してください。 localStorage.setItem( 'allEntries'、JSON.stringify(entry));

また、 '?'あなたのための変数 'エントリ' ...

var entry = localStorage.getItem( 'allEntries')? JSON.parse(localStorage.getItem( 'allEntries')):[]; < - これは次のようになります。

その 'もしelse文' あなたは一時的なオブジェクトを作成して使用し、その後のlocalStorageでそれを置き換えることができます

function deleteEntry(id){ 
 
    console.log("aaaaaaaaaaaaaaAAAA"); 
 

 
     var entry = localStorage.getItem('allEntries') ? JSON.parse(localStorage.getItem('allEntries')) : []; 
 
     var index; 
 
     for (var i = 0; i < entry.length; i++) { 
 
      if (entry[i].id === id) { 
 
       index=i; 
 
       break; 
 
      } 
 
     } 
 
     if(index === undefined) return 
 
     entry.splice(index, 1); 
 
     localStorage.setItem('allEntries', JSON.stringify(entry)); <--- like this 
 

 
    }

+0

のフォーマットを助けますが、またそれも動作しません –

+0

私は私の答えに編集を追加しました。あなたは '?'あなたの変数宣言のエントリ – letterman549

+0

はい私は '?'それでもローカルストレージからオブジェクトを削除していません。 –

0

と同じ。

function deleteEntry(id){ 
    console.log("aaaaaaaaaaaaaaAAAA"); 

     var entry = JSON.parse(localStorage.getItem('allEntries')); 
     var temp= []; 
     for (var i = 0; i < entry.length; i++) { 
      if (entry[i].id !== id) { 
       temp.push(entry[i]); 
      } 
     } 
     localStorage.setItem('entry', JSON.stringify(temp)); 
    } 
関連する問題