doujiang1993 2015-01-02 15:05
浏览 54
已采纳

如何将外部数据库中的记录填充到phonegap应用程序中的未排序列表

I am making a college admission application where students can register themselves and after registering the application will immediately open the next page which contains a list of colleges near them. I am developing this application in phonegap. I am fetching the records in JSON format. I have to populate this data in an unsorted dynamic list.

Structure of Student Registration:

The page id is "Registration". When I click the Register button, the Records(Student Info) is inserted and I immediately call page id ListOfColleges in which I have to show the list of Colleges.

<div data-role="page" id="Registration" data-theme="d" style="width: 100%;">        
    <div data-role="main" style="width: 100%;">

        <div  style="width: 76%; margin-left: 10%; margin-top: 3%;" id="studentRegister">
            <input type="text" placeholder="Name" name="userName">
            <input type="text" placeholder="Email" name="eMail">
            <input type="number" placeholder="Mobile" name="mobNumber">
            <input type="number" placeholder="Course" name="Course">
            <a href="#ListOfColleges" class="ui-btn"  id="btnReg" style="background-color: #B6B6BC; color: black;">Register</a>
        </div>
    </div>        
</div>  

Structure of List of Colleges Page

 <div data-role="page" id="ListOfColleges">     
            <div style="width: 100%; margin-top:21%; margin-left: 1%; color: black;">
                <ul data-role="listview">
                    <li>

                    </li>
                </ul>
            </div>
    </div> 

Format of JSON Data and php Code

<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","1234");
mysql_select_db("demo");
if(isset($_GET['type']))
{       
    if($_GET['type']=="login"){
        $query="Select * from collages";
        $result=mysql_query($query);
        $totalRows=mysql_num_rows($result); 
        if($totalRows>0){
            $recipes=array();
            while($recipe=mysql_fetch_array($result, MYSQL_ASSOC)){
                $recipes[]=array('Collage'=>$recipe);
            }
            $output=json_encode(($recipes));
            echo $output;
        }
     }
  }
else{
    echo "Invalid format";
}

I am trying to write the following JavaScript to populate the "ListOfColleges" page. "btnReg" is the id of the button on the page Registration.

Please help me to write this script and suggestions for modification of structure are welcome..

<script> 
            $(document).ready(function(){ 

               $("#btnReg").click(function(){ 
                  $.ajax({ 
                      url:"http://192.168.1.4/Experiements/webservices/api.php", 
                      type:"GET", 
                      dataType:"json", 
                      data:{type:"login", UserName:userId,Password:userPassword}, 
                      ContentType:"application/json", 
                      success: function(response){    
                           //
                              // *Logic for populate the dynamic list*
                           //   

                      }, 
                      error: function(err){ 
                          alert(JSON.stringify(err)); 
                      } 
                  }) 
               });  
            }); 
        </script>
  • 写回答

1条回答 默认 最新

  • dongtu4028 2015-01-02 18:59
    关注

    I should look for tutorials but here is a guide...

    in the HTML, add an ID to the ListView:

    <ul id="listCollege" data-role="listview">
    

    Then in the Javascript button click function do the following Loop:

    $("#listCollege").empty;
    for (i=0; i< iElements; i++)
    {
      $("#listCollege").append("<li><a href=''>College 1</a></li>");
    }
    

    Obviously you have to create the "li" element with the json data... but that's the text structure.

    Some JQMobile versions or in some cases you may need to add AFTER THE "FOR LOOP" the following:

    $("#listCollege").listview("refresh")
    

    but sometimes you don't have to, and if you write it YOUR CODE WILL FAIL, so take into account to try WITH or WITHOUT the listview("refresh") instruction.

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog