0

So this is a bit complex

I have created an app using phonegap. I am trying to implement a "to-do" list. I have found a great example javascript method, which I have put into the main index.html document.

So when the app first launches, it works fine and looks the way it's supposed to:

enter image description here

it lets me add new to dos, check a bunch to remove, or hit a button to delete them, etc.

but when I close the app and re-load it, it loses all the styling and the delete buttons no longer functions:

enter image description here

but, it I click ADD ITEM, the new items show in the style they are supposed to...

enter image description here

So, here is the code in the index.html head for the scripts:

<script type="text/javascript" language="JavaScript">
    
    //Create a new To-Do
    function createNewToDo()
    {
        var todoDictionary = {};
        //Prompt the user to enter To-Do
        var todo="ASSIGNMENT NAME";
        var todolink="index.html#fastshutter";
        if (todo!=null)
        {
            if (todo == "")
            {
                alert("To-Do can't be empty!");
            }
            else
            {
                //Append the new To-Do with the table
                todoDictionary = { check : 0 , text : todo , text2 : todolink};
                addTableRow(todoDictionary,false);
            }
        }
        
    }
    
    //Add a row to the table
    var rowID = 0;
    function addTableRow(todoDictionary, appIsLoading)
    {
        
        rowID +=1;
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        
        //Set up the CheckBox
        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        element1.name="chkbox[]";
        element1.checked = todoDictionary["check"];
        element1.setAttribute("onclick","checkboxClicked()");
        element1.className = "checkbox";
        cell1.appendChild(element1);
        
        //Set up the TextBox
        var cell2 = row.insertCell(1);
        var 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()");
        element2.className = "textbox";
        cell2.appendChild(element2);
        
        //Set up the View Button
        var cell3 = row.insertCell(2);
        var element3 = document.createElement("input");
        element3.type = "button";
        element3.id = rowID;
        element2.value = todoDictionary["text"];

        element3.setAttribute("onclick","window.open('index.html#fastshutter','_self')");
        element3.className = "viewButton";
        cell3.appendChild(element3);
        
        //Set up the Delete Button
        var cell4 = row.insertCell(3);
        var element4 = document.createElement("input");
        element4.type = "button";
        
        element4.setAttribute("onclick","deleteSelectedRow(this)");
        element4.className = "deleteButton";
        cell4.appendChild(element4);
        
        //Save & Update UI
        checkboxClicked();
        saveToDoList();
        
        if (!appIsLoading)
        alert("Task Added Successfully.");
    }
    
    
    //Add storke to completed tasks text
    function checkboxClicked()
    {
        
        //Get current table
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        
        //Loop through all rows
        for(var i=0; i<rowCount; i++)
        {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            var textbox = row.cells[1].childNodes[0];
            
            //checkbox is checked
            if(null != chkbox && true == chkbox.checked)
            {
                if(null != textbox)
                {
                    textbox.style.setProperty("text-decoration", "line-through");
                }
            }
            
            //checkbox is not checked
            else
            {
                textbox.style.setProperty("text-decoration", "none");
            }
            
        }
        //Save
        saveToDoList();
    }
    
    
    
    //Views textField's content of the selected row
    function viewSelectedRow(todoTextField)
    {
        alert(todoTextField.value);
    }
    
    
    //Deletes current row
    function deleteSelectedRow(deleteButton)
    {
        var p=deleteButton.parentNode.parentNode;
        p.parentNode.removeChild(p);
        saveToDoList();
    }
    
    
    function removeCompletedTasks()
    {
        //Get current table
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        
        //Loop through all rows
        for(var i=0; i<rowCount; i++)
        {
            //Delete row if checkbox is checked
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked)
            {
                table.deleteRow(i);
                rowCount--;
                i--;
            }
            
            
        }
        
        //Save
        saveToDoList();
        alert("Completed Tasks Were Removed Successfully.");
        
    }
    
    
    function saveToDoList()
    {
        //Create a todoArray
        var todoArray = {};
        var checkBoxState = 0;
        var textValue = "";
        
        //Get current table
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        
        if (rowCount != 0)
        {
            //Loop through all rows
            for(var i=0; i<rowCount; i++)
            {
                var row = table.rows[i];
                
                //Add checkbox state
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked)
                {
                    checkBoxState = 1;
                }
                else
                {
                    checkBoxState= 0;
                }
                
                
                //Add text data
                var textbox = row.cells[1].childNodes[0];
                textValue = textbox.value;
                
                //Fill the array with check & text data
                todoArray["row"+i] =
                {
                    check : checkBoxState,
                    text : textValue
                };
                
            }
        }
        else
        {
            todoArray = null;
        }
        
        //Use local storage to persist data
        //We use JSON to preserve objects
        
        window.localStorage.setItem("todoList", JSON.stringify(todoArray));
    }
    
    
    
    function loadToDoList()
    {
        
        //Get the saved To-Do list array by JSON parsing localStorage
        var theList = JSON.parse(window.localStorage.getItem("todoList"));
        
        
        if (null == theList || theList=="null")
        {
            deleteAllRows();
        }
        else
        {
            var count = 0;
            for (var obj in theList)
            {
                count++;
            }
            
            //Clear table
            deleteAllRows();
            //Loop through all rows
            for(var i=0; i<count; i++)
            {
                //Add row
                addTableRow(theList["row"+i],true);
            }
            
        }
        
    }
    
    
    
    function deleteAllRows()
    {
        //Get current table
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        
        //Loop through all rows
        for(var i=0; i<rowCount; i++)
        {
            //delete row
            table.deleteRow(i);
            rowCount--;
            i--;
        }
        
        //Save
        saveToDoList();
    }
    
    
    
    
    </script>

