如何将数据绑定到 Jquery Mobile 列表视图
Posted
技术标签:
【中文标题】如何将数据绑定到 Jquery Mobile 列表视图【英文标题】:How to bind data to Jquery Mobile listview 【发布时间】:2013-02-15 16:40:16 【问题描述】:下面是我的代码,数据没有绑定到列表视图。显示一个空白页面。
<div data-role="page" id="index">
<div data-role="header">
<h1>
demo</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" id="cars-data">
<li >abcd</li>
<li>cdf</li>
</ul>
</div>
</div>
<div data-role="page" id="cars">
<div data-role="header">
<a data-role="button" data-transition="none" data-theme="a" href="#index">Back</a>
<h1>
</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" id="car-data">
</ul>
<img src="" style="height: auto;" id="car-img">
</div>
</div>
<script type="text/javascript" charset="utf-8">
$(document).ready(function ()
$.ajax(
type: "POST",
async: true,
url: "PINCWebService.asmx/GetContacts",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data)
// $("#car-data").html(data);
var list = $("#car-data");
list.empty();
$.each(data, function (rowIndex)
var datar = data.rows.item(rowIndex);
list.append("<li>" + datar + "</li>");
);
list.listview('refresh');
);
);
</script>
【问题讨论】:
我已经用完整的代码***.com/questions/15151747/…回答了你的问题 【参考方案1】:我已经为你写了一些东西。请找出每件事是如何完成的。
服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace SimpleWebService
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
[WebMethod]
public string GetLankanList()
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Lankans> lankanList = new List<Lankans>();
string[] names = "chamara", "janaka", "asanka" ;
for (int i = 0; i < names.Length; i++)
Lankans srilankans = new Lankans();
srilankans.Name = names[i];
lankanList.Add(srilankans);
string jsonString = serializer.Serialize(lankanList);
return jsonString;
public class Lankans
public string Name get; set;
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
</head>
<body>
<div data-role="page" id="lankalistpage">
<div data-role="header">
<h1>Page Title</h1>
</div><!-- /header -->
<div data-role="content">
<div id="LankanLists"></div>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<script src="lankanscript.js"></script>
</body>
</html>
JavaScript
$('#lankalistpage').live('pageshow',function(event)
var serviceURL = 'service1.asmx/GetLankanList';
$.ajax(
type: "POST",
url: serviceURL,
data: param="",
contentType:"application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
);
function successFunc(data, status)
// parse it as object
var lankanListArray = JSON.parse(data.d);
// creating html string
var listString = '<ul data-role="listview" id="customerList">';
// running a loop
$.each(lankanListArray, function(index,value)
listString += '<li><a href="#" >'+this.Name+'</a></li>';
);
listString +='</ul>';
//appending to the div
$('#LankanLists').html(listString);
// refreshing the list to apply styles
$('#LankanLists ul').listview();
function errorFunc()
alert('error');
);
最终输出
你可以download the source here
【讨论】:
以上是关于如何将数据绑定到 Jquery Mobile 列表视图的主要内容,如果未能解决你的问题,请参考以下文章
如何阻止动态元素在 Jquery/Jquery Mobile 中重新绑定?
寻找一种将更多列表动态添加到 jQuery Mobile 列表视图底部的方法