Sqlite中的第二页JQM Listview不刷新

Sqlite中的第二页JQM Listview不刷新,sqlite,listview,jquery-mobile,jquery-mobile-listview,Sqlite,Listview,Jquery Mobile,Jquery Mobile Listview,我正在开发一个离线应用程序,目前将在我的笔记本电脑上运行。我从Sqlite中提取数据并在listview中显示数据。它在第一页可以正常工作,但是如果我想在第二页显示列表视图数据,它就不能工作。我错过了什么 用于在第一页上显示的工作代码: <!DOCTYPE html> <html> <head> <title>Soccer</title> <meta name="viewport" content="width=device-wi

我正在开发一个离线应用程序,目前将在我的笔记本电脑上运行。我从Sqlite中提取数据并在listview中显示数据。它在第一页可以正常工作,但是如果我想在第二页显示列表视图数据,它就不能工作。我错过了什么

用于在第一页上显示的工作代码:

<!DOCTYPE html>
<html>
<head>
<title>Soccer</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
    <script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>

<script type="text/javascript">
    var db = window.openDatabase("ShoufiMobi", "1.0", "Just a Dummy DB", 200000); //will create database Dummy_DB or open it

    //function will be called when an error occurred
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }

    //function will be called when process succeed
    function successCB() {
//        alert("success!");
        db.transaction(queryDB,errorCB);
    }

    //select all from SoccerPlayer
    function queryDB(tx){
        tx.executeSql('SELECT * FROM Mailbox',[],querySuccess,errorCB);
    }

    function querySuccess(tx,result){
        $('#SoccerPlayerList').empty();
        $.each(result.rows,function(index){
            var row = result.rows.item(index);
            $('#SoccerPlayerList').append('<li><a href="#pagetwo"><img src="public_80x80.png"><h3 class="ui-li-heading">'+row['boxname']+'</h3><p class="ui-li-desc">Email '+row['boxstatus']+'</p><span class="ui-li-count">25</span></a></li>');
        });

        $('#SoccerPlayerList').listview();
    }
    successCB();
</script>

</head>

<body>

<div data-role="page" id="pageone">
  <div data-role="header" data-position="fixed" data-theme="b">
    <h1>Soccer Player</h1>
  </div><!-- /header -->

  <div data-role="content">
    <ul id="SoccerPlayerList">
    </ul>
  </div><!-- /content -->

  <div data-role="footer">
    <h4>Footer</h4>
  </div><!-- /footer -->

</div><!-- /pageone -->

<div data-role="page" id="pagetwo">
  <div data-role="header" data-position="fixed" data-theme="b">
    <h1>Welcome To My Second Page</h1>
  </div>

  <div data-role="content">
<a href="#pageone"><h4> hello</h4></a>
  </div>

  <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

</body>
</html>
现在,如果我交换第一页和第二页的内容,我会收到警告:处理SQL:0时出错,并且我会丢失第二页上listview的格式。我错过了什么? 以下是交换页面内容的代码:

<body>

<div data-role="page" id="pageone">
  <div data-role="header" data-position="fixed" data-theme="b">
    <h1>Soccer Player</h1>
  </div><!-- /header -->

  <div data-role="content">
<a href="#pagetwo"><h4> hello</h4></a>
  </div><!-- /content -->

  <div data-role="footer">
    <h4>Footer</h4>
  </div><!-- /footer -->

</div><!-- /pageone -->

<div data-role="page" id="pagetwo">
  <div data-role="header" data-position="fixed" data-theme="b">
    <h1>Welcome To My Second Page</h1>
  </div>

  <div data-role="content">
    <ul id="SoccerPlayerList">
    </ul>

  </div>

  <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

</body>
非常感谢我让它像这样工作:

<!DOCTYPE html>
<html>
<head>
<title>Soccer</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
    <script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>

<script type="text/javascript">

var db = window.openDatabase("ShoufiMobi", "1.0", "Just a Dummy DB", 200000); //will create database Dummy_DB or open it

$(document).on("pageinit", "#pageone", function () {
    //function will be called when an error occurred
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }

    //function will be called when process succeed
    function successCB() {
//        alert("success!");
        db.transaction(queryDB,errorCB);
    }

    //select all from SoccerPlayer
    function queryDB(tx){
        tx.executeSql('SELECT * FROM Mailbox',[],querySuccess,errorCB);
    }

    function querySuccess(tx,result){
        $('#SoccerPlayerList').empty();
        $.each(result.rows,function(index){
            var row = result.rows.item(index);
            $('#SoccerPlayerList').append('<li><a href="#pagetwo"><img src="public_80x80.png"><h3 class="ui-li-heading">'+row['boxname']+'</h3><p class="ui-li-desc">Email '+row['boxstatus']+'</p><span class="ui-li-count">25</span></a></li>');
        });
    }

successCB();
}); //on pageinit #pageone

$(document).on("pagebeforeshow", "#pagetwo", function () {
    $('#SoccerPlayerList').listview();
});
</script>
</head>

<body>
<div data-role="page" id="pageone">
    <div data-role="header" data-position="fixed" data-theme="b">
         <h1>Soccer Player</h1>
    </div>
    <!-- /header -->
    <div data-role="content"> 
        <a href="#pagetwo"><h4> hello</h4></a>       
    </div>
    <!-- /content -->
    <div data-role="footer">
         <h4>Footer</h4>
    </div>
    <!-- /footer -->
</div>
<!-- /pageone -->
<div data-role="page" id="pagetwo">
    <div data-role="header" data-position="fixed" data-theme="b">
         <h1>Welcome To My Second Page</h1>
    </div>
    <div data-role="content">
<a href="#pageone"><h4> Back</h4></a>
<ul id="SoccerPlayerList"></ul>
    </div>
    <div data-role="footer">
         <h1>Footer Text</h1>
    </div>
</div>
</body>
</html>

从代码中很难看出何时调用数据库。但是,假设您希望在第1页上进行调用,以便在导航到第2页时调用已经存在,请刷新第2页的pagebeforeshow事件上的listview:

$(document).on("pageinit", "#pageone", function () {
    $('#SoccerPlayerList').empty();   
    $.each(result.rows, function (index, row) {        
        $('#SoccerPlayerList').append('<li><a href="#pagetwo"><img src="http://lorempixel.com/80/80/technics/' + index + '/"><h3 class="ui-li-heading">' + row['boxname'] + '</h3><p class="ui-li-desc">Email ' + row['boxstatus'] + '</p><span class="ui-li-count">25</span></a></li>');
    });
});

$(document).on("pagebeforeshow", "#pagetwo", function () {
    $('#SoccerPlayerList').listview();
});
工作