Search code examples
cordovajquery-mobilelawnchair

Need to restart app..Using Phonegap with lawnchair


I need to restart my app everytime I insert,delete or update my data. Data is inserting and deleting properly...but dont know why i need to restart my app everytime to get the effect.

I want to use the file structure for this app.

<script type="text/javascript" charset="utf-8" src="js/cordova-2.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="js/lawnchair.js"></script>
<script type="text/javascript" charset="utf-8" src="js/dom.js"></script>
<script>
    $(function(e) {

        var details = Lawnchair({name:'details',adapter:'dom'},function(e){
            //this.nuke();
        });

        details.all(function(data){
            for(var i = 0; i<data.length;i++)
            {
                //alert(data.length);
                var id = i + 1;
                //alert(id);
                document.getElementById("id").value = id;
                var listdiv = document.createElement('li');
                listdiv.setAttribute('id','listdiv' + i);
                listdiv.innerHTML = "ID : " + data[i].value.id + " | " + "Name : " + data[i].value.name + " | " + "Desc : " + data[i].value.desc;
                $('#show_list').append(listdiv);
            }
        });

        $('#save').click(function(e){
            //alert($.now());
            var obj1 = {id:$('#id').val(),name:$('#name').val(),desc:$('#desc').val()};
            //alert("listdiv" + obj1.id);
            details.save({key:"listdiv" + obj1.id,value:obj1});
            alert("Data Saved Successfully");
            //location.reload();
        });

        $('#del').click(function(e){
        if(document.getElementById("name").value != "")
        {
            var id = document.getElementById("id").value;
            details.remove("listdiv" + id,function(obj1){
                $("input:text").val("");
                document.getElementById("show_list").innerHTML = "";
                alert("Data Removed Successfully");
            });
        }
        else
        {
            alert("Please Select Data");
            return false;
        }
        });

        $('li').live('click',function(e){
            //alert(this.id);
            details.get(this.id,function(obj){
                document.getElementById("id").value = obj.value.id;
                document.getElementById("name").value = obj.value.name;
                document.getElementById("desc").value = obj.value.desc;
            });
        });
    });
    </script>

  </head>
  <body>

  </body>
</html>

Solution

  • Because new content has been appended code should look like this:

        details.all(function(data){
            for(var i = 0; i<data.length;i++)
            {
                //alert(data.length);
                var id = i + 1;
                //alert(id);
                document.getElementById("id").value = id;
                var listdiv = document.createElement('li');
                listdiv.setAttribute('id','listdiv' + i);
                listdiv.innerHTML = "ID : " + data[i].value.id + " | " + "Name : " + data[i].value.name + " | " + "Desc : " + data[i].value.desc;
                $('#show_list').append(listdiv);+
            }
            $('#show_list').listview().listview('refresh');
        });
    

    This line has been added (#show_list should be listviews id) :

    $('#show_list').listview().listview('refresh');
    

    If you want to find more about this (how to enhance dynamically added content in jquery Mobile) take a look at my other ARTICLE. Or it can be found HERE in my other answer. Read it carefully because there are numerous ways how his can be achieved.

    EDIT :

    And here's a fix for your problem: http://jsfiddle.net/Gajotres/3cuPP/

    When you click save button you also need to append new data. In your case I have used all function to get all data, that same data is then appended to listview and refreshed.