2017-12-15 1 views
0

이 코드는 원래 input.value를 가져 와서 페이지에 추가했습니다. 이 프로젝트에 로컬 스토리지를 추가했는데 코드가 이미 작성되었지만 localStorage에서 페이지에 대한 입력을 표시하는 데 어려움을 겪고 있습니다. 입력은 배열의 객체로 로컬 저장소에 저장됩니다. 나는 for 루프를 작성하여 그 값을 반복하고 요소를 빌드하고 li에 추가 한 다음 나중에 ul에 추가하는 함수에 전달합니다. 페이지에 표시되지 않고 콘솔에 오류가 표시되지 않습니다. 내가 그렇게 heres는 내 코드를 설정하는 경우 확실하지 않다 :함수 및 로컬 저장소 관련 문제

function fetchInvite() { 
    const rsvpInvite = JSON.parse(localStorage.getItem("Invitees")); 
    const rsvpList = document.getElementById('invitedList'); 

    for(var i = 0; i < rsvpInvite.length; i++) { 
     const name = rsvpInvite[i].name; 
     const confirm = rsvpInvite[i].confirmed; 
     createLI(name, confirm); 

    function createLI(name, confirm) { 
     const li = document.createElement('li'); 

    function createElement(elementName, property, value) { 
     const element = document.createElement(elementName); 

     element[property] = value; 
     return element; 
    } 
    function appendToLI (elementName, property, value) { 
     const element = createElement(elementName, property, value); 
     li.appendChild(element); 
     return element; 
    } 

appendToLI('span', 'textContent', name); 
appendToLI('label', 'textContent', confirm) 
.appendChild(createElement('input', 'type', 'checkbox')); 
appendToLI('button', 'textContent', 'edit'); 
appendToLI('button', 'textContent', 'remove'); 
return li; 

}

} 을}

전체 프로젝트는 여기에 있습니다 : https://github.com/tianniNakiMyers/RSVP_TeamTreeHouse/blob/master/app.js

+0

당신이 게시 된 코드를 확인하시기 바랍니다 수 : 그것과는 별도로

, 여기에 코드의 리팩토링입니다! 링크의 코드와 일치하지 않으며 일부 실수 (아마 오타)가있는 것으로 보입니다. 루프 내에서 함수가 실제로 정의되어 있습니까? –

+0

아, 그 코드가 같아요, 방금 루프를위한 함수를 삽입했습니다. BC를 알아 내려고 노력하고있었습니다. 코드에 오타가 있습니까? 또는 ... Idk는 for 루프의 값을 함수에 전달하는 더 좋은 방법입니다. 나는 그것들을 함수 표현으로 재 작성하는 것에 대해 생각했다. 그러나 그것을 과소 평가하고 더 많은 작업을 필요로했다. 나는 함수형 프로그래밍에 아직 아주 익숙하다. – UntouchedDruid4

답변

0

코드의 문제는 당신은 아마도 fetchInvite을 호출하지 않았을 것입니다.

function elt(parent, tag, text) {          // create element of tagname tag and append it to a parent (if provided) then set its textContent to text and return it 
    var el = document.createElement(tag); 
    if(parent) { 
     parent.appendChild(el); 
    } 
    el.textContent = text; 
    return el; 
} 

function fetchInvite() { 
    const rsvpInvite = JSON.parse(localStorage.getItem("Invitees")); 
    const rsvpList = document.getElementById('invitedList'); 

    for(var i = 0; i < rsvpInvite.length; i++) { 
     const name = rsvpInvite[i].name; 
     const confirm = rsvpInvite[i].confirmed; 

     const li = elt(rsvpList, 'li', '');        // create an li element with empty text and append it to rsvpList 
     elt(li, 'span', name);           // create a span whose text is name and append it to li 
     elt(elt(li, 'label', confirm), 'input', '').type = 'checkbox'; // create an input append it to a label element that contain the text confirm and that is appended to li, then set type of input to 'checkbox' 
     elt(li, 'button', 'edit');          // ... 
     elt(li, 'button', 'remove'); 
    } 
} 

fetchInvite(); // don't forget to call it