如何使用 JSON 数据动态填充 jQuery 移动页面?
How do I dynamically populate jQuery Mobile pages using JSON data?
我正在尝试从某些外部 JSON (http://ip-api.com/json) 获取数据。然后我想使用键来动态填充列表视图。我希望每个列表项都是一个 link,当您单击 link 时,jQuery 移动页面将包含该键的匹配值。我有两个 jQuery 移动页面,一个主页,另一个。
我试图通过将第二个 "data-role=page" div 的 id 更改为当前键数据,并将键数据附加到 h2,并将值数据附加到p.它正在创建一个正确的键列表,但是当我单击第一个项目时,h2 包含所有键,而 p 包含所有值。我如何修改它,以便每个 key/value 对最终成为当前通过单击相应的关键列表项创建的 jQuery 移动页面的 h2 和 p?
我一直在尝试使用 How to populate a jQuery Mobile ListView with JSON data? 中的代码,但我无法完全使用它,因为它没有使用外部 JSON。
<!DOCTYPE html>
<html>
<head>
<title>Geo-Location Data</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/themes/geoLocation.css" />
<link rel="stylesheet" href="assets/css/themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="assets/js/geoLocation.js"></script>
</head>
<body>
<div data-role="page" id="homePage">
<div data-role="header">
<h1>Geo-Location Data</h1>
</div>
<div data-role="main" class="ui-content">
<div data-role="collapsible" data-collapsed="true">
<h2>Click here for Geo-Location Data</h2>
<ul id="list" data-role="listview" data-filter="true" data-inset="true" data-icon="user" data-autodividers="true">
</ul>
</div>
</div>
</div>
<div data-role="page" id="dataPage">
<div data-role="header">
<h2 id="dataHeading"></h2>
</div>
<div data-role="content">
<p></p>
</div>
</div>
</body>
</html>
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li>' + '<a href="#' + key + '" data-transition="slide">'+ key + '</a>' + '</li>');
$("ul#list").listview("refresh");
$("div#dataPage").attr("id", key);
$("h2#dataHeading").append(key);
$("p").append(val);
});
});
})
您正在使用 $.each
循环,因此它在循环遍历 json 数据时不断将 $("h2#dataHeading").append(key);
和 $("p").append(val);
附加到第二页,因此它实际上并没有创建单独的页面。它所做的只是更改 dataPage
页面的 id
一次,此后它将无法找到 $("div#dataPage")
所以我很惊讶项目中的所有链接都有效,除了第一个.
一种更有效的方法是使用 data-* 属性将 key
和 val
直接存储在列表项上,单击时抓取它们,附加到第二页并打开动态的第二页。这减轻了对单独页面的需求,同时保持 DOM 小
例如
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li data-key="'+key+'" data-val="'+val+'"><a>'+ key + '</a></li>');
$("ul#list").listview("refresh");
});
});
//The list item click function
$("#list> li").on("click", function() {
var key= $(this).attr("data-key");
var val= $(this).attr("data-val");
$("#dataHeading").empty().append(key);
$("p").empty().append(val);
$(":mobile-pagecontainer").pagecontainer("change", "#dataPage", { transition: "slide" });
});
});
我建议将 $(document).ready
替换为 pagecreate as JQM works best with inbuilt page Events
当使用 $(":mobile-pagecontainer") you could also send data to a page but you will need another function like pagebeforeshow 在显示数据之前将数据附加到页面时 -- 如果您决定这样做,请阅读注释以了解支持的版本。 JQM 弃用了版本中的某些事件以支持新的替换事件
一个解决方案。有效的方法是完全放弃第二个 JQM 页面,并通过将数据附加到 JavaScript 中的主体来创建它。这是更新后的代码。
<!DOCTYPE html>
<html>
<head>
<title>Geo-Location Data</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/themes/geoLocation.css" />
<link rel="stylesheet" href="assets/css/themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="assets/js/geoLocation.js"></script>
</head>
<body>
<div data-role="page" id="homePage">
<div data-role="header">
<h1>Geo-Location Data</h1>
</div>
<div data-role="main" class="ui-content">
<div data-role="collapsible" data-collapsed="true">
<h2>Click here for Geo-Location Data</h2>
<ul id="list" data-role="listview" data-filter="true" data-inset="true" data-icon="arrow-r" data-autodividers="true">
</ul>
</div>
</div>
</div>
</body>
</html>
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li>' + '<a href="#' + key + '" data-transition="slide">'+ key + '</a>' + '</li>');
$("body").append('<div data-role="page" id="'+ key +'"><div data-role="header" id=""><h2 id="dataHeading"></h2 ></div><div data-role="content"><p>'+ val +'</p></div></div>');
$("ul#list").listview("refresh");
});
});
});
我正在尝试从某些外部 JSON (http://ip-api.com/json) 获取数据。然后我想使用键来动态填充列表视图。我希望每个列表项都是一个 link,当您单击 link 时,jQuery 移动页面将包含该键的匹配值。我有两个 jQuery 移动页面,一个主页,另一个。
我试图通过将第二个 "data-role=page" div 的 id 更改为当前键数据,并将键数据附加到 h2,并将值数据附加到p.它正在创建一个正确的键列表,但是当我单击第一个项目时,h2 包含所有键,而 p 包含所有值。我如何修改它,以便每个 key/value 对最终成为当前通过单击相应的关键列表项创建的 jQuery 移动页面的 h2 和 p?
我一直在尝试使用 How to populate a jQuery Mobile ListView with JSON data? 中的代码,但我无法完全使用它,因为它没有使用外部 JSON。
<!DOCTYPE html>
<html>
<head>
<title>Geo-Location Data</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/themes/geoLocation.css" />
<link rel="stylesheet" href="assets/css/themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="assets/js/geoLocation.js"></script>
</head>
<body>
<div data-role="page" id="homePage">
<div data-role="header">
<h1>Geo-Location Data</h1>
</div>
<div data-role="main" class="ui-content">
<div data-role="collapsible" data-collapsed="true">
<h2>Click here for Geo-Location Data</h2>
<ul id="list" data-role="listview" data-filter="true" data-inset="true" data-icon="user" data-autodividers="true">
</ul>
</div>
</div>
</div>
<div data-role="page" id="dataPage">
<div data-role="header">
<h2 id="dataHeading"></h2>
</div>
<div data-role="content">
<p></p>
</div>
</div>
</body>
</html>
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li>' + '<a href="#' + key + '" data-transition="slide">'+ key + '</a>' + '</li>');
$("ul#list").listview("refresh");
$("div#dataPage").attr("id", key);
$("h2#dataHeading").append(key);
$("p").append(val);
});
});
})
您正在使用 $.each
循环,因此它在循环遍历 json 数据时不断将 $("h2#dataHeading").append(key);
和 $("p").append(val);
附加到第二页,因此它实际上并没有创建单独的页面。它所做的只是更改 dataPage
页面的 id
一次,此后它将无法找到 $("div#dataPage")
所以我很惊讶项目中的所有链接都有效,除了第一个.
一种更有效的方法是使用 data-* 属性将 key
和 val
直接存储在列表项上,单击时抓取它们,附加到第二页并打开动态的第二页。这减轻了对单独页面的需求,同时保持 DOM 小
例如
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li data-key="'+key+'" data-val="'+val+'"><a>'+ key + '</a></li>');
$("ul#list").listview("refresh");
});
});
//The list item click function
$("#list> li").on("click", function() {
var key= $(this).attr("data-key");
var val= $(this).attr("data-val");
$("#dataHeading").empty().append(key);
$("p").empty().append(val);
$(":mobile-pagecontainer").pagecontainer("change", "#dataPage", { transition: "slide" });
});
});
我建议将 $(document).ready
替换为 pagecreate as JQM works best with inbuilt page Events
当使用 $(":mobile-pagecontainer") you could also send data to a page but you will need another function like pagebeforeshow 在显示数据之前将数据附加到页面时 -- 如果您决定这样做,请阅读注释以了解支持的版本。 JQM 弃用了版本中的某些事件以支持新的替换事件
一个解决方案。有效的方法是完全放弃第二个 JQM 页面,并通过将数据附加到 JavaScript 中的主体来创建它。这是更新后的代码。
<!DOCTYPE html>
<html>
<head>
<title>Geo-Location Data</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/themes/geoLocation.css" />
<link rel="stylesheet" href="assets/css/themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="assets/js/geoLocation.js"></script>
</head>
<body>
<div data-role="page" id="homePage">
<div data-role="header">
<h1>Geo-Location Data</h1>
</div>
<div data-role="main" class="ui-content">
<div data-role="collapsible" data-collapsed="true">
<h2>Click here for Geo-Location Data</h2>
<ul id="list" data-role="listview" data-filter="true" data-inset="true" data-icon="arrow-r" data-autodividers="true">
</ul>
</div>
</div>
</div>
</body>
</html>
$(document).ready( function() {
$.getJSON("http://ip-api.com/json", function(data){
$.each(data, function(key, val){
$("ul#list").append('<li>' + '<a href="#' + key + '" data-transition="slide">'+ key + '</a>' + '</li>');
$("body").append('<div data-role="page" id="'+ key +'"><div data-role="header" id=""><h2 id="dataHeading"></h2 ></div><div data-role="content"><p>'+ val +'</p></div></div>');
$("ul#list").listview("refresh");
});
});
});