Hii我正在做一个简单的手机间隙应用程序。我想使用ajax在phonegap的列表视图中显示我的数据。但我在listview.my列表视图中遇到一个问题,一次只显示一个值。我想要的在列表视图中显示所有标题。但它只显示一个标题..如何在listview中显示所有数据库列值。我可以循环it.pls帮助。
$.ajax({
url: 'http://url/display.php',
type: 'GET',
dataType: "json",
success: function(data){
$('#list').append('<li >'+data.title+'</li>');
$("#list").listview("refresh");
alert('Data successfully display');
},
error: function(){
alert('There was an error');
}
});
#html
<div data-role="content">
<div class="example-wrapper">
<ul data-role="listview" id="list" data-divider-theme="a" data-inset="true</ul>
</div>
</div>
<?php
include_once('config/config.php');
$sql="select * from myapp";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$array = $row;
}
echo json_encode($array);
?>
答案 0 :(得分:1)
您没有关闭<ul>
代码。像这样改变,
<div data-role="content">
<div class="example-wrapper">
<ul data-role="listview" id="list" data-divider-theme="a" data-inset="true">
</ul>
</div>
</div>
检查一下,
$.ajax({
url: 'http://url/display.php',
type: 'GET',
dataType: "json",
success: function(data){
var li_tag='';
$(data).each(function( index,value ) {
console.log( index + ": " + value );
li_tag=li_tag+'<li>'+value+'</li>'
});
$('#list').append(li_tag);
$("#list").listview("refresh");
alert('Data successfully display');
},
error: function(){
alert('There was an error');
}
});
答案 1 :(得分:1)
您目前使用PHP代码获取一行数据,我已经修改了您的代码,
<?php
include_once('config/config.php');
$sql="select * from myapp";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$array[] = $row;
}
echo json_encode($array);
?>
像这样的ajax代码,
$.ajax({
url: 'http://url/display.php',
type: 'GET',
dataType: "json",
success: function(data){
for(i=0;i<data.length;i++){
$('#list').append('<li >'+data[i].title+'</li>');
}
$("#list").listview("refresh");
alert('Data successfully display');
},
error: function(){
alert('There was an error');
}
});