2015-01-07 27 views
1

我正在获取JSON数据(在给定距离内可用的酒店列表)。我以JSON格式获得以下信息,酒店名称,从当前位置到酒店的距离,房间数量。在给定的距离内可能有不止一家酒店。我们必须打印所有这些信息ListView像下面一样。我想将酒店下图中的所有信息合并到一个列表视图中。如何将多个信息添加到JQuery Mobile列表视图

enter image description here

当我们点击酒店的任何列表,它会跳转到酒店网站或更多细节奉献酒店页面。

Javascript代码是。

<script> 
      $(document).ready(function(){ 
       $("#btnReg").click(function(){ 
        $("#listOfHotelOption").empty(); 
        var distance = document.getElementById('distance_id').value; 
        var output=""; 
        $.ajax({ 
          url:"http://192.168.1.4/Experiements/webservices/getBuddy.php", 
          type:"GET", 
          dataType:"json", 
          data:{type:"login", Distance:distance}, 
          ContentType:"application/json", 
          success: function(response){ 
          console.log(response) 
           $.each(response, function(index,value){ 

           output+="<li>"+value.Hotel.Name+" "+value.Hotel.Distance+" " +value.Hotel.Rooms+"</li>"; 
           }); 
          $("#listOfHotelOptions").append(output).listview().listview('refresh'); 
         }, 
          error: function(err){ 
          alert(JSON.stringify(err)); 
         } 
        }) //ajax 
       }); //click 
      }); //ready 
    </script> 

**My server site code is** 

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

要格式化的列表项目看起来像你有照片吗? – Tasos

+0

@Tasos,抱歉请忽略之前的评论,我正在尝试输出+ =“

  • ”+“\""+uu+"\"”+“”+ value.Hotel.Name +“have”+ value.Hotel.Rooms +“available”+“
  • ”;也每个列表显示点击能够和重定向到dedecated酒店页面.. – geeks

    回答

    1

    我准备了一个演示从列表项抓取数据,并接近你想要达到

    演示

    http://jsfiddle.net/0m3pxznf/

    jQuery的抢数据的格式从你使用的列表项目<li data-whatever="abcdcd" ...等,并抓住他们点击。

    $(document).on("click", "#hotels >li", function() { 
    var hotelname = $(this).closest("li").attr('data-hotel'); 
    var rooms = $(this).closest("li").attr('data-rooms'); 
    alert(hotelname + "--" + rooms) 
    }) 
    

    的Html

    <li data-hotel="taj" data-rooms="10"><a> 
        <img src="http://s30.postimg.org/9evb4kq65/album_bb.jpg"> 
         <h2>Hotel:<span class="hotel">Taj</span><span class="avail">Available Rooms:<span class="rooms">10</span></h2> 
         <p>Distance:<span class="distance">10km</span></p> <img src="http://s21.postimg.org/3qio5rxeb/album_p.jpg" class="sideimg"></a> 
    
    </li> 
    

    的CSS

    .sideimg{ 
    position: absolute; 
    right:0 !important; 
    top: 0 !important; 
    max-height: 5em !important; 
    max-width: 5em !important; 
    } 
    
    .ui-listview>li h2 { 
    margin-top: -5px !important; 
    } 
    .ui-listview>li p { 
    margin-top:10px !important; 
    } 
    
    .hotel, .rooms { padding-left: 5px; } 
    
    
    .avail { padding-left: 15px; } 
    

    结果

    enter image description here

    如果你想将用户重定向到酒店网站然后将列表中的项目data-url="the-url"并抓住它的点击功能和使用window.location.href = theurlvariable;

    +0

    非常感谢你.. – geeks

    +0

    @neelabhsingh没问题。请参阅最后一个注释以重定向到酒店网站 – Tasos

    +0

    K,这与我们昨天讨论的相同,现在我需要打印特定酒店的所有信息并放入单个列表视图,并且当我们单击列表时它将重定向到专用酒店页面或酒店网站 – geeks

    相关问题