and the style (also in the head)

<style type="text/css">

/* BeginOAWidget_Instance_2127022: #gallery */

.viewButton {
    background-color:Transparent;
    width:50px
    height:32px;
}

.deleteButton { 
    background-color:Transparent;
    width:30px;
    height:30px;
}

Here is the html code to implement the page:

<body bgcolor="#e0e0e0" onload="loadToDoList()" >

<div data-role="page" id="favlist">
<div data-role="header">
<h1><a href="#choose" data-role="button" data-icon="arrow-l" data-iconpos="right">BACK</a> Favourites </h1>
</div>
<div data-role="content">
  <button type="button" class="addToDo" onclick="createNewToDo()">ADD</button>
  <button type="button" class="removeTasks" onclick="removeCompletedTasks()">REMOVE</button>
  <br/><br/><br/>
  <table id="dataTable" width="100%" border="0">
  </table> 
 <a href="#choose" data-role="button" data-theme="a">CLOSE</a> 
</div>
</div>

So I'm not sure if the jquerymobile css file is interfering with this maybe?

Thanks for any advice

EDIT: I was able to figure it out. I needed to override the jquery mobile CSS completely for the button elements by adding a new button class to the jquery css. On first run and adding new items it used the inline styles I had put in the HTML, but on reload it was pulling the standard styles

Community
  • 1
  • 1
Noel Chenier
  • 97
  • 1
  • 1
  • 13
  • 2
    I can't quite tell what you're talking about. Perhaps you could make the images a bit larger. – Blue Skies Dec 06 '13 at 00:23
  • the images are what would show on the iphone screen... i can't make them any larger. THe ADD/REMOVE buttons are on the top, and the two ASSIGNMENT NAMES rows are what get added when you hit the ADD button. THe other buttons to the right of the name aren't labelled, but they are the view and delete buttons. – Noel Chenier Dec 06 '13 at 00:26
  • Sorry, that was sarcasm. They're quite large enough. Quite. – Blue Skies Dec 06 '13 at 00:27
  • 1
    @user1114118 dude, blue skies was sarcastic... – Szymon Dec 06 '13 at 00:27
  • Ah, nice edit @espascarello. Last thing I need is another big To Do list staring me in the face. :-D – Blue Skies Dec 06 '13 at 00:28
  • I almost made them smaller, I picked the medium size... ;) – epascarello Dec 06 '13 at 00:29
  • This is bad practice: `element4.setAttribute("onclick"` – epascarello Dec 06 '13 at 00:29
  • my apologies for not formatting it properly. didnt realize they were showing up that large. Only trying to add as much possible info here so it's clear what I'm trying to do and what is happening. – Noel Chenier Dec 06 '13 at 00:29
  • Don't worry. Just having a little fun at your expense. ;-) – Blue Skies Dec 06 '13 at 00:30
  • @epascarello what would be a better practice? This code works just fine in the example project...but when I add the code to my project it doesnt. – Noel Chenier Dec 06 '13 at 00:31
  • BlueSkies true true :) @user1114118 do you have some life code ? – Szymon Dec 06 '13 at 00:31
  • life code? not sure what you mean – Noel Chenier Dec 06 '13 at 00:33
  • I mean working example of this code, to working with it. Maybe recreate it on jsfiddle or in codepen if you dont have it on your external server. – Szymon Dec 06 '13 at 00:35
  • the problem is, I dont know if it will work there because of the local storage...This problem only happens after re-opening the app after closing it. will either of those be able to record that? I can't even test it in a browser really because it doesn't seem clear the local storage. – Noel Chenier Dec 06 '13 at 00:39
  • I just find it strange that it works fine on first launch, then goes all screwy when the app is relaunched...but any new items added take on the proper style...but again, if the app is relaunched, also go all screwy... – Noel Chenier Dec 06 '13 at 00:40
  • ok, so i coudnt get it to work on jsfiddle. but if I load it in firefox and use the ispector, it shows the previously added elements are taking on the styles from the jquerymobile css file. but the new ones are not. So, how can i force it to NOT use the jquerycss rules? – Noel Chenier Dec 06 '13 at 00:51
  • I was able to figure it out. I needed to override the jquery mobile CSS completely for the button elements by adding a new button class to the jquery css. On first run and adding new items it used the inline styles I had put in the HTML, but on reload it was pulling the standard styles. – Noel Chenier Dec 06 '13 at 20:17

1 Answers1

0

I was able to figure it out. I needed to override the jquery mobile CSS completely for the button elements by adding a new button class to the jquery css. On first run and adding new items it used the inline styles I had put in the HTML, but on reload it was pulling the standard styles

Noel Chenier
  • 97
  • 1
  • 1
  • 13