如何在不创建 100 个单个 html 文件的情况下从 json 为 100 个项目创建 html? [英] How to create html from json for 100 items without creating 100 single html files?

查看:18
本文介绍了如何在不创建 100 个单个 html 文件的情况下从 json 为 100 个项目创建 html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 phonegap 和 jquery mobile 开发了一个应用程序.我有 100 个项目,需要单页.所有这些的 html 都是相同的.它只是显示名称和一些信息.如何在不创建 100 个单个 html 文件的情况下将 1 个项目解析为我的 html 并将其与信息一起显示?我有 json 格式的所有信息.

用户点击这 100 个项目之一,然后新页面应出现

解决方案

你可能需要一种模板,你可以自己做,也可以使用统一的方法.只需在 Internet 上搜索jQuery + 模板",这将使您了解此类任务的多种可能性.你真的有很多方法可以实现你所需要的,

如果您需要互动、双向绑定和强大的 SO 支持者社区,恕我直言,knockout.js(或类似的)是一个不错的选择,正如之前的回答中已经指出的那样.

如果只需要简单明了的展示数据,nano是最小的您可以找到模板引擎,因此,这里有一个简单的 JQM 存根,其中包含使用这种方法的两个页面:

var all = [], current = {};var listTemplate = ['<li class="ui-first-child ui-last-child">','<a href="#page-card" data-id="{id}" class="ui-btn ui-btn-icon-right ui-icon-carat-r">','<h2>{名称}</h2>','<p><strong>{address.city}</strong></p>','<p>{email}</p>','<p class="ui-li-aside">id: <strong>{id}</strong></p>','</a>','</li>'].加入("");var cardTemplate = ['<h3 class="ui-bar ui-bar-a ui-corner-all">{name}</h3>','<div class="ui-body ui-body-a ui-corner-all">','<p>{email}</p>','<p>{网站}</p>','<p>{电话}</p>','<p>{address.street}</p>','<p>{address.city}</p>','</div>'].加入("");功能纳米(模板,数据){return template.replace(/{([w.]*)}/g, function(str, key) {var keys = key.split("."), v = data[keys.shift()];for (i = 0, l = keys.length; i a", function() {var id = $(this).data("id");当前 = $.grep(all, function(item) {返回 item.id == id;})[0];});$(document).on("pagecreate", "#page-list", function() {var $ul = $(this).find("ul");$.ajax({url: "https://jsonplaceholder.typicode.com/users",方法:'获取',跨域:真,数据类型:jsonp",完成:函数(){$ul.listview().listview("刷新");},成功:功能(结果){所有 = 结果;$.each(all, function(i, item) {$ul.append(nano(listTemplate, item))});}});});$(document).on("pagebeforeshow", "#page-card", function() {$(this).find("[data-role=content]").empty().append(nano(cardTemplate, current)).trigger("updatelayout");});

<头><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"><link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css"><script src="https://code.jquery.com/jquery-1.11.2.min.js"></script><script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script><身体><div data-role="page" id="page-list"><div data-theme="a" data-role="header" data-position="fixed"><h3>用户</h3>

<div data-role="内容"><ul data-role="listview" data-inset="true" data-filter="true">

<div data-role="page" id="page-card"><div data-theme="a" data-role="header" data-position="fixed"><h3>详情</h3><a href="#" data-rel="back" class="ui-btn-left">返回</a>

<div data-role="内容">

</html>

I develop an app with phonegap and jquery mobile. I have 100 items and need single pages for it. The html is the same for all of them. It´s just displaying names and some information. How can I parse 1 item to my html and display it with the information without creating 100 single html files? I have all the information as json.

EDIT: The user clicks on one of those 100 items and then the new page shall appear

解决方案

You may need a kind of templating, you can do it by yourself or use a consolidated approach. Just search the internet for "jQuery + template" and this will give you an idea of the many possibilities for such a task. You have REALLY a lot of ways to achieve what you need,

If you need interaction, two-way binding and a great SO community of supporters, IMHO knockout.js (or similar) is a good choiche, as already pointed out in a previous answer.

If you just only need to display data in a simple and straightforward manner, nano is the smallest template engine you could find, so, here is a simple JQM stub with two pages using this approach:

var all = [], current = {};

var listTemplate = [
  '<li class="ui-first-child ui-last-child">',
  '<a href="#page-card" data-id="{id}" class="ui-btn ui-btn-icon-right ui-icon-carat-r">',
  '<h2>{name}</h2>',
  '<p><strong>{address.city}</strong></p>',
  '<p>{email}</p>',
  '<p class="ui-li-aside">id: <strong>{id}</strong></p>',
  '</a>',
  '</li>'
].join("");

var cardTemplate = [
  '<h3 class="ui-bar ui-bar-a ui-corner-all">{name}</h3>',
  '<div class="ui-body ui-body-a ui-corner-all">',
  '<p>{email}</p>',
  '<p>{website}</p>',
  '<p>{phone}</p>',
  '<p>{address.street}</p>',
  '<p>{address.city}</p>',
  '</div>'
].join("");

function nano(template, data) {
  return template.replace(/{([w.]*)}/g, function(str, key) {
    var keys = key.split("."), v = data[keys.shift()];
    for (i = 0, l = keys.length; i < l; i++) { v = v[keys[i]]; }
    return (typeof v !== "undefined" && v !== null) ? v : "";
  });
}

$(document).on("vclick", "#page-list li>a", function() {
  var id = $(this).data("id");
  current = $.grep(all, function(item) {
    return item.id == id;
  })[0];
});

$(document).on("pagecreate", "#page-list", function() {
  var $ul = $(this).find("ul");
  $.ajax({
    url: "https://jsonplaceholder.typicode.com/users",
    method: 'GET',
    crossDomain: true,
    dataType: "jsonp",
    complete: function() {
      $ul.listview().listview("refresh");
    },
    success: function(result) {
      all = result;
      $.each(all, function(i, item) {
        $ul.append(nano(listTemplate, item))
      });
    }
  });
});

$(document).on("pagebeforeshow", "#page-card", function() {
  $(this).find("[data-role=content]").empty().append(nano(cardTemplate, current)).trigger("updatelayout");
});

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>

<body>
  <div data-role="page" id="page-list">
    <div data-theme="a" data-role="header" data-position="fixed">
      <h3>Users</h3>
    </div>
    <div data-role="content">
      <ul data-role="listview" data-inset="true" data-filter="true">
      </ul>
    </div>
  </div>
  <div data-role="page" id="page-card">
    <div data-theme="a" data-role="header" data-position="fixed">
      <h3>Details</h3>
      <a href="#" data-rel="back" class="ui-btn-left">Back</a>
    </div>
    <div data-role="content">
    </div>
  </div>
</body>

</html>

这篇关于如何在不创建 100 个单个 html 文件的情况下从 json 为 100 个项目创建 html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