2013-11-03 4 views
2

저는 간단한 t-do 앱을 만들어서 phonegap을 배우고 있습니다.phonegap : 함수가 호출되지 않습니다.

index.html을 :

<!DOCTYPE html> 
<html> 
    <head> 
     <title>To-dos</title> 
    </head> 
    <body onload='loadToDoList()'> 
     <input type='button' value='Add To-do' onclick='createNewToDo()'/> 
     <input type='button' value='Remove completed To-dos' onclick='removeCompletedTasks()'/> 
     <br/><br/> 
     <table id='dataTable' width='100%' border='1'> 
     </table> 
     <script src='js/index.js'></script> 
    </body> 
</html> 

하는 index.js : 나는 새, 수행 할 작업을 만들 수있어 이후

(function() { 
    var addTableRow, checkboxClicked, deleteAllRows, deleteSelectedRow, loadToDoList, saveToDoList, viewSelectedRow; 

    window.rowID = 0; 

    saveToDoList = function() { 
    var checkBoxState, chkbox, i, row, rows, table, textValue, textbox, todoArray, _i, _len; 
    todoArray = {}; 
    checkBoxState = 0; 
    textValue = ''; 
    table = document.getElementById('dataTable'); 
    rows = table.rows; 
    if (rows.length > 0) { 
     i = 0; 
     for (_i = 0, _len = rows.length; _i < _len; _i++) { 
     row = rows[_i]; 
     chkbox = rows.cells[0].childNodes[0]; 
     if ((chkbox != null) && chkbox.checked === true) { 
      checkBoxState = 1; 
     } else { 
      checkBoxState = 0; 
     } 
     textbox = row.cells[1].childNodes[0]; 
     textValue = textbox.value; 
     todoArray["row" + (i++)] = { 
      check: checkBoxState, 
      text: textValue 
     }; 
     } 
    } else { 
     todoArray = null; 
    } 
    return window.localStorage.setItem('todoList', JSON.stringify(todoArray)); 
    }; 

    loadToDoList = function() { 
    var i, key, theList, val, _results; 
    theList = JSON.parse(window.localStorage.getItem('todoList')); 
    deleteAllRows(); 
    if (theList !== null && theList !== 'null') { 
     i = 0; 
     _results = []; 
     for (key in theList) { 
     val = theList[key]; 
     _results.push(addTableRow(theList["row" + (i++)], true)); 
     } 
     return _results; 
    } 
    }; 

    deleteAllRows = function() { 
    var i, rowCount, table, _i; 
    table = document.getElementById('dataTable'); 
    rowCount = table.rows.count; 
    for (i = _i = 0; 0 <= rowCount ? _i < rowCount : _i > rowCount; i = 0 <= rowCount ? ++_i : --_i) { 
     table.deleteRow(i); 
     rowCount--; 
     i--; 
    } 
    return saveToDoList(); 
    }; 

    viewSelectedRow = function(todoTextField) { 
    return alert(todoTextField.value); 
    }; 

    deleteSelectedRow = function(deleteButton) { 
    deleteButton.parentNode.parentNode.parentNode.removeChild(); 
    return saveToDoList(); 
    }; 

    checkboxClicked = function() { 
    var chkbox, row, rows, table, textbox, _i, _len, _results; 
    table = document.getElementById('dataTable'); 
    rows = table.rows; 
    _results = []; 
    for (_i = 0, _len = rows.length; _i < _len; _i++) { 
     row = rows[_i]; 
     chkbox = row.cells[0].childNodes[0]; 
     textbox = row.cells[1].childNodes[0]; 
     if ((chkbox != null) && chkbox.checked === true && (textbox != null)) { 
     textbox.style.setProperty('text-decoration', 'line-through'); 
     } else { 
     textbox.style.setProperty('text-decoration', 'none'); 
     } 
     _results.push(saveToDoList()); 
    } 
    return _results; 
    }; 

    addTableRow = function(todoDictionary, appIsLoading) { 
    var cell1, cell2, element1, element2, row, rowCount, table; 
    rowID++; 
    table = document.getElementById('dataTable'); 
    rowCount = table.rows.length; 
    row = table.insertRow(rowCount); 
    cell1 = row.insertCell(0); 
    element1 = document.createElement('input'); 
    element1.type = 'checkbox'; 
    element1.name = 'chkbox[]'; 
    element1.checked = todoDictionary['check']; 
    element1.setAttribute('onclick', 'checkboxClicked()'); 
    cell1.appendChild(element1); 
    cell2 = row.insertCell(1); 
    element2 = document.createElement('input'); 
    element2.type = 'text'; 
    element2.name = 'txtbox[]'; 
    element2.size = 16; 
    element2.id = 'text' + rowID; 
    element2.value = todoDictionary['text']; 
    element2.setAttribute('onchange', 'saveToDoList()'); 
    cell2.appendChild(element2); 
    checkboxClicked(); 
    saveToDoList(); 
    if (!appIsLoading) { 
     return alert('Task added successfully'); 
    } 
    }; 

    window.createNewToDo = function() { 
    var todo, todoDictionary; 
    todoDictionary = {}; 
    todo = prompt('To-Do', ''); 
    if (todo != null) { 
     if (todo === '') { 
     return alert('To-Do can\'t be empty'); 
     } else { 
     todoDictionary = { 
      check: 0, 
      text: todo 
     }; 
     return addTableRow(todoDictionary, false); 
     } 
    } 
    }; 

    window.removeCompletedTasks = function() { 
    var chkbox, i, rowCount, table, _i; 
    table = document.getElementById('dataTable'); 
    rowCount = table.rows.length; 
    for (i = _i = 0; 0 <= rows ? _i < rows : _i > rows; i = 0 <= rows ? ++_i : --_i) { 
     chkbox = table.rows[i].cells[0].childNodes[0]; 
     if ((chkbox != null) && chkbox.checked === true) { 
     table.deleteRow(i); 
     i--; 
     rowCount--; 
     } 
    } 
    saveToDoList(); 
    return alert('Completed tasks were removed'); 
    }; 

}).call(this); 

createNewToDo 기능이 잘 작동합니다. 그러나 할일을 확인하고 제거 버튼을 클릭하면 아무 일도 일어나지 않습니다. 내 removeCompletedTasks 함수가 호출되지 않는 이유는 무엇입니까?

+0

행동으로 코드를보기 위해 피들을 제공하면 멋질 것입니다. –

답변

0

여기에 제공된 코드는 JSFiddle입니다. removeCompletedTasks가 실패한 주된 이유는 행이 정의되지 않았기 때문입니다. 이 오류가 수정 된 후에는 외부 함수의 범위 내에 만 정의한 메서드로 인해 다른 오류가 발생합니다. 완료되면 사라지고 호출 할 수 없습니다.

각 메소드에는 윈도우가 있어야합니다. 그들 앞에. 그러면 saveToDoList는 약간의 작업이 필요합니다. 테이블 메서드가 작동하지 않습니다, 그냥 행을 직접 이동하십시오. 일단 행이 있으면 원하는 참조를 사용하십시오.

window.saveToDoList = function() { 
    var checkBoxState, chkbox, i, row, rows, textValue, textbox, todoArray, _i, _len; 
    todoArray = {}; 
    checkBoxState = 0; 
    textValue = ''; 
    rows = document.getElementById('dataTable').rows; 
    if (rows.length > 0) { 
    i = 0; 
    for (_i = 0, _len = rows.length; _i < _len; _i++) { 
     row = rows[_i]; 
     chkbox = row.childNodes[0]; 
     if ((chkbox != null) && chkbox.checked === true) { 
     checkBoxState = 1; 
     } else { 
     checkBoxState = 0; 
     } 
     textbox = row.childNodes[1]; 
     textValue = textbox.value; 
     todoArray["row" + (i++)] = { 
     check: checkBoxState, 
     text: textValue 
     }; 
    } 
    } else { 
    todoArray = null; 
    } 
    return window.localStorage.setItem('todoList', JSON.stringify(todoArray)); 
}; 

그 방법을 사용하면 이제 removeCompletedTasks에 미세 조정이 필요합니다. 루프는 단순한 for 루프로 단순화 될 수 있습니다. 행을 제거 할 때마다 매번 신선한 것으로 처리됩니다. 느린 것은 알지만 적어도 크롬은 테이블의 함수를 반환합니다. 체크 박스를 얻으려면 한 단계 더 많은 어린이들이 필링이 필요했고 지금은 작동합니다.

window.removeCompletedTasks = function() { 
    var chkbox, i, rowCount, rows; 
    rows = document.getElementById('dataTable').rows; 
    rowCount = rows.length; 
    for (i = 0; i < rowCount; ++i) { 
     chkbox = rows[i].childNodes[0].childNodes[0]; 
     if ((chkbox != null) && chkbox.checked === true) { 
     document.getElementById("dataTable").deleteRow(i); 
     --i; 
     --rowCount; 
     } 
    } 
    saveToDoList(); 
    return alert('Completed tasks were removed'); 
    }; 

새로운 예제를 보려면 JSFiddle을 확인하십시오.

관련 문제